.read,.unread { color: white; background-color: black;}.read { /* several unique declarations */}.unread { /* several unique declarations */}
Chaining selectors
We can select elements that have all of multiple classes/IDs. Chain without any separating characters
Chaining type selectors
We can’t chain more than one type selector as HTML elements can’t be multiple types
Class selector chaining
Selects any element that has both the subsection and header classes
<div> <div class="subsection header">Latest Posts</div> <p class="subsection preview">This is where a preview for a post might go.</p></div>
.subsection.header { color: red;}
Class and ID chaining
<div> <p class="subsection" id="preview"> This is where a preview for a post might go. </p></div>
.subsection#preview { color: blue;}
Combinators
Descendant combinator
Selects elements matching the last selector only if the element has an ancestor that matches the previous selector
.ancestor .child would select an element with the class child if it has an ancestor with the class ancestor
Represented by a single space between selectors
B, C will be selected. D will not be selected
<!-- index.html --><div class="ancestor"> <!-- A --> <div class="contents"> <!-- B --> <div class="contents"><!-- C --></div> </div></div><div class="contents"></div><!-- D -->
/* styles.css */.ancestor .contents { /* some declarations */}
Selector order of precedence
When two selectors have the same level of specificity, the rule that is defined last has the most precedence; that is, the last rule overrides any rules before it.