/* A rule with many properties */
h1 {
font-family: 'Helvetica';
color: white;
background-color: #333; /*dark gray*/
}
An element selector (or tag selector) selects all elements with a specific tag:
/* applies to all `class="highlighted"` elements */
.highlighted {
background-color: gold;
}
/* applies to all <p> elements */
p {
font-color: purple;
}
A class selector selects all elements that have a class (among others). Written by putting a . in front of the class name.
Have a rule apply to only a single element by specifying that element's id attribute and then using that class as the selector in the CSS
/* CSS */
#sidebar {
background-color: grey;
}
<!-- HTML -->
<div id="sidebar">This div has a grey background!</div>
# specifies id name, not tag/class name
You can combine any "basic" selectors by writing them next to each other (without any spaces). The rule will apply to any element that meets all the criteria.
/* selects elements that both are <p>
* AND have class="alert" */
p.alert { }
/* selects elements that both are <p> AND
* have class="alert" AND have class="success" */
p.alert.success { }
/* selects elements that both are have class="book"
* AND have class="large" AMD have class="favorite" */
.book.large.favorite { }
As a shortcut, you can use the group selector to write a single rule whose properties apply to multiple selectors. A group selector has a comma between selector parts.
/* Applies to <h1> elements. Also applies to <h2> elements.
* Also applies to <h3> elements. */
h1, h2, h3 {
color: purple;
}
/* The above rule is identical to these 3 rules,
* just written together (to avoid duplication) */
h1 { color: purple; }
h2 { color: purple; }
h3 { color: purple; }
As a shortcut, you can use the group selector to write a single rule whose properties apply to multiple selectors. A group selector has a comma between selector parts.
/* You can group together combined selectors as well!*/
/* This rule applies to both `.green` elements
* AND to `p.alert.success` elements */
.green, p.alert.success {
color: green;
}
The descendant selector selects elements that are "inside" (children of) another specified element. A descendant selector has a blank space between selector parts.
/* Applies to <a> elements that are "inside" a <nav> */
nav a { ... }
/* Applies to class="alert" elements that are
* "inside" a <div>
* This is NOT <div class="alert">! */
div .alert { ... }
/* Applies to <a> that are inside a <li>
* that themselves are inside a <ul> */
ul li a { ... }
The descendant selector selects an element that is "descended" from another, even if there are other elements
in-between. (It selects grandkids and great-grandkids too!)
/* This will select the above <a> even though it is
* inside an <li>, because it has <nav> as an ancestor */
nav a {
color: purple;
}
<nav>
<ul>
<li>
<a href="https://ischool.uw.edu">iSchool</a>
</li>
</ul>
</nav>
There are a number of other selectors as well, but they aren't as common. For example:
/* Child Selector: like descendant, but only immediate kids
* Applies to <a> elements that are *direct children* of a <nav> */
nav > a { }
/* Adjacent Sibling Selector: selects elements that are siblings
* Applies to <p> that are immediately after an <img>.
* (does not apply to the <img>) */
img + p { }
/* Attribute Selector: selects elements with certain attribute
* There are lots of further variants of this.
* Applies to <a href="https://example.org"> elements */
a[href="https://example.org"] { }