Skip to main content

CSS Selectors

CSS Class Selector

Written by Updated CSS Class Selector exercises

Apply reusable styles with class selectors and build names that describe components, variants, and states without depending on page structure.

A class is a label you put on HTML elements so CSS can find them. Add class="highlight" to any element, write .highlight in your CSS, and only those elements change.

The element selector styles every p or every h1. A class lets you pick just the ones you want, and you can reuse the same class on as many elements as you like. If you need one single element, there is also the ID selector, but classes are what you will use most of the time.

How to write it

In CSS, a class starts with a dot. In HTML, you write the name without the dot.

Class Selector Syntax

css

.classname {
    property: value;
}

Class Selector Example

css

.highlight {
    color: red;
    font-size: 20px;
}

The matching HTML

html

<p class="highlight">This paragraph is red and larger.</p>
<p>This one is not touched.</p>

More than one class on an element

Separate class names with spaces: class="box highlight". The element gets the styles of both. This is how most real sites work: one class for the component (card), and small extra classes for variations (card featured).

You can also target an element only when it has both classes, by writing them together with no space: .card.featured { ... }.

Example

Complete Example of CSS Class Selector

html

<!DOCTYPE html>
<html>
<head>
    <title>CSS Class Selector</title>
    <style>
        .highlight {
            color: red;
            font-size: 20px;
        }

        .box {
            background: lightgray;
            padding: 10px;
            border: 1px solid black;
        }
    </style>
</head>
<body>

    <h1 class="highlight">Class Selector Example</h1>

    <p class="highlight">This paragraph is highlighted.</p>

    <p>This paragraph is normal.</p>

    <div class="box">This is a styled box.</div>

</body>
</html>

Elements with the highlight class show in red with larger text. The div with the box class gets a light grey background, padding and a border. Everything without a class stays as it was.

Browser Support

Feature
Chrome
Edge
Firefox
Safari
Class selector11211

Versions show the first stable desktop release with unprefixed support. Data source: MDN Browser Compatibility Data 8.0.8.

Naming classes well

Name a class after what the thing is, not how it looks today. .error-message still makes sense when the design changes from red to orange; .red-text does not.

  • Class names are case-sensitive: .Box will not match class="box".
  • A class name cannot start with a number. .2col does not work; .col-2 does.
  • Use hyphens between words, like main-nav. Spaces would make it two classes.
  • Keep selectors short. .card-title keeps working if you move the card somewhere else on the page; .sidebar div ul li .title breaks as soon as the HTML changes.

CSS Class Selector exercises

Style reusable feature cards

Use the .feature class to give all three cards a teal background, white text, and a 12px border radius.

The HTML your code works on
<section class="features">
  <article class="feature">Fast</article>
  <article class="feature">Accessible</article>
  <article class="feature">Responsive</article>
</section>

Result

Check your answer to see it here.