div and span
span tags are elements used to define parts of a document, so that they are identifiable when a unique classification is necessary. Where other HTML elements such as
em (emphasis), and so on, accurately represent the semantics of the content, the additional use of
div tags leads to better accessibility for readers and easier maintainability for authors. Where no existing HTML element is applicable,
div can valuably represent parts of a document so that HTML attributes such as
dir can be applied.
span represents an inline portion of a document, for example words within a sentence.
div represents a block-level portion of a document such as a few paragraphs, or an image with its caption.
div stands for division. The elements allow semantic attributes (e.g.
lang="en-US"), CSS styling (e.g., color and typography), or client-side scripting (e.g., animation, hiding, and augmentation) to be applied.
For example, to make a certain part of text inside a paragraph red,
span can be used as follows:
(web code used to add formatting to text)
(as seen by visitors of the webpage)
I am <span style="color: red;">red</span> and I am <span style="color: blue;">blue</span>!
I am red and I am blue!
span element was introduced to HTML in the internationalization working group's second draft html-i18n in 1995. However, it was not until HTML 4.01 that it became part of the HTML language, appearing in the HTML 4 W3C Working Draft in 1997.
span was introduced to mark up any inline span of text, because 'a generic container is needed to carry the LANG and BIDI attributes in cases where no other element is appropriate.' It still serves that general purpose, although a much richer range of semantic elements have been defined since then, and there are also many more attributes that may need to be applied.
div defines a 'division' of the document, a block-level item that is more distinct from elements above and below it than a span of inline material.
Differences and default behaviour
There are multiple differences between
span. The most notable difference is how the elements are displayed. In standard HTML, a
div is a block-level element whereas a
span is an inline element. The
div block visually isolates a section of a document on the page, and may contain other block-level components. The
span element contains a piece of information inline with the surrounding content, and may only contain other inline-level components. In practice, the default display of the elements can be changed by the use of Cascading Style Sheets (CSS), although the permitted contents of each element may not be changed. For example, regardless of CSS, a
span element may not contain block-level children.
div elements are used purely to imply a logical grouping of enclosed elements.
There are three main reasons to use
div tags with
Styling with CSS
It is common for
<div> elements to carry
id attributes in conjunction with CSS to apply layout, typographic, color, and other presentation attributes to parts of the content. CSS does not just apply to visual styling: when spoken out loud by a voice browser, CSS styling can affect speech-rate, stress, richness and even position within a stereophonic image.
For these reasons, and in support of a more semantic web, attributes attached to elements within HTML should describe their semantic purpose, rather than merely their intended display properties in one particular medium. For example, the HTML in
<span class="red-bold">password too short</span> is semantically weak, whereas
<em class="warning">password too short</em> uses an
em element to signify emphasis (appearing as text in italics), and introduces a more appropriate class name. By the correct use of CSS, such 'warnings' may be rendered in a red, bold font on a screen, but when printed out they may be omitted, as by then it is too late to do anything about them. Perhaps when spoken they should be given extra stress, and a small reduction in speech-rate. The second example is semantically richer markup, rather than merely presentational.
This kind of grouping and labelling of parts of the page content might be introduced purely to make the page more semantically meaningful in general terms. It is impossible to say how the World Wide Web will develop in years and decades to come. Web pages designed today may still be in use when information systems that we cannot yet imagine are trawling, processing, and classifying the web. Even today's search engines such as Google and others use proprietary information processing algorithms of considerable complexity.
For some years, the World Wide Web Consortium (W3C) has been running a major Semantic Web project designed to make the whole web increasingly useful and meaningful to today's and the future's information systems.
The microformats movement is an attempt to build an idea of semantic
classes. For example, microformats-aware software might automatically find an element like
<span class="tel">123-456-7890</span> and allow for automatic dialing of the telephone number.
Access from code
Automatic testing tools also may need to navigate web page markup using
id attributes. In dynamically generated HTML, this may include the use of page testing tools such as HttpUnit, a member of the xUnit family, and load or stress testing tools such as Apache JMeter when applied to form-driven web sites.
The judicious use of
span is a vital part of HTML and XHTML markup. However, they are sometimes overused.
Various list structures available in HTML may be preferable to a home-made mixture of
For example, this:
<ul class="menu"> <li>Main page</li> <li>Contents</li> <li>Help</li> </ul>
... is usually preferable instead of this:
<div class="menu"> <span>Main page</span> <span>Contents</span> <span>Help</span> </div>
Other examples of the semantic use of HTML rather than
span elements include the use of
fieldset elements to divide up a web form, the use of
legend elements to identify such divisions and the use of
label to identify form
input elements rather than
table elements used for such purposes.
HTML5 introduced several new elements; a few examples include the
figure elements. The use of semantically appropriate elements provides better structure to HTML documents than
- HTML and HTML5
- HTML element
- HTML attribute
- Cascading Style Sheets and Help:Cascading Style Sheets
- ContextObjects in Spans (COinS)
- Semantic Web
- ^ a b "HTML5: A vocabulary and associated APIs for HTML and XHTML". W3C. 4.4 Grouping content. Retrieved 16 September 2014.
- ^ a b "HTML5: A vocabulary and associated APIs for HTML and XHTML". W3C. 4.5 Text-level semantics. Archived from the original on 1 August 2015. Retrieved 16 September 2014.
- ^ "HTML/Elements/span - Web Education Community Group". 2013-06-13. Retrieved 2013-11-14.
- ^ "HTML <div> Tag". W3Schools. Retrieved 22 March 2018.
- ^ "HTML 5.1: 4. The elements of HTML". W3.org. Retrieved 3 August 2017.
- ^ Harold, Elliotte Rusty (2008). Refactoring HTML. Addison Wesley. p. 184. ISBN 0-321-50363-5.
There is no simple way to find all the unidentified lists in a site. [...] They can be marked up in dozens of different ways: as paragraphs,
divs, tables, [etc]. Once you've found a list, marking up the individual items is easy. Just use
dlinstead of the current wrapper element. [...] For example to remove the bullets add this rule to the page's CSS stylesheet: [...]
- ^ Raggett, Dave; Arnaud Le Hors; Ian Jacobs (1999). "Adding structure to forms: the FIELDSET and LEGEND elements". HTML 4.01 Specification. W3C. Retrieved 12 July 2010.
FIELDSETelement allows authors to group thematically related controls and labels. Grouping controls makes it easier for users to understand their purpose while simultaneously facilitating tabbing navigation for visual user agents and speech navigation for speech-oriented user agents. The proper use of this element makes documents more accessible.
- ^ van Kesteren, Anne (2010). "HTML5 differences from HTML4". W3C. Retrieved 30 June 2010.