Span and div

From Wikipedia, the free encyclopedia
Jump to: navigation, search

In HTML, the span and div elements are for generic organization and styling beyond immediate elements like formatting tags "p" & "h".

All HTML elements have a single function and the name of that function is abbreviated for use as an "HTML tag," and most can or must be used in pairs or in combination with other tags. The most basic use, for example: a p, paragraph element, should contain a paragraph of text; while an h1, heading-1 element, should contain the highest-level heading of the page or section.[1]

span and div are also single function elements, but of a second and third order, reflecting "spanning" and "division" functions, defining the two useful levels of content specification: span defining an area of text to which some change (style class, formatting, content definition, etc.) can be applied immediately "in-line"; while a "div" typically defines a larger area of text of a specific style (defined "look"), type, or section of an HTML page, and can have its own formatting, classes, definitions—and contain an unlimited number of spans. Both are able to take advantage of CSS "programmatic scripting" convention to apply "named classes" that are prior-defined in a CSS statement or associated CSS file. Span and div are the two HTML "functions", then, that are specifically intended to be used to create organization and stylistic areas, without ambiguity.


The code tags were introduced to HTML in the internationalization WG second draft html-i18n on 1995-09-25. However it was not until HTML 4.01 the new element became part of the HTML language and appeared in the HTML 4 W3C Working Draft on 1997-09-17.[2]

Differences and default behavior[edit]

There are multiple differences between div and 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), however the permitted contents of each element may not be changed. For example, regardless of CSS, a span element may not contain block-level children.[3]

Practical usage[edit]

span and div elements are used purely to imply a logical grouping of enclosed elements.

There are three main reasons to use span and div tags with class or id attributes:

Styling with CSS[edit]

Perhaps the most common use of <span> and <div> elements is to carry class or 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 for compatibility with the concepts of the semantic web, discussed below, attributes attached to elements within any 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, and uses a more appropriate class name. By the correct use of CSS, '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 correct markup, rather than merely presentational.

Semantic clarity[edit]

This kind of grouping and labeling 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[edit]

Once the HTML or XHTML markup is delivered to a page-visitor's client browser, there is a chance that client-side code will need to navigate the internal structure (or Document Object Model) of the web page. The most common reason for this is that the page is delivered with client-side JavaScript that will produce on-going dynamic behavior after the page is rendered. For example, if rolling the mouse over a 'Buy now' link is meant to make the price, elsewhere on the page, become emphasized, JavaScript code can do this, but JavaScript needs to identify the price element, wherever it is in the markup. The following markup would suffice: <div id="price">$45.99</div>. Another example is the Ajax programming technique, where, for example, clicking a hypertext link may cause JavaScript code to retrieve the text for a new price quotation to display in place of the current one within the page, without re-loading the whole page. When the new text arrives back from the server, the JavaScript must identify the exact region on the page to replace with the new information.

Less common, but just as important examples of code gaining access to final web pages, and having to use span and div elements' class or id attributes to navigate within the page include the use of automatic testing tools. On dynamically generated HTML, this may include the use of automatic 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 div and 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 homemade mixture of div and span elements.[4]

For example, this:

<ul class="menu">
  <li>Main page</li>

... is usually preferable to this:

<div class="menu">
  <span>Main page</span>

Other examples of the semantic use of HTML rather than div and 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 div, span or table elements used for such purposes.[5]

HTML5 introduces new elements; a few examples include the header, footer, nav and figure elements.[6]

See also[edit]


  1. ^,-h2,-h3,-h4,-h5,-and-h6-elements
  2. ^ "HTML/Elements/span - Web Education Community Group". 2013-06-13. Retrieved 2013-11-14. 
  3. ^
  4. ^ 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 ul, ol, or dl instead of the current wrapper element. [...] For example to remove the bullets add this rule to the page's CSS stylesheet: [...]" 
  5. ^ 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. "The FIELDSET element 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." 
  6. ^ van Kesteren, Anne (2010). "HTML5 differences from HTML4". W3C. Retrieved 30 June 2010. 

External links[edit]