CSS Selectors
Select the element(s)
CSS selectors
We can divide CSS selectors into five categories:
- Simple selectors (select elements based on name, id, class)
- Combinator selectors (select elements based on a specific relationship between them)
- Pseudo-class selectors (select elements based on a certain state)
- Pseudo-elements selectors (select and style a part of an element)
- Attribute selectors (select elements based on an attribute or attribute value)
Combinator selectors
.classselect all elements with the class name.class1.class2Selects all elements with both class1 and class2.class1 .class2Selects all elements with class2 that is a descendant of an element with class1p.introSelects all<p>elements with class=”intro”div, pSelects all<div>elements and all<p>elementsdiv pSelects all<p>elements inside<div>elementsdiv > pSelects all<p>elements where the parent is a<div>elementdiv + pSelects the first<p>element that is placed immediately after<div>elements
Attribute Selectors
- [title~=flower] Selects all elements with a title attribute containing the word “flower”
a[href^="https"]Selects every<a>element whose href attribute value begins with “https”a[href$=".pdf"]Selects every<a>element whose href attribute value ends with “.pdf”