开发者

Semantic HTML Practice

I read about semantic HTML online...

Semantic HTML means using HTML tags for their implied meaning, rather than just using (meaningless) div and span tags for absolutely everything.

If you use <h1> instead of <div class="header">, and <h2> instead of , et cetera, Google and other search engines will interpret your header开发者_开发技巧s as being important titles in your page. This way, when people search on the words in your headers and sub-headers, your page will be considered more relevant (and rank higher). Plus, it's much shorter and cleaner.

So, below is semantic,

<h1>My Website Name</h1>
<h2>My Website Tagline </h2>

What about this below?

<div id="header">
  <h1><span class="hide">My Website Name</span></h1>
  <h2><span class="hide">My Website Tagline</span></h2>
</div>

I tend to combine h tags with div and span tags like above - is this practised considered as the lack of semantic?

The reason why I have the span with the hide class is that I want to display the site logo instead of text. So use CSS to set the background of h1 as image and then hide the text. is this incorrect practise?

Then, if I don't use div, what can I use to make a box around the h1 and h2?

As far as I know, html 5 is not fully ready yet, we must not use <header> yet, must we??

Thanks.


I would do something like the following if I was going to use HTML5:

<header>
  <hgroup>
    <h1>My Website Name</h1>
    <h2>My Website Tagline</h2>
  </hgroup>
</header>

Remember to add display: block; to the HTML5 elements and createElement for IE in the CSS though. The header element shows the block is a header and the hgroup element is there to show that the second h* element is a sub heading, so shouldn't be taken into account when calculating the header levels in the document.

If you don't want to use HTML5 yet then you could use divs instead of the new elements, and use the HTML5 element names as the class value. This will make it easier to switch over when you feel comfortable using HMTL5 on a live site.

You don't really need to use the span elements. You can use tricks such as using a large negative text-indent in the CSS to hide the text off the screen.


If you want to display a logo instead of text, use an image. Google say so (even if they don't know the difference between a tag and an attribute). Taglines, BTW, are not subheadings (and the site name (and thus logo) is usually only a heading on the homepage).

<div id="header">
  <h1><img src="foo.png" alt="My Website Name"></h1>
  <p><img src="foo.png" alt="My Website Tagline"></p>
</div>


Unfortunately, Internet Explorer 8 does not recognize many HTML5 tags, and when I've tested it, I was unable to set CSS values for the <header> tag, for example. So for now I would recommend that you continue to use div tags to group your semantic meaning.

As a sidenote, Google does not like hidden text, and if you have a lot of it, it will consider it deceptive coding. One is probably fine, but you'd be better off using the alt attribute on the image tag.


Nobody suggested that you should not use DIVs at all... semantic HTML does not mean there cannot be div or span tags in your code. It just only means that whenever possible (there is a specific tag available for a specific semantic meaning) you should try to give semantic meaning.

h2 is not to be used for taglines, as somebody else already suggested.

Also, in my interpretation (some will argue), h1 is not for the name of your website. It is the title for the content on a specific page.


I agree with @David Dorward, the tag line should be in a p tag.

Your example (wrapping the header elements with a div) is perfectly acceptable, though I would like to raise a small caution: Be careful that you do not get in the habit of wrapping everything in div tags. For example:

<div class="content">
    <div class="list">
        <ul>
            <li>something</li>
            <li>something</li>
            <li>something</li>
        </ul>
    </div>
</div>

Since a ul tag is already a block element, the above markup would be better off like this:

<div class="content">
    <ul class="list">
        <li>something</li>
        <li>something</li>
        <li>something</li>
    </ul>
</div>

And then just style the ul to look like the div.

On the matter of displaying the logo as an image:

If your logo is text-based, or has text in it, you would be better off doing the following:

HTML

<div id="header">
    <h1 class="logo">My Logo Text - My Website Tagline</h1>
</div>

CSS

.logo { text-indent:-9999px;background-image:url(thelogo.jpg) no-repeat;}
      /* Also add height and width based on your logo height and width */
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜