The paragraph above will be styled by the class “center” AND the class “bold”.
You can also omit the tag name in the selector to define a style that will be used by all HTML elements that have a certain class. In the example below, all HTML elements with class=”center” will be center-aligned:
| .center {text-align:center} |
In the code below both the h1 element and the p element have class=”center”. This means that both elements will follow the rules in the “.center” selector:
| <h1 class=”center”>This heading will be center-aligned</h1> <p class=”center”>This paragraph will also be center-aligned.</p> |
Do NOT start a class name with a number! This is only supported in Internet Explorer.
Add Styles to Elements with Particular Attributes
You can also apply styles to HTML elements with particular attributes.
The style rule below will match all input elements that have a type attribute with a value of “text”:
| input[type=”text”] {background-color:blue} |
The id Selector
You can also define styles for HTML elements with the id selector. The id selector is defined as a #.
The style rule below will match the element that has an id attribute with a value of “green”:
| #green {color:green} |
The style rule below will match the p element that has an id with a value of “para1”:
| p#para1 { text-align:center; color:red } |
Do NOT start an ID name with a number! It will not work in Mozilla/Firefox.
CSS Comments
Comments are used to explain your code, and may help you when you edit the source code at a later date. A comment will be ignored by browsers. A CSS comment begins with “/*”, and ends with “*/”, like this:
| /*This is a comment*/ p { text-align:center; /*This is another comment*/ color:black; font-family:arial } |










