CSS: Selectors

By using selectors, you can apply certain rules to one or several elements.

Element Selectors

Element selectors refers to HTML tags such as h1, p, body etc. The selector would be, for example, the h1 tag. After adding the selector, you'd add one or several declarations, such as font: 15px arial; color: blue;

In order to make all text in the body green and center it, you would use the following code:

Class Selectors

Class selectors are useful when you want to style specific parts of a document, rather than applying them to all elements like the element selectors. You can use the same class an unlimited amount of times in a document. An advice is to use class names that makes sense. A red border might turn blue in your next layout change, so it'd be more appropriate to simply name it "border".

For the usage, imagine you have a p tag that needs to have a font size of 13px, using Arial and be in bold. If you'd use an element selector, you'd make all paragraphs 13px, using Arial and be in bold. So this is why classes are useful.

To apply a class to, for example, a p tag, use the following code:

The code in your CSS would then be as follows:

If you'd like all elements to be able to use the class, simply rename the code to .bold {

Id Selectors

Id's are slightly different from classes. They're unique, and can only be used once in a document.

To apply an id selector to a div tag, the following code would be used:

The code in your CSS would then be as follows:

If you'd like all elements to be able to use the id, simply remake the code into #border {

Descendant Selectors

Descendant selectors are very useful as they can style an element that is located inside of another element. Imagine the following situation: You have an unordered list, and you want all li elements to be styled in a certain way. You could apply a class to each li element, but that would be bothersome. It's way easier to take use of the descendant selectors.

In this case, ul is the ancestor, and li is the descendant. So you'd specify the code like this:

If the ul has a certain class specified to it, you would put the code as ul.tutorial li {

Children Selectors

Children selectors are very specific and allows the styling of an element's child. For example, you might want to style the first strong inside a p tag, but you want the second strong tag to remain untouched. In order to do so, the following code would be used:

How to Group Selectors

It's possible to group selectors. For example, if you'd like all your p, h2 and h3 tags to have a blue font, and rather than individually make a class for each, you could group them:

You could also group several selectors into one tag in your HTML. Notice each selector needs to be seperated by a space.

Comments

Linda | 26 Feb 2009, 19:24

Well, grouping is for elemtents/classes that have the same properties. If you want different fonts for each, you just specify it for each element/class like usual. Like so...

h2, h3 { color: blue; }
h2 { font-family: tahoma; }
h3 { font-family: verdana; }

Numra | 26 Feb 2009, 16:05

So, if I group selectors like h2 and h3 to say they turn blue. Then have another line that says h2 is also tahoma font. Then I'd have another line that says h3 is verdana. So they're the same color but different fonts. Would that work or would I need two different lines for each?

Add Your Comment



Notify me about new comments
Hide my email