Search

Tuesday, 17 July 2012

When to use an ID or Class in CSS

A CSS class is re-usable, A CSS id must only exist once in any page.

If you want to use a style more than once on any given webpage then it must be put into a CSS class. 

  • Use an id to style a unique item on the page eg.page footer, main menu, search bar. 
  • Use a CSS Class as many times as necessary for formatting buttons, paragraphs, headings, textboxs, hyperlinks(anchors) etc.

Example of a CSS class:

.special { font-weight:bold; border:solid 1px black; }

Example of a CSS id:

#special { font-weight:bold; border:solid 1px black; }

Example use in HTML:

<span class='special'>some text</span>   OR
<span id='special'>some text</span>

1 comment: