32

I have seen it a lot in css talk. What does semantically correct mean?

Landon Kuhn
  • 76,451
  • 45
  • 104
  • 130
johnny
  • 19,272
  • 52
  • 157
  • 259
  • 4
    @johnny: have you tried searching for an answer to your question before posting it? Not that it is a bad question, but it's one about which people have written a ton. – Mike Dinescu Aug 18 '09 at 15:16
  • 8
    Yes, but honestly, and no offense intended, but I don't see the harm in these type questions, nor why some people are offended when they are asked. If I owed a website, I wouldn't mind it because it just creates more pages that can be indexed. I see it as helping the website. But, I did look and didn't see a simple answer. Again, no offense is intended. I've just never understood the complaints. – johnny Aug 18 '09 at 16:10
  • 3
    Actually johnny is correct. As long as the questions is original to *stackoverflow* then it is a legitimate question. http://meta.stackexchange.com/questions/2686/how-should-you-respond-to-give-me-a-fish-rtfm-questions – TJ L Sep 04 '09 at 20:40

6 Answers6

52

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).
Community
  • 1
  • 1
Nathan Long
  • 122,748
  • 97
  • 336
  • 451
22

Semantics basically means "The study of meaning".

Usually when people are talking about code being semantically correct, they're referring to the code that accurately describes something.

In (x)HTML, there are certain tags that give meaning to the content they contain. For example:

An H1 tag describes the data it contains as a level-1 heading. An H2 tag describes the data it contains as a level-2 heading. The implied meaning behind this is that each H2 under an H1 is in some way related (i.e. heading and subheading).

When you code in a semantic way, you basically give meaning to the data you're describing.

Consider the following 2 samples of semantic VS non-semantic:

<h1>Heading</h1>
<h2>Subheading</h2>

VS a non-semantic equivalent:

<p><strong>Heading</strong></p>
<p><em>Subheading</em></p>

Sometimes you might hear people in a debate saying "You're just talking semantics now" and this usually refers to the act of saying the same meaning as the other person but using different words.

Community
  • 1
  • 1
Jamie Dixon
  • 53,019
  • 19
  • 125
  • 162
11

"Semantically correct usage of elements means that you use them for what they are meant to be used for. It means that you use tables for tabular data but not for layout, it means that you use lists for listing things, strong and em for giving text an emphasis, and the like."

From: http://www.codingforums.com/archive/index.php/t-53165.html

cakeforcerberus
  • 4,657
  • 6
  • 32
  • 42
4

HTML elements have meaning. "Semantically correct" means that your elements mean what they are supposed to.

For instance, you definition lists are represented by <dl> lists in code, your abbreviations are <abbr>s etc.

Kenan Banks
  • 207,056
  • 34
  • 155
  • 173
1

It means that HTML elements are used in the right context (not like tables are used for design purposes), CSS classes are named in a human-understandable way and the document itself has a structure that can be processed by non-browser clients like screen-readers, automatic parsers trying to extract the information and its structure from the document etc.

For example, you use lists to build up menus. This way a screen reader for disabled people will know these list items are parts of the same menu level, so it will read them in sequence for a person to make choice.

0

I've never heard it in a purely CSS context, but when talking about CSS and HTML, it means using the proper tags (for example, avoiding the use of the table tag for non-tabular data), providing proper values for the class and id that identify what the contained data is (and using microformats as appropriate), and so on.

It's all about making sure that your data can be understood by humans (everything is displayed properly) and computers (everything is properly identified and marked up).

Thomas Owens
  • 114,398
  • 98
  • 311
  • 431
  • It also exists in pure CSS context, though. Consider the different image replacement hacks (e.g. FIR) which set `display: none` on the alternative text inside a `span`. This is semantically wrong and has the consequence that a screen reader software, when seeing the CSS, skips the text even though it *should* have been read (after all, the alternative text is there precisely for the benefit of the blind users). – Konrad Rudolph Aug 20 '09 at 14:55