Labeling correctly
It means that you're calling something what it actually is. The classic example is that if something is a table
, it should contain rows and columns of data. To use that for layout is semantically incorrect - you're saying "this is a table" when it's not.
Another example: a list (<ul>
or <ol>
) should generally be used to group similar items (<li>
). You could use a div
for the group and a <span>
for each item, and style each span
to be on a separate line with a bullet point, and it might look the way you want. But "this is a list" conveys more information.
Fits the ideal behind HTML
HTML stands for "HyperText Markup Language"; its purpose is to mark up, or label, your content. The more accurately you mark it up, the better. New elements are being introduced in HTML5 to more accurately label common web page parts, such as headers and footers.
Makes it more useful
All of this semantic labeling helps machines parse your content, which helps users. For instance:
- Knowing what your elements are lets browsers use sensible defaults for how they should look and behave. This means you have less customization work to do and are more likely to get consistent results in different browsers.
- Browsers can correctly apply your CSS (Cascading Style Sheets), describing how each type of content should look. You can offer alternative styles, or users can use their own; as long as you've labeled your elements semantically, rules like "I want headlines to be huge" will be usable.
- Screen readers for the blind can help them fill out a form more easily if the logical sections are broken into
fieldsets
with one legend
for each one. A blind user can hear the legend
text and decide, "oh, I can skip this section," just as a sighted user might do by reading it.
- Mobile phones can switch to a numeric keyboard when they see a form input of
type="tel"
(for telephone numbers).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…