Slide 4.3: CSS syntax
Slide 4.5: CSS how to ...
Home

CSS Id and Class Selectors


In addition to setting a style for an HTML element, CSS allows you to specify your own selectors called id and class.

The Id Selector
The id selector is used to specify a style for a single, unique element. The id selector uses the id attribute of the HTML element, and is defined with a ‘#’.

(Top example) The style rule will be applied to the element with id="para1".

The Class Selector
The class selector is used to specify a style for a group of elements. Unlike the id selector, the class selector is most often used on several elements. This allows you to set a particular style for any HTML elements with the same class. The class selector uses the HTML class attribute, and is defined with a ‘.’.

(Middle example) All HTML elements with class="center" will be center-aligned.

You can also specify that only specific HTML elements should be affected by a class.

(Bottom example) All p elements with class="center will be center-aligned.
 
     
     
     


Demonstration
The following demonstration shows how the script of HTML and CSS is displayed on the Web: