Skip to page content or skip to Accesskey List.
Search evolt.org
evolt.org login: or register

Work

Main Page Content

Guidelines for the use of <span>

Rated 3.88 (Ratings: 8) (Add your rating)

Log in to add a comment
(15 comments so far)

Want more?

  • More articles in Code
  • More articles by KUQ
 
Picture of KUQ

Keith Underdown

Member info | Full bio

User since: July 08, 2002

Last login: November 08, 2006

Articles written: 1

I would like to suggest some guidelines for the use of the <span> element. They come from a recent discussion on the CSS-discuss list.

In short: if you think you need <span> then think more carefully about other tags such as <cite>.

The discussion revolved around whether semantic information was being conveyed by the styling of the span. We came to the conclusion that it almost always was.

Let’s take a simple related case that doesn’t actually use a span because it predates them but could. One often finds <b> being used to make some characters bold. This could arguably be replaced by <span class="bold">bold</span> to get the same visual effect but we need to consider what is going on here. Why has the author picked out this particular piece of text? In general it is to emphasise it some way. (We are assuming that the author is intelligent enough not use something horrible like <h1><b>Heading</b&gt</h1>.) This points to the use of <strong> — it won't make any difference visually, but will give an aural browser a much better clue about how to render it. In fact this is an old argument and most CSS-savvy designers would accept it. How does this help us with spans? Well, if we apply it the other way around we can see that a relatively complex bit of markup collapses to something quite simple.

Another example cited in the discussion was the typographic convention of rendering naval vessels’ names in italics as in USS Enterprise. This is a kind of citation and one should therefore use <cite>, styled if necessary. My Oxford Manual of Style says this is the correct treatment for the names of aircraft and vehicles too. So we would have HMS Dreadnought and Chitty-chitty-bang-bang or Spirit of St Louis. (Hmm, now I have to pick a fight with the site designers here; <cite> is set to normal in the style sheet.)

My particular take on this was that I had been replacing <sup> mark up with a span using vertical-align and font-size to control the line spacing effect because a trusted correspondent suggested it but I’d begun to have doubts because I felt the <sup> did convey semantic information. An obvious example would be x=y2 or H2O. In my case I use them to make a standard "glyph" for a commonly recurring abbreviation St for "Saint". So this is another case of markup possible with a span but which can use simpler and more explicit markup.

In writing this I’ve tried hard to find good examples for the use of span and I thought I’d found two in my own work but I’m now convinced that in both cases styling existing, more semantically correct, tags would be better.

Acknowledgements

I use exactly the same parameters that I use for <sup> in a rule called "acknowledgement" but use a span to apply that. It seems obvious now that this has semantic significance and I ought to use a rule based on <cite>.

Footnotes

I have a blue-stocking daughter who uses lots of footnotes. Her technique in HTML is to use superscript numbers inside <a ...></a> to link them to the footnote text. It seems to me now that she should use <a class="footnote_ref" ....>, where there is a rule in the stylesheet like a.footnote_ref {font-size: 75%; vertical-align: 33%;}. There’s probably even a case for a counter here!

Document Control

I have used spans to denote new text within an existing paragraph. Another two useful tags in this context might be <ins> and <del> but they might be a bit heavy-handed in a non-legal context.

Conclusions

The general conclusion was that it was quite hard to come up with cases where there was no semantic overtones implied by the use of a span to make text look different in some way. So in summary, we can put forward the following principles for the use of <span> or its substitution by more meaningful markup.

  1. Be suspicious of spans because more often than not there will be some semantic element in what you are trying to convey. For example, until I discovered &lt;q&gt; I was using &lt;span class=&quot;quote&quot;&gt;
  2. Use &lt;em&gt; or &lt;strong&gt; rather than &lt;i&gt; or &lt;b&gt; unless the use is purely decorative. An example of the latter might be in a company logotype where the design department has specified that the product name will always be italicised. Although even that is a weak example. Better &lt;span class=&quot;logotype&quot;&gt;product name&lt;&#47;span&gt; or a &lt;logotype&gt; tag in XML so that when the branding changes to be all upper case or whatever then a simple stylesheet change is all it takes.
  3. If one is using XML then it is possible to make spans more explicit by declaring one’s own tag. &lt;ship&gt; in the example of the name of a naval vessel or &lt;logotype&gt; in the second example
  4. The use of &lt;sup&gt; and &lt;sub&gt; is usually semantic so it is good practice to use them but style them where necessary.
Wrote my first program in 1969! Worked on the VME operating system from 1973 to 1986. Then worked for UK bank as an systems analyst then Information Architect from 1986 to 1998 (I don't move around much!). Starting consulting in 1998 on my 50th birthday. That's dried up so I'm trying to reinvent myself as a web architect! See www.qconsultancy.co.uk.

lang / xml:lang

Submitted by luminosity on July 22, 2002 - 06:11.

One place where span can be useful is for setting the lang attribute when you change your document language, and it's not a direct quotation, or something else that suits existing markup easily. Maybe there should be a &lt;lang&gt; tag, although &lt;lang lang="en"&gt; would look sort of strange.

login or register to post comments

can't you set styles in articles?

Submitted by branko on July 22, 2002 - 07:13.

It would seem that you can set <span style="color: #903; font-variant: small-caps; text-shadow: 2px 2px;">styles in replies</span>; can't you set them in articles?

login or register to post comments

Styles in articles

Submitted by MartinB on July 22, 2002 - 09:58.

You can submit them, but they'll get edited out. Please refer to the code styleguide.

login or register to post comments

Underscores aren't allowed in class names

Submitted by skunk on July 23, 2002 - 05:42.

Great article, but your footnote example has an underscore in a class name which isn't allowed. Just thought I'd let you know :)

login or register to post comments

Here is my candidate

Submitted by IanO on July 23, 2002 - 17:24.

In writing this I’ve tried hard to find good examples for the use of span and I thought I’d found two in my own work but I’m now convinced that in both cases styling existing, more semantically correct, tags would be better

login or register to post comments

Styling existing semantic tags

Submitted by MartinB on July 23, 2002 - 17:50.

Ian, without wanting to be an arse over this, but in that case, why aren't you using &lt;em&gt; in your comment?

login or register to post comments

Without a scientific polling, I believe

Submitted by IanO on July 24, 2002 - 12:30.

that most users will more easily notice the yellow background color. Here is another example. I was doing accounting reports for a large company. I watched the comptroller going through the reports, turning page after page. Occasionally, she would stop and put a check next to some account. I asked her, "what is your criteria for this selection?" She replied that if the actual was 15% or more greater than budget, she wanted to know. I went back to my office and in 15 minutes those items were now displayed in red and bolded. It was a big hit with the managers who were bleary eyed from staring at pages and pages of numbers. Do you think that if I caused an < em > type font change without the color change it would have been equally as effective?

login or register to post comments

Em -v- colour change

Submitted by MartinB on July 24, 2002 - 16:19.

  1. This site is not a multi-page accounting report with lots of visually undistinguished figures
  2. This site has code guidelines which exclude simplistic colour changes for emphasis
  3. Colour changes are absolutely no earthly use to people using screenreaders

The compromise in places where the existing style guide permits colour emphasis is to style the appropriate semantic tag thusly:

&lt;em style=&quot;background-color:yellow&quot;&gt;emphasised text&lt;/em&gt;

login or register to post comments

or even better...

Submitted by skunk on July 24, 2002 - 18:17.

&lt;em class="fifteenPercentOverBudget&gt;some account&lt;em&gt;<br /><br />

&lt;style type="text/css"&gt;<br />
.fifteenPercentOverBudget {<br />
  color: red;<br />
  background-color: yellow;<br />
  font-weight: bold;<br />
}<br />
&lt;/style&gt;

The name of the class adds additional meaning to the markup at a source level, and the use of a class rather than an inline style attribute allows the properties of every em of that type on the page to be instantly changed by editing the CSS.

login or register to post comments

I stand corrected

Submitted by IanO on July 25, 2002 - 13:29.

Your solution is much more elegant than mine.

I have another example for the use of the < span > tag that I attempted to Email to you. I clicked on your name in these comments and was directed to your page. There I clicked again and got a .uk address. I sent the example to you but it bounced with a User unknown message.

The example was a For Sale poster for a neighbor's car. At the bottom of the page I included some tear off tabs with his phone number. The style include dotted borders and rotated text. Each tab was the same text repeated again and again included in < span > tags with the .class name. Let me know if your Email address is corrected and I'll send you the complete example (only tested on IE 5.5).

login or register to post comments

semantic de-emphasis

Submitted by EdwardGeorge on July 30, 2002 - 07:02.

Everyone goes on about not using spans when there is a tag that semantically explains the styling but these all seem biased towards emphasis, i tend to use spans to de-emphasize text and i can't seem to find a tag that fits this styling. Anyone have any ideas for this situation?

login or register to post comments

Maybe a silly question...

Submitted by kiptester on August 15, 2002 - 14:10.

Functionally, what is the difference between span and div?

login or register to post comments

difference between span and div

Submitted by skunk on August 16, 2002 - 06:32.

Span is an inline element, div is a block level element. This is like the difference between an italic tag and a paragraph - block level elements always display on a new line, while inline elements can be placed within text and will not cause any line breaks. Block level elements also have widths, heights, margins and padding - inline elements dont have these (or shouldn't have them).

login or register to post comments

So, when do you think it's advisable to use span?

Submitted by lisrael on September 3, 2002 - 02:44.

The main point of the article, seems to me, is that we should seriously scrutinize our use of span tags, and use semantic tags instead whenever possible. That's a very good point. But the article doesn't go beyond that to look at the bigger picture. When is it advisable to use a span? Without answering that question, I think the guidelines are incomplete.

My own answer is that it's appropriate to use spans to define content that meets all three of these conditions:
1. You need to distinguish it from other content (want a different style, need to apply scripting to it, etc.)
2. It's inline
3. There is no other appropriate (structural or semantic) tag within the HTML spec.
(Additions to the list are welcome.)

As one example, I maintain a music festival site. Some of the pages have workshop listings. I use spans to define types of content:
< SPAN CLASS="wkstitle" >
< SPAN CLASS="wksteacher" >
< SPAN CLASS="wkstime" >
< SPAN CLASS="wksdescription" >
Since there is no < description > or < teacher > tag in HTML, I use spans. Any feedback on how that fits within these Guidelines? XML would clearly be better suited to this sort of usage, but we're not there yet.

login or register to post comments

So when is it advisable to use span?

Submitted by KUQ on September 18, 2002 - 02:42.

lisrael has restated the article's main point! Essentially, be careful to use the tags that do exist before using <span>. When there is no suitable tag use a span but give it an appropriate class. When you migrate to XML declare the class as a tag.

login or register to post comments

The access keys for this page are: ALT (Control on a Mac) plus:

evolt.orgEvolt.org is an all-volunteer resource for web developers made up of a discussion list, a browser archive, and member-submitted articles. This article is the property of its author, please do not redistribute or use elsewhere without checking with the author.