Skip to content

HTML Class Attribute

The HTML class attribute is used to specify one or more class names for an element. Commonly, the class attribute points to a class in a style sheet. The class name is case sensitive.

This attribute can also be used by JavaScript via the HTML DOM to make certain changes to HTML elements with a specified class name.

In HTML5, you can use the class attribute for any HTML element.

In HTML 4.01, the class attribute cannot be used with the following elements: <head>, <html>, <base>, <basefont>, <param>, <style>, <meta>, <script>, and <title>.

Although there aren’t specific requirements for the name of classes, it’s better to use names describing the semantic purpose of the element, and not its presentation. The name should begin with a letter (a-z or A-Z), a hyphen (-), or an underscore (_), and can be followed by letters, digits (0-9), underscores, and hyphens.

Syntax

Syntax of the HTML Class Attribute

html
<tag class="classname">&lt;/tag&gt;

Example of the HTML class attribute:

Example of the HTML Class Attribute

html
<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      .red {
        color: red;
      }
      .orange {
        color: orange;
      }
    </style>
  </head>
  <body>
    <h1>Example of the HTML class attribute</h1>
    <p class="red">It is some red paragraph.</p>
    <p>This is a some text.</p>
    <p class="orange">It is some orange paragraph.</p>
  </body>
</html>

In CSS, if you want to select elements with a specific class, use a period (.) character followed by the class name.

Example of the HTML class attribute used with CSS:

Example of the HTML class attribute used with CSS

html
<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      .title {
        background-color: #1c87c9;
        color: #ffffff;
        padding: 20px;
      }
    </style>
  </head>
  <body>
    <h1>Example of the class attribute</h1>
    <h2 class="title">Heading</h2>
    <p>Lorem ipsum, or lipsum as it is sometimes known, is dummy text used in laying out print, graphic or web designs. The passage is attributed to an unknown typesetter in the 15th century who is thought to have scrambled parts of Cicero's De Finibus Bonorum et Malorum for use in a type specimen book.</p>
    <h2 class="title">Heading</h2>
    <p>Lorem ipsum, or lipsum as it is sometimes known, is dummy text used in laying out print, graphic or web designs. The passage is attributed to an unknown typesetter in the 15th century who is thought to have scrambled parts of Cicero's De Finibus Bonorum et Malorum for use in a type specimen book.Lorem ipsum, or lipsum as it is sometimes known, is dummy text used in laying out print, graphic or web designs. The passage is attributed to an unknown typesetter in the 15th century who is thought to have scrambled parts of Cicero's De Finibus Bonorum et Malorum for use in a type specimen book.</p>
  </body>
</html>

You can also manipulate the class attribute using JavaScript. The classList property provides methods to add, remove, or toggle classes dynamically.

Example of the HTML class attribute used with JavaScript:

Example of the HTML Class Attribute with JavaScript

html
<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      .highlight {
        background-color: yellow;
      }
    </style>
  </head>
  <body>
    <p id="demo">Click the button to add a class.</p>
    <button onclick="document.getElementById('demo').classList.add('highlight')">Add Class</button>
  </body>
</html>

HTML elements can also have more than one class name. Each of them must be separated by a space.

Example of the HTML class attribute with multiple class names:

Example of the HTML class attribute with some class names

html
<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      .title {
        background-color: #202131;
        color: #dddddd;
        padding: 15px 25px;
      }
      .text-right {
        text-align: right;
      }
    </style>
  </head>
  <body>
    <h1>Example of multiple classes</h1>
    <p>
      Lorem ipsum, or lipsum as it is sometimes known, is dummy text used in laying out print, graphic or web designs. The passage is attributed to an unknown typesetter in the 15th century who is thought to have scrambled parts of Cicero's De Finibus Bonorum et Malorum for use in a type specimen book.
    </p>
    <h2 class="title">London</h2>
    <p>
      Lorem ipsum, or lipsum as it is sometimes known, is dummy text used in laying out print, graphic or web designs. The passage is attributed to an unknown typesetter in the 15th century who is thought to have scrambled parts of Cicero's De Finibus Bonorum et Malorum for use in a type specimen book.
    </p>
    <h2 class="title text-right">Paris</h2>
    <p>
      Lorem ipsum, or lipsum as it is sometimes known, is dummy text used in laying out print, graphic or web designs. The passage is attributed to an unknown typesetter in the 15th century who is thought to have scrambled parts of Cicero's De Finibus Bonorum et Malorum for use in a type specimen book.
    </p>
    <h2 class="title">Tokyo</h2>
  </body>
</html>

Different tags, such as <h2> and <p> can have the same class name and the same style.

Example of the HTML class attribute with the <h2> and <p> elements:

Example of the HTML Class Attribute

html
<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      .grey-text {
        color: #808080;
      }
    </style>
  </head>
  <body>
    <h1>Example of the class attribute </h1>
    <h2 class="grey-text">Heading</h2>
    <p class="grey-text">
      Lorem ipsum, or lipsum as it is sometimes known, is dummy text used in laying out print, graphic or web designs. The passage is attributed to an unknown typesetter in the 15th century who is thought to have scrambled parts of Cicero's De Finibus Bonorum et Malorum for use in a type specimen book.
    </p>
  </body>
</html>

Practice

What is the purpose of the class attribute in HTML?

Dual-run preview — compare with live Symfony routes.