` elements.
It's also extended to a class named `.my-class`, and an element that has a `lang` attribute. Check your understanding Test your knowledge of simple selectors * {} What kind of simple selector is used in the above snippet? attribute `[]` are used for **attribute** simple selectors. ID `#` are used for **ID** simple selectors. universal `*` is the **universal** simple selector. class `.` are used for **class** simple selectors. div {} What kind of simple selector is used in the above snippet? class A `.` is used for **class** simple selectors. type An `element` name is used for **type** simple selectors. attribute Square brackets `[]` are used for **attribute** simple selectors. ID A `#` is used for **ID** simple selectors. Pseudo-classes and pseudo-elements CSS provides useful selector types that focus on specific platform state,
like when an element is hovered, structures *inside* an element, or parts of an element. Pseudo-classes HTML elements find themselves in various states,
either because they are interacted with,
or one of their child elements is in a certain state. For example, an HTML element could be hovered with the mouse pointer by a user
*or* a child element could also be hovered by the user.
For those situations, use the `:hover` pseudo-class. `/* Our link is hovered */ a:hover { outline: 1px dotted green; } /* Sets all even paragraphs to have a different background */ p:nth-child(even) { background: floralwhite; }` Find out more in the [pseudo-classes module](/learn/css/pseudo-classes). Pseudo-element Pseudo-elements differ from pseudo-classes because instead of responding to the platform state,
they act as if they are inserting a new element with CSS.
Pseudo-elements are also syntactically different from pseudo-classes,
because instead of using a single colon (`:`), we use a double colon (`::`). `.my-element::before { content: 'Prefix - '; }` As in the above demo where you prefixed a link's label with the type of file it was,
you can use the `::before` pseudo-element to insert content **at the start of an element**,
or the `::after` pseudo-element to insert content at the **end of an element**. Pseudo-elements aren't limited to inserting content, though.
You can also use them to target specific parts of an element.
For example, suppose you have a list.
Use `::marker` to style each bullet point (or number) in the list `/* Your list will now either have red dots, or red numbers */ li::marker { color: red; }` You can also use `::selection` to style the content that has been highlighted by a user. `::selection { background: black; color: white; }` Learn more in the [module on pseudo-elements](/learn/css/pseudo-elements). Check your understanding Test your knowledge of pseudo selectors Pseudo-element selectors use how many colons? : One `:` is used to target pseudo-classes. :: Two `::` are used to target pseudo-elements. ::: This is invalid and targets nothing. p:hover { background: white; color: black; } What kind of pseudo selector is used in the above snippet? Pseudo-class One `:` is used to target pseudo-classes. Pseudo-element Two `::` are used to target pseudo-elements. Complex selectors You have already seen a vast array of selectors,
but sometimes, you will need more *fine-grained control* with your CSS.
This is where complex selectors step in to help. It's worth remembering at this point that although the following selectors give us more power,
we can only **cascade downwards**, selecting child elements.
We are not able to target upwards and select a parent element.
We cover what the cascade is and how it works
[in a later lesson](/learn/css/the-cascade). Combinators A combinator is what sits between two selectors.
For example, if the selector was `p > strong`, the combinator is the `>` character.
The selectors which use these combinators help you select items based on their position in the document. Descendant combinator To understand descendant combinators,
you need to first understand parent and child elements. `A paragraph of text with some bold text for emphasis.
` The parent element is the `` which contains text.
Inside that `
` element is a `` element, making its content bold.
Because it is inside the ``, it is a child element. A descendant combinator allows us to target a child element.
This uses a space () to instruct the browser to look for child elements: `p strong { color: blue; }` This snippet selects all `` elements
that are child elements of `` elements only, making them blue recursively. Because the descendant combinator is recursive,
the padding added to each child element applies, resulting in a staggered effect. This effect is better visualised in the above example,
using the combinator selector, `.top div`.
That CSS rule adds left padding to those `
` elements.
Because the combinator is recursive,
all `
` elements that are in `.top` will have that same padding applied to them. Take a look at the HTML panel in this demo
to see how the `.top` element has several `
` child elements which themselves,
have `
` child elements. Next sibling combinator You can look for an element that immediately follows another element
by using a `+` character in your selector. To add space between stacked elements,
use the next sibling combinator to add space
*only* if an element is a **next sibling** of a child element of `.top`. You could add margin to all child elements of `.top`,
using the following selector: `.top * { margin-top: 1em; }` The problem with this is that because you're selecting every child element of `.top`,
this rule potentially creates extra, unnecessary space.
The **next sibling combinator**,
mixed with a **universal selector** enables you to not only control what elements get space,
but also apply space to **any element**.
This provides you with some long-term flexibility,
regardless of what HTML elements appear in `.top`. Subsequent- sibling combinator A subsequent combinator is very similar to a next sibling selector.
Instead of a `+` character however,
use a `~` character.
How this differs is that an element just has to follow another element with the same parent,
rather than being the next element with the same parent. Use a subsequent selector along with a `:checked` pseudo class to create a pure CSS switch element. This subsequent combinator provides a little less rigidity,
which is useful in contexts like the above sample,
where we change the color of a custom switch when its associated checkbox has the `:checked` state. Child combinator A child combinator (also known as direct descendant)
allows you more control over the recursion that comes with combinator selectors.
By using the `>` character, you limit the combinator selector to apply **only** to direct children. Consider the previous, next sibling selector example.
The space is added to each **next sibling**,
but if one of those elements also has **next sibling elements** as children,
it can result in undesirable, extra spacing. To alleviate this problem,
change the **next sibling selector** to incorporate a child combinator: `> * + *`.
The rule will now **only** apply to direct children of `.top`. Compound selectors You can combine selectors to increase specificity and readability.
For example, to target `
` elements,
that also have a class of `.my-class`, write the following: `a.my-class { color: red; }` This wouldn't apply a red color to all links
and it would also only apply the red color to `.my-class` **if** it was on an `` element.
For more on this, see the [specificity module](/learn/css/specificity). Check your understanding Test your knowledge of complex selectors Which of the following symbols is **not** a selector combinator? > The direct descendant combinator. á Invalid, not a CSS symbol. + The next sibling combinator. * This universal simple selector. . The class simple selector. section.awesome { border: 1px solid hotpink; } The above selector is an example of a... Combinator A symbol used to combine selectors into a more specific one. Compound selector When 2 or more selectors are used together, without a combinator, in order to create a more specific selector. Terminator Not a selector type, but sounds like one doesn't it? đ¤ Resources [CSS selectors reference](https://developer.mozilla.org/docs/Web/CSS/CSS_Selectors) [Interactive selectors game](https://flukeout.github.io/) [Pseudo-class and pseudo-elements reference](https://developer.mozilla.org/docs/Learn/CSS/Building_blocks/Selectors/Pseudo-classes_and_pseudo-elements) [A tool that translates CSS selectors into plain-english explainers](https://kittygiraudel.github.io/selectors-explained/) [Previous
arrow_back
Box Model](/learn/css/box-model) [Next
Nesting
arrow_forward](/learn/css/nesting) Except as otherwise noted, the content of this page is licensed under the [Creative Commons Attribution 4.0 License](https://creativecommons.org/licenses/by/4.0/), and code samples are licensed under the [Apache 2.0 License](https://www.apache.org/licenses/LICENSE-2.0). For details, see the [Google Developers Site Policies](https://developers.google.com/site-policies). Java is a registered trademark of Oracle and/or its affiliates. Last updated 2021-03-29 UTC.