CSS Classes
In the previous section we talked about how CSS selectors let the browser know what part of the file to apply your styles to. In that example we just used simple tags from html as the selectors (for example, p
, a
, and h1
). However, you can actually do a lot more with selectors using ID
s and Classes
.
IDs
Sometimes you want to format one particular part of a web page, but not the whole thing. For example, you might have a certain paragraph of text that you want to be red, but you want the rest of the paragraphs to be black. You can use an ID
to solve this problem. First, you just add the id
attribute the paragraph you want to single out:
Next, you can create an ID selector
in your css file by using the #
symbol followed by the id name. This lets the browser know you want to apply a style to only the item with the matching id attribute.
Classes
Classes
work almost the same way as IDs, with one main difference: You can re-use classes in multiple tags. This is great for times when you want to format some tags one way, but others a different way. Instead of using the id
html attribute, you use the class
attribute:
Then you can use the .
character followed by the class name in your css file:
One neat thing about classes is that you can have multiple classes per tag. For example, you could say something like this:
Combining Multiple Selectors
If you want to get really fancy, you can save yourself some time by using multiple selectors at once. For example, what if you had a CSS file like this:
You could just combine the selectors and rewrite this as:
There's actually many ways to combine CSS selectors, but it's not too important for now. If you're interested in knowing more, take a look at the links below.
Recommended Reading
Last updated