What HTML attribute assigns a class to an element?

The story behind the answer

The HTML attribute that assigns a class to an element is `class`.

A class groups elements that share a meaning, behavior, or presentation. For example, `<p class="warning">` lets CSS target the paragraph with the `.warning` selector, while JavaScript can find matching elements through class-based DOM methods. The value may contain multiple class names separated by spaces.

The `class` attribute is a global HTML attribute, so it can be used on nearly any HTML element. It does not make an element unique: many elements can share the same class. That is the key difference from `id`, which is intended to identify one element within a document.

A common mix-up is confusing `class` with `style`. The `style` attribute holds CSS declarations directly on one element, whereas `class` assigns a reusable label whose rules normally live in a stylesheet. Classes also support semantics beyond visual styling, such as component states, JavaScript hooks, and microformats.

Source: Wikipedia · fact-checked Aug. 2026

Add question to a list

Choose a list to keep this question in: