Skip to content

The class Attribute

Global 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.

<!-- Single class -->
<element class="classname">Content</element>
<!-- Multiple classes (space-separated) -->
<element class="class1 class2 class3">Content</element>
ValueDescription
classnameOne or more space-separated class names. Case-sensitive.
Result
Result
Result
Result

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

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>

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>

The classList API provides methods to manipulate classes dynamically:

Result
// Check if class exists
element.classList.contains('active'); // true or false
// Add one or more classes
element.classList.add('active', 'highlight');
// Remove one or more classes
element.classList.remove('active', 'highlight');
// Toggle class (add if absent, remove if present)
element.classList.toggle('active');
// Replace one class with another
element.classList.replace('old-class', 'new-class');

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>
<!-- Good -->
<div class="user-profile"></div>
<!-- Avoid mixed case (harder to remember) -->
<div class="UserProfile"></div>
<div class="userProfile"></div>
<!-- Too specific - hard to reuse -->
<div class="homepage-hero-section-title-text-blue"></div>
<!-- Better - reusable components -->
<div class="hero-title text-primary"></div>
<!-- Organized and readable -->
<div class="card card-large card-shadow
flex flex-column gap-4
text-center">
Content
</div>

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 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; }

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>

The class attribute is supported by all browsers.

BrowserSupport
ChromeAll versions
FirefoxAll versions
SafariAll versions
EdgeAll versions
  • id - Unique identifier for a single element
  • style - Inline CSS styles
  • data-* - Custom data attributes