CSS Selectors - CSS Tutorial - W3Teach
Home / CSS Tutorial / CSS Selectors

CSS Selectors

CSS Selectors

A CSS selector selects the HTML element(s) you want to style. CSS selectors are used to find (or select) HTML elements based on their element name, id, class, attribute, and more.

The CSS element Selector

The element selector selects HTML elements based on the element name.

The CSS id Selector

The id selector uses the id attribute of an HTML element to select a specific element. The id of an element is unique within a page, so the id selector is used to select one unique element.

To select an element with a specific id, write a hash (#) character, followed by the id of the element.

The CSS class Selector

The class selector selects HTML elements with a specific class attribute. To select elements with a specific class, write a period (.) character, followed by the class name.

<!DOCTYPE html>
<html>
<head>
<style>
/* Element selector */
p {
  text-align: center;
  color: red;
}
/* ID selector */
#para1 {
  text-align: center;
  color: blue;
}
/* Class selector */
.center {
  text-align: center;
  color: green;
}
</style>
</head>
<body>

<p>Every paragraph will be red.</p>
<p id="para1">This paragraph is blue!</p>
<p class="center">This paragraph is green!</p>

</body>
</html>
Test Your Knowledge!

Take our CSS Tutorial quiz and earn a certificate.