Styling with Classes

In addition to the id attribute, the class attribute is another attribute that is valid in every HTML element.

stacked building blocks with similar class names

Like the id attribute, the class attribute is an identifier and selector value for HTML elements. The class attribute groups similar types of elements together by using the same name value. This allows for easy styling of these elements with same CSS code.

Naming Practices for Classes

The class attribute follows the same technical naming conventions as the id attribute. As with the id attribute, the class attribute value should be as descriptive as possible about the function or element types groups, while prioritizing both brevity and readability.

Note Unlike the id attribute which should contain a single unique id value, elements may have more than one class assignment. In this case, a space separates each class.

<div id="example-1" class="class-name-1 class-name-2 notice-the-space">
    <p>Notice the space between each of the three class names.</p>

<div id="example-2" class="class-name-2">
    <p>This paragraph has a unique id, but the same "class-name-2" as the one above it.</p>