Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
So far, we have used all three basic selector types: Element, Class and ID. Now, while those three will serve as the building blocks for almost every style you write in your CSS, their application will be incomplete without also understanding how to write descendant selectors. I do not exaggerate when I say that over 90% of the styles in your site will be descendant selectors. Descendant selectors target rules based on where an element is found within another element. To target every span tag inside of a paragraph tag, you would only need to write the selector p span, for example.
Descendant selectors are typically used in conjunction with ID selectors. This allows you to style all paragraphs in the sidebar, so that they look different than the main content. Let's create a couple of descendent selectors to finish styling our sample page. So here we have the descendent_selector.htm file. Now, just to kind of refresh your memory about the structure of the page, remember we have a div tag up top with an ID of top. Then we have a div tag down at the bottom of the page with an ID of bottom. In between those, we have two paragraphs, one that has a class attribute of greenText and one that is just a normal paragraph.
Well, let's say we want to target the paragraphs just in this top region; style these guys separately without affecting any other content. If we didn't know about descendant selectors, we would probably be trying to use a class selector to do that. So this is one of those instances where I want to point out how much more efficient a descendant selector is over a class selector. So we're going to do a brand-new CSS Rule, and this time we're not going to choose any of the first three options of selector type. We're going to choose the last one, which is Compound.
Now, Compound is Dreamweaver's way of basically just saying, "Whatever you want to write." Now, you will notice that my selector has already resolved itself for me. That was nice of Dreamweaver, #top p. That's exactly the descendant selector I need to write. Now, how did it know to do that? Well, because I selected this paragraph within this region. So when I selected Compound, Dreamweaver basically wrote the compound selector for me, based on that area. Now, it's not always going to get it right, so don't rely on Dreamweaver to write these selectors for you. Look at it more of a way of saving a little bit of time.
Now, if you need to write that yourself, remember just do #top, and this is the important part, space, p. That space indicates that you're moving from one element to another. So we're looking for any paragraph inside of any region with an ID of top. Again, we're going to do it in This document only, and we're going to go ahead and click OK. So for Color here, I'm going to type in #000. For Font-size, I'm going to type-in 1 em, and then for Font- style I'm going to Italicize that text.
I'm going to go ahead and click OK, and notice that my paragraphs in the top-region restyled. So unlike a class selector, I don't have to do any manual application whatsoever. I'm just telling my browser, "Look inside any region with an ID of top. If you find a paragraph inside that, style it like this." So we're targeting very specific areas. Now, let's tackle the heading 1 in our bottom region. Now, if we looked at this on a page again, and we said, "Hey. We want this heading to look slightly different than any other heading on the page," again, a lot of people would resort to a Class Selector, but we can use the structure of the page to our advantage.
We notice that this is the only heading 1, and the only one inside of a region with an ID of bottom. So we're going to use that to write our descendant selector. Once again, I'm going to choose New CSS Rule. I am going to make sure it says Compound, and once again, because I placed my cursor into that element, you'll notice that Dreamweaver resolved it for me, #bottom h1. I'm going to go ahead and click OK, making sure it's being defined in this document only. For Color, I'm going to choose #FF0, which is the yellow color. For Font-size, I'm going to choose 1.6 ems.
So maybe a good bit larger there. Font-weight is going to be normal. So headings are normally bold, so by changing Font-weight to normal this particular heading will not be bold. For Line-height, I'm going to choose 1.2, and we're going to do a multiple. Now, again, later on in Chapter 07 when we do typography, we'll go into what a multiple, what an em is, those things in a little bit more detail. So I'm going to go ahead and click OK, and now I see the heading looks totally different than the one above it, and no class selectors are involved in that at all.
Now, finally, we've been using IDs for our descendant selectors, and they are commonly found within descendant selectors, but you don't have to use them. You can use any selectors you want to do a descendant selector. You can also do more than two. You can have as many elements as you want in a row, so you could say, body, space form, space table, space paragraph, and that would target any paragraph inside of a table, inside of a form, inside of the body tag. So you can go as deep into that as you want. Let's go ahead and do another New CSS Rule. Now, again, this time I'm going to choose Compound, and because I didn't have anything focused on the page, I now have to write this compound selector myself.
Now, again, don't be confused by the term Compound versus Descendant. Descendant selectors are also sometimes referred as Contextual Selectors. The word Compound here really doesn't have any meaning to CSS. In Dreamweaver, it just basically means you can do something beyond the basic three types of selectors. So for Selector Name here, I'm going to type in body, and then space, remember that space is important, and then p, and then space, and then span. Now, I want to point out something else that Dreamweaver is doing for us here.
If you're learning how to write these selectors, there is a really nice tool that Dreamweaver has given you guys right below the name of the selector. Notice that it says, This selector name will apply your rule to all elements that are within any paragraph elements, that are within any body elements. So if you write a descendant selector, you're going to get a description down below it of what you're going to be targeting. If that reads a little bit different than your intent, then perhaps your selector needs a little bit of refining them. Now, again, I'm going to go ahead and click OK. I'm going to make a very simple change to this.
I'm just going to change the Font- weight to bold, and then click OK. Again, notice the word "alone" is now bold, because it is in a tag, which is also inside the tag. Perfect! Now, this page is not going to win any design contest, but we've successfully created individual looks for each element, based on its position and type. Once you do a few descendant selectors, it's very easy to see how powerful, and how useful they are. You'll find they control an overwhelming majority of the styling in your sites.
Properly structuring, and naming elements inside your site, and consistently using those standards, makes it easy to create descendant selectors that do all the work for you, eliminating the need to rely on class selectors for everything that you do.
Get unlimited access to all courses for just $25/month.Become a member