Guide to CSS Selectors
Learn how to use the right type of CSS selectors to style visual elements
To style elements, you need to identify and target them using CSS selectors. You can select and style elements based on their id, class, types, attributes, values of attributes, and much more.
Figuring out when to use the right type of selector might be one of the hardest parts of CSS. The benefits of investing time in learning and testing different CSS selectors include fewer codes and faster load time. If you are new to web design, we recommend you start with an intro to HTML before learning CSS.
The universal selector is marked with an asterisk (*
) and serves as the basis for other selectors. It selects all elements regardless of their type. For example, creating a rule for the selector that adds a red background to an element will add red background to every single element of the page:
* {
background-color: red;
}
The universal selector is often used in combinations with other selectors, allowing us to select all elements inside another element.
Element type selectors target elements by their specific type. To use this selector, type the element's name without any extra <h1>
headings of the
h1 {
text-align: left;
color: black;
}
This style will only apply to <h1>
elements and won't affect other headings and body text.
Class selectors find elements with the specific class set by the .
). For example, the selector for all elements of the class center would look like this: .center
.
The class selector is the most commonly used selector type because of its flexibility — you can add the same class to different elements or multiple classes to one element.
Combining element type selectors with class selectors allows you to target elements of a specific type with a certain class. For example, to select all <h1>
headings with the class center, use the selector h1.center
.
The id selector uses the id
attribute of an element to find a specific element on the #
) before the id's value — for example, #header
.
You might want to add element tags just to let yourself visually know in your id="header"
is applied to the container <div>
, you can use div#header
as a selector.
Pro Tip: Id selectors aren't widely used in CSS, but they are crucial in JavaScript.
Combinator selectors explain the relationship between the element you want to target and one of the other elements — whether the target's children, parents, ancestors, descendants, or siblings.
The descendant selector symbol is a space (
) — for example, div h1
. This selector targets all <h1>
elements nested inside all <div>
elements. The other combinators are the child selector (>
), the adjacent sibling selector (+
), and the general sibling selector (~
).[1]
Pseudo-classes allow you to target elements that you wouldn't be able to target otherwise. A pseudo-class follows a selector it specifies with a colon (:
) between them. Most selectors of this type can be loosely divided into 2 groups:
- Dynamic pseudo-class selectors, like
a:hover:
They allow us to target elements that users are interacting with. For example, the:hover
selector allows you to change the style of links users hover over. - Structural pseudo-class selectors like
p:first-child:
This type of selector allows us to indicate elements with higher accuracy. For example,:first-child
and:last-child
allows you to target the first or last element among a group of sibling elements, respectively.[2]
The
If you want to style the first letter of every paragraph differently to make it look like a storybook — there's a selector that allows you to do just that! The selector p::first-letter
will apply the chosen style to all of the first letters of all paragraphs. Other popular pseudo-elements are ::before
and ::after
to add cosmetic content before or after the element.
To learn more about pseudo-elements, check their comprehensive list on MDN Web Docs.[3]
Attribute selectors aren't common, but they have some powerful use cases. They allow you to target elements that have a particular attribute or attribute value.
Attribute selectors are enclosed in square brackets ([...]
). For example, to style all elements with a title
attribute, we'd use the selector [title]
. We can also target elements with specific titles. In that case, the code would look like [title="Important"]
.
An element type selector matches elements of the corresponding type — for example, the <p>
, <span>
, and <div>
<div>
are quite generic, and they hold all kinds of
A common <body>
tag on any given
body {
padding: 0;
margin: 0;
width: 100%;
height: 100%;
}
Class selectors are what you'll end up using most of the time when creating style sheets. Class selectors are introduced with a full stop (.
). This selector applies styling to all elements with a specified class attribute. It's a great way to customize how
The id selector lets you apply styling to an element with a specified id. It uses a hash (#
) followed by the element's id.
While it might be easy to target elements by giving them ids, a style sheet like that can become a nightmare to maintain as it grows bigger. That's why this type of selector is used less often than class selectors.