The span element seems to be exactly like a div, but at the in-line level rather than at the block level. However, I can't seem to think of any beneficial logical divisions that the span element can provide.
A single sentence, or word if not contained in a sentence, seems to be the smallest logical part. Ignoring CSS, since CSS is only for layout and not for semantic meaning, when does span provide additional semantic value by chopping up a sentence or string of words?
It seems that in all cases, other elements are better suited to adding semantic value, making span a purely layout element. Is this true?
Span can be used to add semantic meaning that falls outside the scope of HTML. This can be done by using classes which identify certain attributes. For example, if you are writing a science-fiction novel you can use span to identify made-up words, because you may want to format those differently, or because you may want the spell-checker to ignore them:
Then the wizard called upon the <span class="wizardword">gravenwist</span> and bade it attack the approaching army. The <span class="wizardword">gavenwist</span> resisted but the wizard's <span class="wizardword">wistwand</span> was too powerful.
This could render as
Then the wizard called upon the gravenwist and bade it attack the approaching army. The gavenwist resisted but the wizard's wistwand was too powerful.
Another good example of this sort of thing are microformats, which allow the creation of arbitrary structure within HTML:
<span class="tel">
<span class="type">home</span>:
<span class="value">+1.415.555.1212</span>
</span>
The advantage of span, versus div, is that spans can appear almost everywhere because they are inline content, and divs are block elements, so they can only occur inside certain other elements.
A very useful benefit would be to mark changes in language. E.g.
<p>Welcome to Audi UK, <span lang="de">Vorsprung durch Technik</span>.</p>
Screen readers with multiple language capabilities could make use of this.
So they're not presentational, just generic. In fact, spans are rarely presentational, providing a semantically-meaningful class name is used, like "spelling-mistake" and not "bold-red-text".
<div class="name">
<span class="firstname">John</span>
<span class="lastname">Doe</span>
</div>
It depends completely on what you want to express. If marking up the first name is of semantic value to you (be it just to have a hook for CSS to format first names or to extract them using some scripting language), then you can use a span.
I use SPAN a lot when I want to have JavaScript parse the element and insert some value inside the tag, for example:
<span datafield="firstname"></span>
Would have a value inserted into it later, so in that case it does have meaning, though only a meaning that I decide to give it. The fact that span otherwise has no effect on the layout is ideal in that case.
spans can actually be carriers for semantic information in form of class attributes. This is used by microformats.
span tags need a class or id attribute to give them meaning.
e.g. <span class="personal_phone_number">0123 456789</span>
Ignoring CSS, since that will give the
semantic meaning, when does span
provide additional semantic value by
chopping up a sentence or string of
words?
Ignoring CSS (and other non-HTML markup), never. A <span>'s only purpose in life is to carry markup that you can't express in HTML. Markup such as <span style="dropCap">, which doesn't have an equivalent in HTML but has existed in print publishing for hundreds of years, and which is always applied to just one character - the first letter of an item (article, whatever), without causing a word-break (or any larger break).
It seems that in all cases, other
elements are better suited to adding
semantic value, making span a purely
layout element. Is this true?
Yes and no. The only real value of <span> is that it is semantically neutral. That is, unlike for example <p>, it doesn't do anything that you might want to have it not do when you're using it to carry other markup. And there are times, like <span style="dropCap"> above, when you don't want any other effects.
If you want to apply formatting rules to part of the contents (for example a single word or sentence) of a tag. You can use the span tag. It is sometimes called tagless formatting.
I use spans in my EBNF -> XHTML converter to apply a different format to literals and tokens.
SPAN (and DIV) elements by themselves are generally considered to be semantically neutral. A good approach is to use semantic markup as much as appropriately possible, but sometimes you run into situations where the existing html elements that do provide semantic meaning (EM, STRONG, ABBR, ACRONYM, etc, etc) aren't the right fit semantically for your content. So the next step is to use a semantically neutral SPAN or DIV with a semantically meaningful id or class.
I think he's asking about the difference between a div and a span, and there really isn't one, other than the default behavior.
It's a matter of convention. When using styling, div is typically used to demarcate divisions of content, while span is used to demarcate inline text. You could just as easily use div everywhere or use span everywhere, but it's helpful to think of them as different, even if it's only by convention.
In HTML could be used for microformats. But since actual HTML specification is XHTML, there is no point.
Instead of:
<P>Hello, my name is <SPAN class="name"> Joe Sixpack </SPAN></P>
I'd rather use:
<P>Hello, my name is <FOAF:name> Joe Sixpack </FOAF:name></P>
The meaning of SPAN is "this is a (generic) span of (e.g., text) content". Compare to DIV, which means "this is a logical division (i.e., a generic document section)".
SPAN is mainly a hook for hanging styles off of (so you can use <span style='color:blue'> instead of <font color='blue'>).
From the spec:
The DIV and SPAN elements, in conjunction with the id and class attributes, offer a generic mechanism for adding structure to documents. These elements define content to be inline (SPAN) or block-level (DIV) but impose no other presentational idioms on the content. Thus, authors may use these elements in conjunction with style sheets, the lang attribute, etc., to tailor HTML to their own needs and tastes.
Suppose, for example, that we wanted to generate an HTML document based on a database of client information. Since HTML does not include elements that identify objects such as "client", "telephone number", "email address", etc., we use DIV and SPAN to achieve the desired structural and presentational effects. We might use the TABLE element as follows to structure the information:
<!-- Example of data from the client database: -->
<!-- Name: Stephane Boyera, Tel: (212) 555-1212, Email: sb#foo.org -->
<DIV id="client-boyera" class="client">
<P><SPAN class="client-title">Client information:</SPAN>
<TABLE class="client-data">
<TR><TH>Last name:<TD>Boyera</TR>
<TR><TH>First name:<TD>Stephane</TR>
<TR><TH>Tel:<TD>(212) 555-1212</TR>
<TR><TH>Email:<TD>sb#foo.org</TR>
</TABLE>
</DIV>
<DIV id="client-lafon" class="client">
<P><SPAN class="client-title">Client information:</SPAN>
<TABLE class="client-data">
<TR><TH>Last name:<TD>Lafon</TR>
<TR><TH>First name:<TD>Yves</TR>
<TR><TH>Tel:<TD>(617) 555-1212</TR>
<TR><TH>Email:<TD>yves#coucou.com</TR>
</TABLE>
</DIV>
Related
I've always used <b> tag to bold something, because that is the way I was taught to do it a long time ago. But now my IDE always informs me that <b> is deprecated and to use css style. Assuming by that they want me to use <div style="font-weight:bold;">Bold Text</div>. How vital is this message that my IDE is giving me? Should I go back and change all my<b> to style?
Below is an example of both situations. Could someone explain the difference's between both and why <b> is deprecated now?
<b>Bold Text</b>
Vs.
<div style="font-weight:bold;">Bold Text</div>
Would <b> be better because if someone has css turned off on the browser, it would still be show correctly?
The correct question is: "What markup best describes my content?"
Let's start with the <b> tag (which is not deprecated):
The b element represents a span of text to be stylistically offset
from the normal prose without conveying any extra importance, such as
key words in a document abstract, product names in a review, or other
spans of text whose typical typographic presentation is boldened.
...
You should not use b and i tags if there is a more descriptive and
relevant tag available. If you do use them, it is usually better to
add class attributes that describe the intended meaning of the markup,
so that you can distinguish one use from another.
...
It may help to think of b or i elements as essentially a span element
with an automatic fallback styling. Just like a span element, these
elements usually benefit from class names if they are to be useful.
http://www.w3.org/International/questions/qa-b-and-i-tags
By comparison, <strong> has a more specific purpose:
The strong element represents a span of text with strong importance.
http://www.w3.org/TR/html-markup/strong.html
For example:
<p><strong>Warning.</strong> Here be dragons.</p>
Here we emphasize the word "warning" to stress its importance.
But not:
<p><strong>Item 1:</strong> Foo, bar, and baz.</p>
"Item 1" isn't meant to be stressed, so <strong> is the wrong tag. Furthermore, it's possible that the whole structure could be better represented.
If the meaning of the text has strong importance, <strong> is appropriate (just like this line).
Perhaps you just want a thicker font for style purposes and the text has no particular meaning. In that case, neither <strong> nor <b> may be appropriate.
<span class="product-name">Hello World</span>
.product-name { font-weight: bold; }
In all cases:
Use the markup which describes the content.
Do not use inline styles (use an external stylesheet).
Do not name styles based on their visual representation (e.g. naming a style "bold" is a poor choice)
Would <b> be better because if someone has css turned off on the
browser, it would still be show correctly?
No. Use the correct markup for the job. It's fairly unusual for someone using the visual representation of your site to willingly disable the stylesheet, but non-visual consumers care primarily about the structure of your document. A "non-visual consumer" could be a search engine parsing your content or a screen reader application.
Additional Reading:
http://www.w3.org/TR/html51/text-level-semantics.html#the-strong-element
http://www.w3.org/TR/html51/text-level-semantics.html#the-b-element
It's not "vital" if the code still works. Though it would conform to current standards which will give the code a longer future.
The difference is that using CSS separates your styling from your content. <b> is a style, nothing more. And it tightly couples that markup to that style. The separation allows you to emphasize the markup in other ways instead of always using a bold font.
Would be better because if someone has css turned off on the browser, it would still be show correctly?
No, because if the user wants to disable styling then your <b> tag undermines that, because it's mixing styling with content.
You should be using <strong> in place of <b>. You could use styles (text-weight: bold in a separate sheet) if a particular group of text was always going to be bold, and you didn't (or couldn't) want to use <strong> for whatever reason. But I would only go that route if you already were applying other styles to that same element.
If you are talking about SEO
Use <strong> should be SEO friendly too... (focus on the keywords)
and it's important !
I find that using <strong></strong> is the better approach than using <b> or inline styles.
In XHTML/HTML which elements has semantic value , which are presentational and which are not in both category?
And who decide which tag is semantic, presentational? W3C or web developer with their own terms?
What is the difference between structural and semantic mark-up?
Is DIV and span not semantic , if yes then why we use ?
Ordered lists (OL) and unordered lists (UL), for instance, are examples of markup elements with some semantic loading. The idea is to show to a client that several elements are somehow connected, for example, represent options in some menu. It helps screen readers which will read the menu options in sequence.
And who decide which tag is semantic, presentational? W3C or web developer with their own terms?
Both. The standards dictate what and how is supposed to be used. Developers can either agree or ignore it (as with the ubiquitous case with tables used for design).
I believe the general idea is to treat (X)HTML construct as structural (with semantic meaning or without one), and use CSS to adjust presentational properties.
It depends, in general presentational elements are the ones that act on the look and feel of the object, while semantic gives only "significate".
Example: what is the difference between:
<h1>Hey, I'm a title</h1>
and
<font size="36px">Hey, I'm a title</h1>
?
Pretty nothing, but tags are the one with also semantic value, because they mean something, instead of the font tag that contains only presentational things.
Semantic is about the meaning of an element.
Presentation is about the look of an element.
E.g. <i> is a presentational statement, while <blockquote> or <q> is a semantic statement.
I don't know whether there is a definitive list of elements that are seen as more semantic or more presentational, but generally you should be able to decide which one it is by simply asking you whether the markup says something about what the element is or what it should look like.
<span> and <div> tags can be used to convey information about both, but they should be used to say something about the purpose of the element. The styling can then be set via CSS. So, while <span> and <div> have no meaning per se, the class or ID set for them should hold information about their purpose rather than their look.
What are practical cons to use W3C valid presentational element, which are not listed as deprecated
for XHTML-CSS developers,
Sighted End users of site,
and for Screen reader users, ?
like <b> , <i>, <br>, <hr>, <small> If i use these tags for presentational purpose. (note: these are also supported in HTML 5)
for example:
If i use <b> in place of <span style="font-weight: bold">
If i use <i> in place of <span style="font-style: italic">
If i use <br> to make line break in paragrah not to make space
If i use <hr> in place of <div style="border-bottom: 1px solid #666">
If i use <small> in place of <span style="font-size: 9px">
I know the difference between <strong> and <b>. My question is not about <strong> vs <b>
There's not a ‘con’ to using them per se. There's a ‘con’ to using them for the wrong thing. They have often been used for the wrong thing in web pages in the past, but that doesn't mean there's no right thing.
<i> is semantically equivalent to <span style="font-style: italic">: that is to say, there is no semantic content. A word inside <i>...</i> is not more important; a web page text-to-speech converter shouldn't(*) read that word in an emphasised tone of voice.
When you want to emphasise a word, like ‘definitely’ in this sentence, you should definitely use <em>. This is the common case. But when you merely want a typographical detail, such as italicising a block of text just for visual purposes, or typographical quirks like always refering to your site as “ThingsWorld!”, <em> isn't suitable and you'd want the semantics-free version (the <i> or the <span>).
Similarly, <strong> is suitable for “for the wrong thing” above as it is meant to be strongly emphasised. If you just wanted part of the text to be rendered in bold, without implying that it is more important than the surrounding text, you'd use <b> or, again, styles on another element like <span> or <div>.
A common use for <i> and <b> is when you are taking text from an unsemantic source. In particular it is common to be importing content from a word processor or similar application where there is no concept of emphasis as such, only ‘italic’ and ‘bold’. In this case <i> and <b> (or the span equivalents) are appropriate, because you don't know what the semantic intent behind those text styles is; that information is already lost.
If you auto-converted all italics to <em> like many tools do, you might be marking up italics that aren't meant to represent emphasis. For example references to other works, which would be better marked up with <cite>, or words/phrases from another language such as Latin or French, which would be better marked up with <span lang> (and associated style to tell it words from another language should be italicised).
<small> and <big> might theoretically be the same case, but whilst imported text with bold/italic in is common, imported sized text isn't. Consequently no-one uses these tags and you're generally better off with the normal CSS font-size styles.
<br> and <hr> are arguably not presentational in the same way that the above tags are. A line break can be an important part of content (eg. in separating lines in an address), and a horizontal rule represents a stronger separation between sections of text than a mere paragraph. Of course if you use <br> to create fake paragraphs you're doing it wrong (and you're David Siegel circa 1998), and using an <hr> just to get a nice little line that's not right either.
(*: illustrative example; finding out what real screen readers actually do is left as an exercise for the reader.)
For completeness here is the Index of Elements that lists which are deprecated and which aren't (and you are correct in stating them as not deprecated).
Generally speaking however people use:
<strong> instead of <b>;
<em> instead of <i>;
<hr> and <br> because there is no alternative;
<small> is in my experience rarely used, with CSS style being used instead.
It's important to understand that <strong> and <em> have semantic meaning but <b> and <i> do not so there are valid use cases for both.
hrs are a pain to style with CSS without adding more html
brs just annoy me as they are not really required when you having padding and margins to play with
I tend to use em or strong rather than i or b - both of these imply italic or bold whereas em or strong are a bit more abstract so if you want to give some 'em'phasis to some text or make some text look a bit stronger they make more sense without necessarily being bold or italic
The cons of using <br> elements to indicate line-breaks (and therefore not using <p> elements to indicate paragraphs) is that you're limiting your scope of CSS control over the document. It's much easier to manage many <p> elements of text, than text interspersed with <br>s in one element via CSS.
Whenever I put together am HTML document, I use
<br> to indicate a 'soft' break.
(As above: rather use margins on <p> elements
to show paragraphs.)
<hr> to
indicate the division of two sections
of the document, e.g. between the
header and navigation, or navigation
and content. When a browser without
CSS support views the document, a
clear divide is visible. I then hide the <hr>s via the CSS.
Of your 5 examples, only this one, I think, has significance: "# If i use <small> in place of <span style="font-size:9px>"
Small is relative, whereas font-size:9px is absolute. We should always use relative units, allowing the browser to choose their own absolute scale. <span style="font-size:small"> (or preferably <span style="font-size:smaller">) would be the equivalent.
Is <div/> different from <span style="display:block" /> in any way?
They render just fine the same. Any semantic difference between the two?
Yes they are different.
Even though you style a span with display: block you still can't put block-level elements inside it:
<div><p>correct</p></div>
<span style="display: block;"><p>wrong</p></span>
The (X)HTML still has to obey the (X)HTML DTD (whichever one you use), no matter how the CSS alters things.
Here's an example where it makes a real difference (for valid code, at least):
<a href='example.com'>
<span class='title' style='display:block;'>The title of the image is also a link</span>
<img src="example.com/someimage.jpg"/>
</a>
That allows you to make your span a block level element and allows the image and span to highlight together when moused over.
A div would not be valid nested inside an a tag.
A <div> is a block level element that has no specific semantics of its own, beyond defining a discrete block of content. A <span> is an inline element that has no specific semantics of its own, beyond defining a discrete segment of inline content.
You can use CSS to make a span display as a block, but there is absolutely no reason to do so EDIT: other than for purely visual effects, as Gabriel demonstrates; what I mean is that you shouldn't use CSS to try to coerce a span into having block-level significance in terms of document structure. Furthermore, if you do, your content will probably appear meaningless to a user without CSS, such as a blind user, or a search engine.
If it's a block, use a div. If it's part of inline content, use a span. Remember, CSS is about presentation alone; your markup still needs to be structured in a logical manner if your content is to be usable.
See http://www.w3.org/TR/html401/struct/global.html#edef-DIV for the details.
Yes. They can contain different things and are allowed in different places.
They will also be rendered differently in environments where CSS is not available (e.g. in some email systems)
For some time I've been making websites, but have never really seen discussion of the proper usage of the container tags. I've seen any number of different types of content in the collection tags, but it usually seems that the page creator just picks a style they like and sticks with it.
The main discrepancy in my mind is that between
<p>
<div>
but I'd also like opinions regarding
<span>
and any others I may be forgetting.
HTML was originally created to put the content of documents into some sort of structure understandable to computers. With that in mind, the p tag is supposed to hold anything that would be structured as a paragraph if the content of the page were to be turned into a printed document. The div and span elements are reserved as general-use containers to facilitate formating and grouping of related elements to provide additional levels of structure, perhaps correlating to pages in a text document.
In some cases, p tags should contain other elements, such as anchor (a), image (img) and other in-line elements, because they relate directly to the content of the rest of the paragraph and it makes sense to group them that way, or the text of the rest of the paragraph provides a more in-depth description.
If there is not additional description of those elements, however, it does not make sense to place them in a paragraph simply as a convenient container; a div would be more appropriate. In general, a paragraph is supposed to contain one paragraph of text and any directly related or described elements. Nothing else makes much sense in a paragraph.
UPDATE: HTML5 also adds a number of other semantic "container" elements, including article, nav, header, section, and aside.
I think, the meaning of the tags is something like this:
<p>Paragraph, usually just text</p>
<div>A block, containing anything</div>
<span>Just a simple non-blocking wrapper</span>
The difference between these three (and many other) tags is their semantic meaning. The HTML standard includes both tags with specific semantic meanings (<p> for paragraphs, <em> for emphasized text, etc.) and tags without semantic meaning.
The latter are <div> and <span>, which are used to identify block- or inline-level content which needs to be identified (using, say a class= or id= attribute), but for which a semantically-specific tag does not exist. For example, one may write <p>Hi, my name is <span class="name">John Doe</span>.</p> — indicating that it's a paragraph (which the browser already has an idea how to handle) and that part of it's content is a name (which means absolutely nothing to the browser unless CSS or JavaScript uses it).
These tags are therefore incredibly useful both in adding additional information to an HTML document which doesn't fit within the semantic tags supplied by the standard (see the hCard specification for an excellent example) and for applying visual (CSS) or functional (JavaScript) structure to a document without altering its semantics.
I think page creators should use semantic markup, meaning that the markup they create should communicate meaning (and not presentation). <div> and <p> have different meanings. The former is used to define a division (or section) of an HTML page, the latter to define a paragraph of text.
<p> is a block-level element that should contain a paragraph, comprised of text, inline elements that modify that text (<p>, <a>, <abbr>, etc.), and images.
<div> is a block-level element used to divide the page, almost always in conjunction with CSS styles.
<span>... well, I honestly don't use this tag that often. It's an inline element, and I use it usually when I'd like to apply styles to a portion of text that wouldn't benefit from using something with more meaning, like the <strong> and <em> tags.
I was tought to view <span> and <div> as the "tofu of webdeveloppement", since it has no real flavor but you can do virtually anything with it.
(X)HTML tags define what the text they're surrounding is. Is it and address, is it a link, is it a paragraph, and so on...
<div> and <span> are simply ways of getting to pieces of your site you normally can't get to. Like when you're trying to resize a | symbol. Fastest way I've ever found was to put a span around it, give it a class and then implement the CSS.
That's what they're good for, in my opinion. I'd be interested to hear more or even corrections on what I've written here.
It sounds like you need to read the HTML specification
The p element:
The p element represents a paragraph.
The div element:
The div element has no special meaning at all. It represents its children. It can be used with the class, lang, and title attributes to mark up semantics common to a group of consecutive elements.
The span element:
The span element doesn't mean anything on its own, but can be useful when used together with the global attributes, e.g. class, lang, or dir. It represents its children.
The major difference between div and span is that span is flow content, phrasing content, and palpable content, while a div is only flow content and palpable content.
Essentially this boils down to:
div elements are block-level elements, and typically may only be placed within other block-level elements, whereas span elements are inline elements, and may be placed within most other elements.
The HTML spec defines which elements are acceptable as descendents of each element.