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
CS Čeština
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
CS Čeština
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

Chaining Selectors

You can also join selectors without any space or comma. Then an element must match all of them at once:

div.group {
	color: purple;
}

The selector div.group selects the elements that are a div and also have the class group. A p element with the class group wouldn’t be selected, and neither would a div without the class. This is called chaining. It isn’t new to you: a[target="_blank"] and a:hover from the previous exercises are chained selectors too.

Chaining is useful for two reasons. First, it selects elements more precisely, for example only the groups that are under the mouse pointer: .group:hover. Second, a chained selector wins over each of its parts. The groups now have a linen background from the rule for .group. When you give them another background with a rule for .group:hover, it wins over .group, so the background changes on hover. You’ll find out exactly why in the exercise on specificity.

Mind the difference: div.group (no space) is not the same as div .group (with a space), which you’ll meet in the next exercise, or div, .group (with a comma), which groups the selectors.

Instructions

  1. 1

    Create a rule for .group:hover that sets a darker beige background: background-color: wheat. Then move the mouse pointer over the groups.

Back

11/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

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with: