CSS: Class Attribute

The id attribute is for one element, a singular usage. To define a style for multiple elements, use the class attribute for each of those elements:

<p class="name"> content </p>

In the <style> section, define the style for that class:

p.name { color: red; }
HTML file: Displayed by browser:
<!DOCTYPE html>
  <html>
    <head>
      <style>
p.error { color:red; }
      </style>
    </head>
    <body>
<p>This is a paragraph.</p>
<p>This is a paragraph.</p>
<p class="error">I am different.</p>
<p>This is a paragraph.</p>
<p class="error">I am different too.</p>
    </body>
  </html>

This is a paragraph.

This is a paragraph.

I am different.

This is a paragraph.

I am different too.

Note: Class names cannot start with a number. They must start with a letter. They can start with an underscore, but the second character has to be a letter and not a number.

Good habit Use the id attribute for just one single element! Use the class attribute for a group of elements. Your code will not pass validation if you use the same id on more than one element, in the same HTML document!
Back button Table of Contents Next button