HTML and CSS Reference
In-Depth Information
But how do I use id in CSS?
You select an element with an id almost exactly like you select an element with a
class. To quickly review: if you have a class called
specials
, there are a couple
of ways you can select elements using this class. You could select just certain
elements in the class, like this:
p.specials {
This selects only par
agraphs that are in the
specials class.
color: red;
}
Or you can select all the elements that belong to the
specials
class, like this:
.specials {
This selects all elements in the specials class.
color: red;
}
Using an id selector is very similar. To select an element by its id, you use a # (hash mark)
character in front of the id (compare this to class, where you use a . [dot] in front of the
class name). Say you want to select any element with the id
footer
:
#footer {
color: red;
This s
elects any element that
has the id “footer”.
}
Or you could select only a
<p>
element with the id
footer
, like this:
p#footer {
Th
is selects a <p> element
if it has the id “foot
er”.
color: red;
}
The other difference between class and id is that an id selector should match
only one
element in a page.