Supported by :   My Law Forms Ltd. - Deed polls and Tenancy Agreements

XHTML help – DIV or SPAN? DIV vs SPAN.

These two tags are often used inter-changeably by developers for different reasons, so what are the differences between them? How should they be used to markup web pages?

The basic difference between the two is their default behaviour. DIV tags are block-level tags and can be used as containers for other tags. SPAN tags are inline tags and should only contain content. These default behaviours can of course be altered by CSS definitions, but are default in any browser context.

As a reference, if you want to align text within these tags use the following CSS attributes;

DIV - text-align: left|right|center|justify
SPAN - float: left|right

DIV tags are also used to define logical divisions of page content. It also allows CSS to be defined for whole sections of HTML. By applying an id to a DIV you can also add controls using JavaScript.

A SPAN tag is not intended to container other tags, but rather to allow localised styling. This is especially useful when combined with classes. It has even been suggested that future semantic web models will use such markup to define relevant content.

For example by defining a class telephone, it is possible ensure all telephone numbers are displayed in the same fashion:

<span class="telephone">44-20-555-4444</span>

SPAN tags have similar properties but should be used to reference inline content and apply style by classes.

In summary: use DIVs for defining sections of a page, and SPANs to enclose and style text or classes of text.

This entry was posted on Thursday, April 19th, 2007 at 1:58 pm and is filed under HTML help, XHTML tips. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

2 Responses to “XHTML help – DIV or SPAN? DIV vs SPAN.”

  1. Andy Nguyen says:

    Thanks! I always thought of the difference as DIVs result in “line-breaks” and SPANs do not!

  2. Jason Robertson, Elmtree says:

    nice clear explanation