🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEcss

css Documentation

LOADING ENGINE...

Class Selectors

AI & DATA SCIENCE // class-selectors

Class selectors, written with a leading dot like .card, target every element carrying that class attribute, and are the most common way to apply reusable styling in CSS.

Syntax

.classname {
  property: value;
}

Deep Dive Course

A class selector matches any element whose class attribute includes the given class name, and a single element can carry multiple classes at once, separated by spaces in the HTML, like class="card featured", combining the styling from every matching class rule. Classes carry moderate specificity, higher than a plain type selector but lower than an ID selector, and are specifically designed to be reused across as many elements as needed throughout a document, unlike an ID, which is meant to be unique.

1Understanding Class Selectors

A class selector matches any element whose class attribute includes the given class name, and a single element can carry multiple classes at once, separated by spaces in the HTML, like class="card featured", combining the styling from every matching class rule. Classes carry moderate specificity, higher than a plain type selector but lower than an ID selector, and are specifically designed to be reused across as many elements as needed throughout a document, unlike an ID, which is meant to be unique.

💡

Design classes to be reusable across multiple elements and even multiple projects, rather than naming them after one specific instance's content or position — a class named .card is far more reusable than one named .third-box-on-homepage.

editor.html
.card {
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 16px;
}
localhost:3000

2Practical Example

Here is a real-world application of Class Selectors showing how it is used in production CSS code.

editor.html
<div class="card featured">...</div>

.card { padding: 16px; }
.featured { border: 2px solid gold; }
localhost:3000

3Best Practices

Follow these guidelines when working with Class Selectors:

1. Use classes as the primary, default way to apply styling, since they're reusable across any number of elements, unlike IDs

2. Combine multiple classes on a single element, like class="btn btn-primary", to compose styling from smaller, reusable pieces rather than duplicating declarations across many single-purpose classes

3. Name classes descriptively based on their purpose or role, like .error-message, rather than their specific visual appearance, like .red-text, which becomes misleading if the styling later changes

⚠️

Tip: Design classes to be reusable across multiple elements and even multiple projects, rather than naming them after one specific instance's content or position — a class named .card is far more reusable than one named .third-box-on-homepage.

editor.html
.card {
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 16px;
}
localhost:3000

Examples

Example 01Basic Usage
.card {
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 16px;
}
Example 02Advanced Example
<div class="card featured">...</div>

.card { padding: 16px; }
.featured { border: 2px solid gold; }

Best Practices

  • Use classes as the primary, default way to apply styling, since they're reusable across any number of elements, unlike IDs
  • Combine multiple classes on a single element, like class="btn btn-primary", to compose styling from smaller, reusable pieces rather than duplicating declarations across many single-purpose classes
  • Name classes descriptively based on their purpose or role, like .error-message, rather than their specific visual appearance, like .red-text, which becomes misleading if the styling later changes

Interview Question

Why is a class selector generally preferred over an ID selector for styling that might apply to more than one element?

Hint: Think about the actual HTML rule for how many times an id value versus a class value is allowed to appear on a single page.

An id attribute's value is meant to be unique within an entire HTML document, no two elements should ever share the same id, which is a rule enforced by HTML's own semantics even though browsers don't strictly reject a duplicate id outright, they just produce ambiguous, unreliable behavior wherever that id is referenced, such as an anchor link jumping to the wrong element. A class attribute, by contrast, is explicitly designed to be shared freely across as many elements as needed, with no uniqueness constraint at all, making it the structurally correct choice whenever styling needs to apply to more than a single specific element. Using an ID selector for styling that's actually meant to be reused across multiple elements would require either duplicating that id, violating HTML's uniqueness expectation, or writing a separate, needlessly duplicated rule for each individual id, whereas a single class rule naturally applies to however many elements carry that class, which is exactly the more scalable, maintainable approach.

Exercises

MediumPractice using Class Selectors in a real scenario.
View Solution
.card {
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 16px;
}

Frequently Asked Questions

Why is a class selector generally preferred over an ID selector for styling that might apply to more than one element?

An id attribute's value is meant to be unique within an entire HTML document, no two elements should ever share the same id, which is a rule enforced by HTML's own semantics even though browsers don't strictly reject a duplicate id outright, they just produce ambiguous, unreliable behavior wherever that id is referenced, such as an anchor link jumping to the wrong element. A class attribute, by contrast, is explicitly designed to be shared freely across as many elements as needed, with no uniqueness constraint at all, making it the structurally correct choice whenever styling needs to apply to more than a single specific element. Using an ID selector for styling that's actually meant to be reused across multiple elements would require either duplicating that id, violating HTML's uniqueness expectation, or writing a separate, needlessly duplicated rule for each individual id, whereas a single class rule naturally applies to however many elements carry that class, which is exactly the more scalable, maintainable approach.

Related Functions

Basic-SelectorsID-SelectorsCSS-Attribute-Selectors