The class Attribute
The class Attribute
Section titled “The class Attribute”The class attribute assigns one or more class names to an HTML element. Unlike the id attribute, the same class can be applied to multiple elements, making it the primary way to style groups of elements with CSS.
Syntax
Section titled “Syntax”<!-- Single class --><element class="classname">Content</element>
<!-- Multiple classes (space-separated) --><element class="class1 class2 class3">Content</element>Values
Section titled “Values”| Value | Description |
|---|---|
classname | One or more space-separated class names. Case-sensitive. |
Examples
Section titled “Examples”Basic Usage
Section titled “Basic Usage” Result
Multiple Classes
Section titled “Multiple Classes” Result
Card Components
Section titled “Card Components” Result
Utility Classes
Section titled “Utility Classes” Result
Class Naming Conventions
Section titled “Class Naming Conventions”BEM (Block Element Modifier)
Section titled “BEM (Block Element Modifier)”BEM is a popular methodology for naming CSS classes:
Result
BEM Structure:
- Block:
search-form- Standalone component - Element:
search-form__input- Part of the block - Modifier:
search-form__button--primary- Variant of element
Utility-First (Tailwind-style)
Section titled “Utility-First (Tailwind-style)”Modern utility-first approaches use many small, single-purpose classes:
<div class="flex items-center justify-between p-4 bg-blue-500 text-white rounded-lg shadow-md"> <h2 class="text-2xl font-bold">Title</h2> <button class="px-4 py-2 bg-white text-blue-500 rounded hover:bg-gray-100"> Action </button></div>Semantic Naming
Section titled “Semantic Naming”Use names that describe purpose, not appearance:
<!-- Good - describes purpose --><div class="warning-message"></div><button class="submit-button"></button>
<!-- Avoid - describes appearance (can change) --><div class="red-box"></div><button class="blue-button"></button>JavaScript classList API
Section titled “JavaScript classList API”The classList API provides methods to manipulate classes dynamically:
Result
Common classList Methods
Section titled “Common classList Methods”// Check if class existselement.classList.contains('active'); // true or false
// Add one or more classeselement.classList.add('active', 'highlight');
// Remove one or more classeselement.classList.remove('active', 'highlight');
// Toggle class (add if absent, remove if present)element.classList.toggle('active');
// Replace one class with anotherelement.classList.replace('old-class', 'new-class');Best Practices
Section titled “Best Practices”Use Classes for Styling, IDs for JavaScript
Section titled “Use Classes for Styling, IDs for JavaScript”<!-- Recommended approach --><button id="submit-btn" class="btn btn-primary">Submit</button>
<style> /* Style with classes */ .btn { padding: 10px 20px; } .btn-primary { background: blue; }</style>
<script> // Target with ID document.getElementById('submit-btn').addEventListener('click', ...);</script>Keep Class Names Lowercase
Section titled “Keep Class Names Lowercase”<!-- Good --><div class="user-profile"></div>
<!-- Avoid mixed case (harder to remember) --><div class="UserProfile"></div><div class="userProfile"></div>Avoid Overly Specific Classes
Section titled “Avoid Overly Specific Classes”<!-- Too specific - hard to reuse --><div class="homepage-hero-section-title-text-blue"></div>
<!-- Better - reusable components --><div class="hero-title text-primary"></div>Group Related Classes
Section titled “Group Related Classes”<!-- Organized and readable --><div class="card card-large card-shadow flex flex-column gap-4 text-center"> Content</div>Performance Considerations
Section titled “Performance Considerations”Class Selector Performance
Section titled “Class Selector Performance”Class selectors (.classname) are very fast in modern browsers:
/* Fast - simple class selector */.button { }
/* Slower - complex descendant selector */.container .sidebar .widget .button { }
/* Fast - use multiple classes instead */.widget-button { }Reusing Classes
Section titled “Reusing Classes”Reusing classes reduces CSS file size and improves maintainability:
/* Instead of this: */.header-button { padding: 10px 20px; background: blue; }.sidebar-button { padding: 10px 20px; background: blue; }.footer-button { padding: 10px 20px; background: blue; }
/* Do this: */.btn { padding: 10px 20px; background: blue; }Accessibility Considerations
Section titled “Accessibility Considerations”Classes themselves don’t affect accessibility, but they enable important styling:
<!-- Use classes to visually hide elements while keeping them accessible --><style> .visually-hidden { position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden; clip: rect(0, 0, 0, 0); border: 0; }</style>
<button> <span class="visually-hidden">Search</span> <svg><!-- Search icon --></svg></button>Browser Support
Section titled “Browser Support”The class attribute is supported by all browsers.
| Browser | Support |
|---|---|
| Chrome | All versions |
| Firefox | All versions |
| Safari | All versions |
| Edge | All versions |
Related Attributes
Section titled “Related Attributes”id- Unique identifier for a single elementstyle- Inline CSS stylesdata-*- Custom data attributes