</> Code Corner

CSS · CSS Tutorial · Lesson 3

CSS Selectors

Element, id, class, universal and grouping selectors.

CSS selectors are used to find (select) the HTML elements you want to style.

Selector Example Selects
element p All <p> elements
#id #firstname The element with id="firstname"
.class .intro All elements with class="intro"
element.class p.intro Only <p> elements with class="intro"
* * All elements
grouping h1, h2, p All <h1>, <h2> and <p> elements

Example

<style>
#para1 { color: red; }
.center { text-align: center; }
p.large { font-size: 24px; }
h1, h2 { color: teal; }
</style>
<h1 class="center">Heading</h1>
<h2>Sub heading</h2>
<p id="para1">Red paragraph</p>
<p class="center large">Centered and large</p>

Keep reading “CSS Selectors”

Sign up to read the full lesson, try every example in the live editor, and take the 5-question quiz.

Chat with us 👋