CSS Tips - Class or ID

CSS – Class or ID?

It’s an old one but we still get asked –

People are often confused by class and id in CSS. The simple rule is id is for a CSS rule used only once and class is used for a css rule that is required more than once.

(Remember in CSS a class statement always starts with . and id starts with #)

ID

Use an id for elements that are used once and only once on a page

The HTML:

1 <div id=”content”></div>

The CSS:

1 #content {background: #fff}

Class

Use a class for elements that may be reused on the page

The HTML:

1 <p class=”large-text”>
2 </p>

The CSS:

1 .large-text {font-size: 1.4em}
1 reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *


7 − six =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>