Skip to main content

HTML class Attribute

The class attribute sets a class for an HTML element. Several elements can share the same class.


The class Attribute

The class attribute usually points to a class name in a style sheet, so the element gets that style. It can also be used by JavaScript to find and change elements with a given class. Several elements with the same class are all styled the same way.

Tip: The class attribute can be used on any element. Note: The class name is case sensitive.

Three divs with a class

Three divs sharing the city class, all styled the same.

Prompt
A page with three div elements that all share class="city", styled by a .city rule in the head (tomato background, white text, black border, margin and padding). Each div has an h2 and a paragraph for London, Paris, and Tokyo.
Output: complete HTML page

Two spans with a class

Two spans sharing the note class.

Prompt
A page with two span elements sharing class="note", styled by a .note rule (larger red text), used inside a heading and a paragraph to mark important words.
Output: complete HTML page

The Syntax For Class

To make a class, write a period (.) followed by the class name, then put the CSS properties inside curly braces.

Example

Defining and using a class named city.

Prompt
A page that defines a class named city (period followed by the name, CSS in curly braces) giving tomato background and white text with padding, applied to three city headings and paragraphs.
Output: complete HTML page

Multiple Classes

An element can belong to more than one class. Separate the class names with a space (for example, a city class and a main class together), and the element gets the styles from all of them.

Example

An element with two classes getting styles from both.

Prompt
A page where an element has two classes (city and main) separated by a space, plus two elements with just the city class, so the first gets styles from both classes.
Output: complete HTML page

Different Elements Can Share Same Class

Different elements can use the same class name and share its style.

Example

An <h2> and a <p> sharing the city class.

Prompt
A page where different elements (an h2 and a p) both use the city class and share the same styling, for Paris.
Output: complete HTML page

Use of the class Attribute in JavaScript

JavaScript can use the class name to work with specific elements. The getElementsByClassName() method finds all elements with a given class.

Don't worry if the code is unfamiliar; you will learn more in the HTML JavaScript chapter.

Example

A button that hides all elements with the city class.

Prompt
A page with a button that, when clicked, runs JavaScript using getElementsByClassName to hide all elements with the class name city. Include a few city divs.
Output: complete HTML page

Chapter Summary

To recap: the class attribute sets one or more class names for an element. Classes are used by CSS and JavaScript to select elements, can be used on any element, and the class name is case sensitive. Different elements can share a class, and JavaScript can reach them with getElementsByClassName().