CSS and id

We have seen how to use an element identified by its id attribute with JavaScript, we know how to use the elements identified by the class attribute with CSS. It shouldn't be strange to think that we can use the id as selector in CSS.

The reason to use id and not class should be self-evident: we have just one single element in our HTML page that we want to comply with a very specific style. For instance, we could use it for the page footer:
<p id="footer">Please steal this page, it isn't copyrighted in any way</p>
In the CSS we write:
#footer { color: red; }
We can be even more specific, and say that the style is defined for the footer attribute on a paragraph element:
p#footer { color: red; }

A fun book for HTML beginners: Head First HTML.

No comments:

Post a Comment