Classes and IDs
We saw how to select HTML tags with CSS, but, most of the time, you'll have multiple identical HTML tags, such as <p>
or <a>
. How do we differentiate them so we can only select and style a specific one? Here come the classes and IDs. They're used to select a specific HTML tag you have put an attributeid
or class
, for example:
<div id="header"></div> <p class="big"></p>
To select this ID header
in CSS, we'll need to write a hash (#
) character, followed by the ID of the element, in this case, header
:
#header { margin-left: 10px; }
To select a class, we'll need to write a period (.
) character, followed by the name of the class:
.big { font-size:20px; }
So what is the difference between IDs and classes? The only difference is that IDs can be used only once in an HTML document, while Classes can be used multiple times. We also need to know the following:
For IDs:
- Each element can have only one ID
- Each page can have only one element with that ID
For...