💡 Tip: Login now to track your course progress and unlock your downloadable certificates instantly!

CSS Class Selector

The CSS Class Selector is used to style specific HTML elements that have a class attribute.
Unlike the element selector, which targets all elements of a type, the class selector allows you to apply styles to only selected elements, giving you more control and flexibility.

A class can be used multiple times on a page.

Class selectors are defined using a dot (.) followed by the class name.

Class Selector Syntax
.classname {
    property: value;
}
Class Selector Example
.highlight {
    color: red;
    font-size: 20px;
}

👉 This will apply styles to all elements with class="highlight".

Attributes

PropertyDescriptionExample
colorSets text colorcolor: blue;
backgroundSets background colorbackground: yellow;
font-sizeSets text sizefont-size: 18px;
paddingAdds inner spacingpadding: 10px;
borderAdds borderborder: 1px solid black;

Example

Complete Example of CSS Class Selector
<!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>

Output

Browser Output

Elements with class highlight will appear in red color with larger text
The <div> with class box will have a light gray background, padding, and border
Elements without the class will remain unchanged

Browser Support

Chrome
Chrome
Firefox
Firefox
Edge
Edge
Safari
Safari
Opera
Opera
IE
IE9+
✅Yes✅Yes✅Yes✅Yes✅Yes✅Yes

Notes

  • Class selector starts with a dot (.)
  • A class can be used on multiple elements
  • An element can have multiple classes (e.g., class="box highlight")
  • Class names should not start with numbers
  • Best practice: use meaningful and reusable class names

Conclusion

The CSS Class Selector provides flexibility and control by allowing you to style specific elements without affecting others. It is one of the most commonly used selectors in modern web development.

Sign In Form

User your email and password to singn in

Don’t have an account, signup here : 

HTML5 & CSS3

Tools and Tutorials

Want a Website

Want a website for your business ?

Please fill out the details below, so we can reach out to you.

Registration Form

Signup to track your record and much more.

We have sent you an email with a registration link. Please click the link to verify your email address, or enter the 6-digit OTP provided in the email.

Didn't receive the OTP. Regenerate OTP Resend