Selectors¶
A selector tells the browser which elements a rule applies to. Good selectors keep CSS small and predictable.
Type selectors¶
A type selector targets an element name:
Use type selectors for base styles that should apply everywhere.
Class selectors¶
A class selector targets elements with a matching class attribute. The dot is part of the selector:
Classes can be reused on many elements. They are the main tool for component styles.
ID selectors¶
An id selector targets one element:
An id must be unique on the page. Avoid using ids for routine styling because they have high specificity and are hard to override later. Use them for in-page links and JavaScript hooks.
Grouping selectors¶
Apply the same declarations to several selectors with a comma:
Descendant and child selectors¶
/* any <a> inside <nav> */
nav a {
text-decoration: none;
}
/* only direct <li> children of <ul> */
ul > li {
margin-bottom: 0.25rem;
}
The space is a descendant combinator. The > is a child combinator. Use the child combinator when you only want direct children.
Attribute selectors¶
/* links that open in a new tab */
a[target="_blank"] {
text-decoration: underline;
}
/* email inputs */
input[type="email"] {
border-color: #888;
}
Attribute selectors are useful when the attribute itself carries meaning you want to style.
Choosing a selector¶
- Use a class for a reusable visual style.
- Use a type selector for base element styles.
- Use an id for a unique anchor or script hook, not for routine styling.
- Keep selectors short.
body main article ul li ais a sign that the link needs a class.
Pseudo-classes such as :hover and pseudo-elements such as ::before have their own chapters.
Try it¶
Add three links to a page: one normal link, one with class="nav-link", and one with target="_blank".
- Style all links with a type selector.
- Give
.nav-linka different colour. - Style
a[target="_blank"]with an underline. - Add a rule for
nav aand check which links it affects.
Use the browser's inspector to see which rule wins.
Next: Nesting