The id and class Selectors

3:01:00 PM |

The id Selector

The id selector uses the id attribute of the HTML element, and is defined with a "#".
The style rule below will be applied to the element with id="para1":

Example

#para1
{
text-align:center;
color:red;
}

* Do NOT start an ID name with a number! It will not work in Mozilla/Firefox.

The class Selector

The class selector uses the class attribute of the HTML element, and is defined with a ".".
The style rule below will be applied to the element with class="center"

.center {text-align:center;}
You can also specify that only specific HTML elements should be affected by a class.
In the example below, all p elements with class="center" will be center-aligned:

Example

p.center {text-align:center;}