HTML class Attribute

Beginner
⏱️ 5 min read
📚 Updated: Jun 2026
🎯 2 Examples
HTML & CSS

Introduction

The class attribute in HTML is a fundamental feature that allows developers to assign one or more class names to HTML elements. These class names, defined in CSS or referenced from external stylesheets, enable styling and help JavaScript select groups of elements for behavior and interactivity.

What You’ll Learn

01

Global attr

Any element.

02

Multiple

Space-separated.

03

CSS hook

.classname.

04

classList

JS API.

05

vs id

Reusable.

06

vs classid

Different.

Purpose of class

The primary purpose of the class attribute is to provide a way to select and style multiple elements with common characteristics. By assigning a class name to an element, you can apply predefined styles or scripts to a group of elements, promoting consistency and maintainability in your HTML documents.

💡
class vs id

class names can be reused on many elements. id must be unique per page. Use classes for shared styling; use ids for one-of-a-kind anchors or JavaScript targets.

📝 Syntax

Add class with one or more space-separated class names:

class.html
<p class="important-text">This paragraph has an important style.</p>

<button class="btn btn-primary">Save</button>

Syntax Rules

  • Global attribute—valid on virtually every HTML element.
  • Multiple classes are separated by spaces, not commas.
  • Class names should start with a letter; use letters, digits, hyphens, and underscores.
  • CSS targets classes with a dot: .important-text { }.
  • Do not confuse with the unrelated legacy classid attribute.

💎 Values

The class attribute accepts one or more space-separated class names. These names are defined in your CSS or linked from external stylesheets. Here is a basic example:

class-values.html
<p class="important-text">This paragraph has an important style.</p>

In this example, the class attribute assigns the important-text class to the paragraph element, allowing you to style it using CSS with the selector .important-text.

⚡ Quick Reference

PatternHTMLCSS / JS
Single classclass="card".card { }
Multiple classesclass="btn primary"Both .btn and .primary apply
Add class in JSel.classList.add("active")
Remove classel.classList.remove("active")
Toggle classel.classList.toggle("open")
Read all classesel.className or el.classList

Applicable Elements

ElementSupported?Notes
<div>, <p>, <span>YesMost common styling targets
<input>, <button>, <form>YesForm and UI components
<svg> elementsYesClass on SVG markup in HTML
<html>, <body>YesPage-level theming
Any HTML elementYes (global)Except where explicitly forbidden in spec

Examples Gallery

CSS styling with multiple classes and dynamic classList.add in JavaScript.

👀 Live Preview

Elements styled via class names:

Welcome to our Website!

This paragraph has an important style.

This div has a highlighted background.

Example

Let’s explore a comprehensive example of using the class attribute to style multiple elements:

class.html
<style>
  .important-text {
    font-weight: bold;
    color: red;
  }
  .highlight-background {
    background-color: yellow;
  }
</style>

<h1 class="important-text">Welcome to our Website!</h1>
<p class="important-text">This paragraph has an important style.</p>
<div class="highlight-background">
  <p>This div has a highlighted background.</p>
</div>
Try It Yourself

How It Works

In this example, the class attribute is used to apply different styles to multiple elements. Both the heading and paragraph share important-text, while the div uses highlight-background, creating a visually cohesive page.

Dynamic Values with JavaScript

Similar to other attributes, the class attribute can be manipulated dynamically using JavaScript. This is useful when you want to alter styling or behavior based on user interactions:

class.html
<p id="dynamicElement">This text will get a new style dynamically.</p>

<script>
  // Dynamically add a class to an element
  document.getElementById("dynamicElement").classList.add("dynamic-style");
</script>
Try It Yourself

How It Works

In this script, the classList.add method dynamically adds the dynamic-style class to the element with id dynamicElement. CSS rules for .dynamic-style then apply immediately.

♿ Accessibility

  • Classes are not semantic — Do not rely on class names alone for meaning; use proper HTML elements and ARIA when needed.
  • State classes — Pair visual classes like is-open with appropriate ARIA attributes such as aria-expanded.
  • Do not hide content with CSS only — Use accessible patterns when toggling visibility.
  • Focus styles — Never remove focus outlines without providing an accessible alternative.
  • Prefer semantic HTML — Use <button> instead of <div class="button"> when possible.

🧠 How class Works

1

Author assigns class names

Add class="important-text" to HTML elements.

Markup
2

CSS matches selectors

Rules like .important-text { } style every matching element.

Styles
3

JavaScript can update classes

classList adds or removes classes at runtime for interactivity.

Script
=

Reusable styled components

One class definition styles many elements across the page.

Browser Support

The class attribute and classList API are universally supported in all modern browsers.

HTML standard · Essential

Supported everywhere

The class attribute is one of the most widely used features in HTML and CSS.

100% Modern browsers
Google Chrome Full support
Supported
Mozilla Firefox Full support
Supported
Apple Safari Full support
Supported
Microsoft Edge Full support
Supported
class attribute Universal

Bottom line: Use class on any element for CSS and JavaScript hooks in all projects.

💡 Best Practices

✅ Do

  • Use meaningful, descriptive class names
  • Reuse classes for shared styling patterns
  • Separate multiple classes with spaces
  • Use classList for dynamic changes
  • Follow a consistent naming convention (BEM, utilities, etc.)

❌ Don’t

  • Confuse class with legacy classid
  • Use spaces inside a single class name
  • Rely on classes for semantic meaning alone
  • Start class names with digits
  • Overuse classes where semantic elements suffice
  • Use meaningful and descriptive class names to improve code readability and maintenance.
  • Avoid excessive use of classes for styling; prefer using semantic HTML elements when applicable.
  • Leverage the class attribute to group elements with similar behavior, making it easier to apply styles and scripts.

Conclusion

The class attribute is a crucial tool for web developers, enabling efficient styling and scripting of HTML elements. It connects markup to CSS and JavaScript across your entire site.

By understanding how to assign, combine, and manipulate class names, you can create well-organized and visually appealing web pages.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about class

Bookmark these before writing HTML and CSS.

5
Core concepts
📝 02

Multiple

Space-separated.

Syntax
🔴 03

CSS dot

.name.

Styles
⚙️ 04

classList

add/remove.

JS
🔖 05

Reusable

vs unique id.

Compare

❓ Frequently Asked Questions

It assigns one or more class names to an element for CSS styling and JavaScript selection.
Separate names with spaces: class="btn btn-primary".
class is reusable on many elements. id must be unique on the page.
Use element.classList.toggle("classname").
No. class is for CSS/JS hooks. classid is a legacy attribute for embedded objects.
Nearly all HTML elements—class is a global attribute.

Style elements with CSS classes

Practice the class attribute with shared styles and dynamic classList.add in JavaScript.

Try CSS class example →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful