👀 Live Preview
Elements styled via class names:
Welcome to our Website!
This paragraph has an important style.
This div has a highlighted background.

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.
Any element.
Space-separated.
.classname.
JS API.
Reusable.
Different.
classThe 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 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.
Add class with one or more space-separated class names:
<p class="important-text">This paragraph has an important style.</p>
<button class="btn btn-primary">Save</button>.important-text { }.classid attribute.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:
<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.
| Pattern | HTML | CSS / JS |
|---|---|---|
| Single class | class="card" | .card { } |
| Multiple classes | class="btn primary" | Both .btn and .primary apply |
| Add class in JS | — | el.classList.add("active") |
| Remove class | — | el.classList.remove("active") |
| Toggle class | — | el.classList.toggle("open") |
| Read all classes | — | el.className or el.classList |
| Element | Supported? | Notes |
|---|---|---|
<div>, <p>, <span> | Yes | Most common styling targets |
<input>, <button>, <form> | Yes | Form and UI components |
<svg> elements | Yes | Class on SVG markup in HTML |
<html>, <body> | Yes | Page-level theming |
| Any HTML element | Yes (global) | Except where explicitly forbidden in spec |
CSS styling with multiple classes and dynamic classList.add in JavaScript.
Elements styled via class names:
This paragraph has an important style.
This div has a highlighted background.
Let’s explore a comprehensive example of using the class attribute to style multiple elements:
<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>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.
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:
<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>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.
is-open with appropriate ARIA attributes such as aria-expanded.<button> instead of <div class="button"> when possible.Add class="important-text" to HTML elements.
Rules like .important-text { } style every matching element.
classList adds or removes classes at runtime for interactivity.
One class definition styles many elements across the page.
The class attribute and classList API are universally supported in all modern browsers.
The class attribute is one of the most widely used features in HTML and CSS.
Bottom line: Use class on any element for CSS and JavaScript hooks in all projects.
classList for dynamic changesclass with legacy classidclass attribute to group elements with similar behavior, making it easier to apply styles and scripts.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.
classBookmark these before writing HTML and CSS.
Any element.
ScopeSpace-separated.
Syntax.name.
add/remove.
JSvs unique id.
Compareclass="btn btn-primary".class is reusable on many elements. id must be unique on the page.element.classList.toggle("classname").class is for CSS/JS hooks. classid is a legacy attribute for embedded objects.class is a global attribute.Practice the class attribute with shared styles and dynamic classList.add in JavaScript.
5 people found this page helpful