Span and div

Span and div
HTML
This box: view · talk · edit

In HTML, the span and div elements are used where parts of a document cannot be semantically described by other HTML elements.

Most HTML elements carry semantic meaning – i.e. the element describes, and can be made to function according to, the type of data contained within. For example, a p element should contain a paragraph of text, while an h1 element should contain the highest-level heading of the page; user agents should distinguish them accordingly. However, as span and div have no innate semantic meaning besides the logical grouping of the content, they can be used to specify non-standard presentation or behaviour without superfluous semantic meaning.

Contents

Differences and default behavior

There is one difference between div and span. 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, in the same way as a paragraph. The span element contains a piece of information inline with the surrounding text. In practice, even this feature can be changed by the use of Cascading Style Sheets (CSS).

Practical usage

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

When they are labeled with class or id attributes, span and div elements can denote types of information otherwise indescribable with HTML. For example, <div id="byline">Fred Smith</div> may be used to indicate the author's name in a document, and <span class="date">10th Feb 2010</span> may be used specifically to indicate a date.

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

Styling with CSS

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, <span class="red small">password too short</span> is semantically meaningless, whereas <span class="warning">password too short</span> is much more useful.[1] By the correct use of CSS, on the screen 'warnings' may be rendered in a red, small font, 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 semantic markup, rather than merely presentational, but serves both purposes when combined with CSS.

Semantic clarity

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 and in what ways 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.

The World Wide Web Consortium (W3C) has for some years 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.

During the page design process, the designer has a clear idea of exactly the purpose and meaning of each element and sub-element of the content. If possible, and if standard HTML elements exist that express that meaning, they should be used. If not, there is no better time to encapsulate the meaning and purpose in a span or div element with appropriate class or id attributes. If nothing more, doing so will help future editors to maintain the markup.

The microformats movement is an attempt to build on this 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

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, in order to affect it. 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.

Overuse

The judicious use of div and span is a vital part of HTML and XHTML markup. However, they are sometimes overused.

For example, when structurally and semantically a series of items need an outer, containing element and then further containers for each item, then there are various list structures available in HTML, one of which may be preferable to a homemade mixture of div and span elements.[2]

For example, this...

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

...is usually preferable to 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 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.[3]

HTML5 introduces many new semantic elements and attributes that are intended to be used in these ways. A few examples include the header, footer, nav and figure elements.[4]

See also

References

  1. ^ Harold, Elliotte Rusty (2008). Refactoring HTML. Addison Wesley. p. 133. ISBN 0-321-50363-5. "HTML does not have individual elements representing these uses. Instead they should be indicated by a span or div element whose class attribute indicates the reason for formatting the text as italic: [example ...] <span class="species">..." 
  2. ^ 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: [...]" 
  3. ^ Raggett, Dave; Arnaud Le Hors, Ian Jacobs (1999). "Adding structure to forms: the FIELDSET and LEGEND elements". HTML 4.01 Specification. W3C. http://www.w3.org/TR/1999/REC-html401-19991224/interact/forms.html#h-17.10. 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." 
  4. ^ van Kesteren, Anne (2010). "HTML5 differences from HTML4". W3C. http://www.w3.org/TR/html5-diff/#language. Retrieved 30 June 2010. 

External links


Wikimedia Foundation. 2010.

Игры ⚽ Поможем сделать НИР

Look at other dictionaries:

  • Span — may refer to length or space:* span (length), the width of a human hand * span (architecture) ** For powerlines, the distance between two pylons ** For aerial tramways, the distance between two supporting structures ** For a bridge, the distance… …   Wikipedia

  • Base Realignment and Closure — (or BRAC) is a process of the United States federal government directed at the administration and operation of the Armed Forces, used by the United States Department of Defense (DoD) and Congress to close excess military installations and realign …   Wikipedia

  • Lewis and Clark Expedition — This article is about the early 19th century expedition. For Lewis and Clark, see Lewis and Clark (disambiguation). Route of the expedition The Lewis and Clark Expedition, or ″Corps of Discovery Expedition (1804–1806) was the first… …   Wikipedia

  • HTML element — This article is about the HTML elements in general. For information on how to format Wikipedia entries, see Help:Wiki markup and Help:HTML in wikitext HTML HTML and HTML5 Dynamic HTML XHTML XHTML Mobile Profile and C HTML Canvas element Character …   Wikipedia

  • HTML — For the use of HTML on Wikipedia, see Help:HTML in wikitext. HTML (HyperText Markup Language) Filename extension .html, .htm Internet media type text/html Type code TEXT …   Wikipedia

  • Tableless web design — Cascading Style Sheets CSS Animations Dynamic CSS Comparison of layout engines Comparison of stylesheet languages Internet Explorer box model bug CSS Zen Garden The Zen of CSS Design CSSTidy Style sheet Tableless web design Holy Grail (web… …   Wikipedia

  • Classical Hamiltonian quaternions — For the history of quaternions see:history of quaternions For a more general treatment of quaternions see:quaternions William Rowan Hamilton invented quaternions, a mathematical entity in 1843. This article describes Hamilton s original treatment …   Wikipedia

  • U.S. Route 41 in Michigan — This article is about the section of highway in Michigan. For the entire length of highway, see US Highway 41. US Highway 41 US 41 highlighted in red …   Wikipedia

  • Template:Navbox — [edit] [ …   Wikipedia

  • Micro-format — Microformat Un microformat (parfois abrégé sous μF ou uF) est une approche de formatage de données basé sur le web[1] qui cherche à réutiliser le contenu existant comme les métadonnées, en n utilisant que des classes[2] et attributs[3] XHTML et… …   Wikipédia en Français

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”