Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

The Cascade

Two rules can set the same property for the same element. The browser then has to decide which declaration to use. This decision process is called the cascade, and CSS is named after it: Cascading Style Sheets.

The cascade takes several things into account. The simplest one is the order: when two rules have the same selector, the one written later in the stylesheet wins.

p {
	color: green;
}

p {
	color: purple;
}

The paragraphs will be purple, because the second rule comes later. Declarations that don’t conflict all apply: if the first rule also contained text-align: center, the paragraphs would be purple and centered.

When one of your rules “doesn’t work”, first check whether a rule written later overrides it.

Instructions

  1. 1

    At the very beginning of style.css, above all the other rules, create a rule for h2 that sets color: darkolivegreen. Click Run: the headings stay sienna! The rule for h2 further down sets color too, and because it comes later, it wins.

  2. 2

    Fix it so that the headings are dark olive green: delete the original rule for h2 with color: sienna further down in the file.

Back

14/17

  1. 1. Rules and Comments
  2. 2. Quiz: Rules
  3. 3. Universal Selector
  4. 4. Type Selector
  5. 5. Class Selector
  6. 6. ID Selector
  7. 7. Quiz: Class or ID?
  8. 8. Attribute Selector
  9. 9. Pseudo-classes
  10. 10. Grouping Selectors
  11. 11. Chaining Selectors
  12. 12. Selecting Descendants
  13. 13. Selecting Children
  14. 14. The Cascade
  15. 15. Specificity
  16. 16. Inheritance
  17. 17. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.