Semantic HTML Practice

懵懂的女人 提交于 2019-12-04 07:07:45
David Storey

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 */
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!