logo

id or class selector in css

logo

One of the advantages of CSS particularly to designersis its ability to easily apply a set of styles to all elements of the same type. Unimpressed? Consider this: by editing a single line of CSS, you can change the colors of all your headings. Don’t like the blue you’re using? Change that one line of code, and they can all be purple, yellow, maroon, or any other color you desire. That lets you, the designer, focus on design, rather than grunt work. The next time you’re in a meeting and someone wants to see headings with a different shade of green, just edit your style and refresh. The results are accomplished in seconds and there for everyone to see.

Selecting elements on the basis of their class names is a very common technique in CSS. The attribute selector syntax [class="top"] is rather awkward, but thankfully there’s a simpler and shorter form for it: the class selector.

Here’s a simple example that selects all elements with a class attribute that contains the value “top”:

An ID selector matches an element that has a specific id attribute value. Since id attributes must have unique values, an ID selector can never match more than one element in a document. The attribute selector syntax (id=”top”) must be write on html document to use it.

after declaration please put a closing ; to close it.

Deciding Between id and class

You may assign classes to any number of elements, as demonstrated earlier; the class name warning was applied to both a p and a span element, and it could have been applied to many more elements. IDs, on the other hand, are used once, and only once, within an HTML document.

logo
Designed by bali net design. Best view with firefox 3+ , Safari 3+, internet explorer 7+