A CSS selector selects the HTML element(s) you want to style.
CSS selectors are used to "find" (or select) the HTML elements you want to style.
We can divide CSS selectors into five categories:
The element selector selects HTML elements based on the element name.
Example:
p {
text-align: center;
color: red;
}
Example
The CSS rule below will be applied to the HTML element with id="p1":
#p1 {
text-align: center;
color: blue;
}
Example
In this example all HTML elements with class="c1" will be blue and center-aligned:
.c1 {
text-align: center;
color: blue;
}
The universal selector (*) selects all HTML elements on the page.
Example
The CSS rule below will affect every HTML element on the page:
* {
text-align: center;
color: blue;
}
h1, h2, p {
text-align: center;
color: red;
}