Start learning with our library of video tutorials taught by experts. Get started

CSS Web Site Design

Specificity


From:

CSS Web Site Design

with Eric Meyer

Video: Specificity

In this video we're going to talk about specificity, which in a lot of ways is the heart of conflict resolution in CSS, and the easiest way to sort of approach the subject is to say, suppose that I have two rules that absolutely conflict, I said I want my list items, my li elements to have a background of red, but then, I also said that I want the level background of green and I can't have both, I mean it would be cool if CSS would just sort of strike them together, candy cane style, but it doesn't. The list items have to have either a red background or a green background.
Expand all | Collapse all
  1. 14m 34s
    1. Welcome
      28s
    2. What is CSS?
      5m 34s
    3. Design tour
      2m 38s
    4. Using the exercise files
      1m 20s
    5. Installing the Web Developer toolbar
      4m 34s
  2. 25m 56s
    1. XHTML essentials
      3m 55s
    2. CSS essentials
      5m 17s
    3. Embedded style sheets
      2m 20s
    4. Linking a style sheet
      2m 19s
    5. Linking to multiple style sheets
      2m 20s
    6. Using linked and embedded style sheets together
      4m 21s
    7. Using imported style sheets
      5m 24s
  3. 57m 48s
    1. ID selector essentials
      6m 38s
    2. Class selector essentials
      4m 9s
    3. Best practices for classes
      4m 52s
    4. Grouped selection
      4m 2s
    5. Descendant selectors
      6m 44s
    6. The sources of style
      6m 38s
    7. Specificity
      8m 21s
    8. Making things important
      4m 32s
    9. Inheritance essentials
      5m 12s
    10. Making things really unstyled
      4m 2s
    11. User style sheets
      2m 38s
  4. 39m 3s
    1. Box model essentials
      7m 35s
    2. Simple floating
      5m 3s
    3. Using float for layout
      5m 5s
    4. Fixing column drop
      5m 35s
    5. Clearing essentials
      4m 20s
    6. Float containment
      6m 35s
    7. Creating a navbar from a list
      4m 50s
  5. 38m 3s
    1. Coloring text
      4m 13s
    2. Defining color in CSS
      8m 12s
    3. Coloring backgrounds
      6m 35s
    4. Applying background images
      4m 19s
    5. Manipulating the direction of background images
      2m 52s
    6. Positioning backgrounds
      7m 23s
    7. Background shorthand
      4m 29s
  6. 58m 28s
    1. Altering line height
      7m 32s
    2. Font style and weight
      5m 45s
    3. Sizing fonts
      9m 59s
    4. Using font families
      10m 38s
    5. Font shorthand
      6m 5s
    6. Justifying text
      4m 56s
    7. Vertically aligning text
      4m 22s
    8. Transforming text
      3m 49s
    9. Text decoration
      5m 22s
  7. 44m 40s
    1. Margin essentials
      14m 21s
    2. Adding borders
      6m 52s
    3. Padding
      9m 17s
    4. Using negative margins
      7m 19s
    5. Margin collapsing
      6m 51s
  8. 20m 38s
    1. Styling tables and captions
      5m 23s
    2. Styling table cells
      6m 30s
    3. Styling a column with classes
      4m 51s
    4. Styling links inside table cells
      3m 54s
  9. 30m 40s
    1. Styling for specific mediums
      4m 3s
    2. Creating a print style sheet
      6m 35s
    3. Hiding layout for print
      4m 11s
    4. Styling for print
      6m 34s
    5. Complex styling for print
      4m 37s
    6. Creating a footer
      4m 40s
  10. 37m 23s
    1. Getting started
      1m 11s
    2. Setting global styles
      4m 4s
    3. Defining masthead and navbar colors
      3m 27s
    4. Layout of the navlink bar
      3m 38s
    5. Using columns
      4m 49s
    6. Setting content styles
      1m 53s
    7. Creating the sidebar boxes
      5m 42s
    8. Creating the sidebar form
      3m 23s
    9. Completing the sidebar
      3m 29s
    10. Making a table
      3m 12s
    11. Creating a footer
      2m 35s
  11. 1m 28s
    1. Goodbye
      1m 28s

Watch this entire course now—plus get access to every course in the library. Each course includes high-quality videos taught by expert instructors.

Become a member
Please wait...
CSS Web Site Design
6h 8m Intermediate Sep 12, 2006

Viewers: in countries Watching now:

CSS gives Web designers control over the appearance of their web sites by separating the visual presentation from the content. It lets them easily make minor changes to a site or perform a complete overhaul of the design. In CSS Web Site Design, instructor and leading industry expert Eric Meyer reviews the essentials of CSS, including selectors, the cascade, and inheritance. The training also covers how to build effective navigation, how to lay out pages, and how to work with typography, colors, backgrounds, and white space. Using a project-based approach, Eric walks through the process of creating a Web page, while teaching the essentials of CSS along the way. By the end of the training, viewers will have the tools to master professional site design. Exercise files accompany the training videos.

Subjects:
Web Web Design
Software:
CSS
Author:
Eric Meyer

Specificity

In this video we're going to talk about specificity, which in a lot of ways is the heart of conflict resolution in CSS, and the easiest way to sort of approach the subject is to say, suppose that I have two rules that absolutely conflict, I said I want my list items, my li elements to have a background of red, but then, I also said that I want the level background of green and I can't have both, I mean it would be cool if CSS would just sort of strike them together, candy cane style, but it doesn't. The list items have to have either a red background or a green background.

Which one is it going to be, the answer as it turns out is green. But the question asked is why, you might say to yourself, well, because green came later, but suppose that I said this, I said body li background red, but then I said li background green. The green came second, but the red looks like it might be, I don't know, a little more specific perhaps. So which one do you think it is, red or green. As it turns out, when we hit Reload it's red.

The reason for this is specificity, which is one of those kind of dried topics that you really have to understand very close in order to be able to write CSS really well. So let's use these as an example. For this list item, role the li background green, there's one element descriptor, li and so the specificity of that is 0,0,0,1. The one being that there's one element descriptor here. Now the other zeros, we'll get to in a moment, but just accept that they're there. The specificity of the body li is 0,0,0,2 because there's two element descriptors, there's body and there's li.

So, when you have a case where there are two declarations that conflict, we have background red, background green, we have conflicting declarations applying to the same elements which are list items in this document, then the declaration whose associated selector has a higher specificity will win out. The other really doesn't matter, if the specificity isn't equal. All right, so, because background red, the associate selector has a higher specificity 0,0,0,2 then, the selectors specificity associated with background green, red wins out. Now, suppose we put in a class to one of these, like we might say, li.first background yellow, then, when we go over, we discover that the list item with the class of first is yellow, not because it's last, but because it has a higher specificity. Here we have one element descriptor and we have one class descriptor, li for the element and .first for the class, so the specificity here is 0,0,1 for the class and 1 for the element, so 0,0,1,1 is a higher specificity then 0,0,0,2, which is, will only have appear for body li, so li.first wins out over body li. Now, the reason that, only that one list item ends up with the yellow background, is because it's the only one that has a class of first.

If we can group these together, then both of those will win out because li.first 0,0,1,1 and li.last 0,0,1,1, these don't all get counted up together, because this is a group selector here, this one gets counted up and this one gets counted up, separate from each other, exactly the same as if I had written these as two different rules, remember that how group selectors work. Now, we can start to take this, you know, even further. I can say html body li.first, which would give this one a specificity of 0,0,1,3 because there's one class descriptor and three element descriptors HTML, body and li. That's not really going to have an impact in the presentation here, but it does point out how these things sort of act together.

Let's do an ID, navlinks li, now you list item inside an element with an ID of navlinks, should have a background of fuchsia. So the specificity for this will be 0,1 for the ID, 0,1 for the element li. So 0,1 navlinks, 0 cause there are no classes and 1 for the element, the li and if we hit Reload, now they are all fuchsia, because the, the specificity of this selector outweighs any of these others.

Now, of course is important to remember that you only have to worry about the specificity conflict if you have conflicting declarations. We've been, you know, fighting with background right now, but suppose right here the lowliest of these selectors, just plain, old li, I set font style italic and the declaration font-style italic is associated with a selector, li has a specificity of 0,0,0,1, but it's still going to work, it's still going to come through, because there is no conflicting font style declaration in any of the rules whose selectors have hire specificities. So, the specificity resolution only takes in when there are conflicts.

You might have been wondering this whole time I've been muttering numbers at you, I've always started with a zero, and you might have caused to wonder why that is, and that's because there's one more way to cause specificity to happen and that's the in-line style attribute. The specificity of the style declaration here is, the style attribute value is 1,0,0,0.

That's higher than 0,1,0,1, which is the specificity of the navlinks li rule. So when we go back and we hit Reload, the element, which has the in-line style attribute, will win out over basically anything that we put into our author style sheet, into our own style sheet, because it's specificity 1,0,0,0 can never be outweighed by any rule up here, because rules that we put in our own style sheet will always start with zero and this one will always start with one. So, this is a way to override anything you might've written in a style sheet although it's not a recommended way to style at all because you're sticking style information into the document source and at that point, you may as well just go back to font elements and spacer divs. So, you can override with the highest specificity styled attribute, but it is just not recommended, so I'm going to take it back out.

So let's go back to sort of our original case, which is, we have two rules and this sort of have the same specificity, and there's a conflict. So here, we're going to see yellow backed, italic text. And if we reverse these, background yellow and then background green, we're going to see green backed, italic text.

That's specificity in a nutshell, and it's not the most immediately intuitive of concepts, but as I say it is very important to understand in order to write more efficient CSS and also to figure out why, on occasion, maybe you've added a rule, and nothing happens. Sometimes that happens, you want to change something and you go back to your browser and hit Reload and there's no change. Whatever was you just typed in, had no apparent effect, and there's a way to figure out whether or not that's a specificity conflict, which we'll discuss in the next chapter.

There are currently no FAQs about CSS Web Site Design.

Share a link to this course
Please wait... Please wait...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.
Upgrade now


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Upgrade now

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed CSS Web Site Design.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Welcome to the redesigned course page.

We’ve moved some things around, and now you can



Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked