You can add an HTML element to an HTML class using the class attribute. Doing so lets you modify all the elements in a class in the same way using CSS or JavaS­cript.

HiDrive Cloud Storage
Store and share your data on the go
  • Store, share and edit data easily
  • ISO-certified European data centres
  • Highly secure and GDPR compliant

What are HTML classes?

HTML classes are used in HTML to identify and group elements in a web page. Class is one of the most important HTML at­trib­utes you should be aware of when learning HTML. Classes allow de­velopers to style or ma­nip­u­late multiple elements without having to address them in­di­vidu­ally.

Tip

The class attribute can be applied to any HTML element.

What are HTML classes used for?

HTML classes are part of the standard toolkit for web de­velopers. They are essential, par­tic­u­larly for two areas of ap­plic­a­tion:

  1. Styling elements with CSS: If you include CSS in your HTML, classes make it possible to define styles that can be applied to several elements at the same time. This allows you to keep the design of your website con­sist­ent and at the same time, make the CSS code modular.
  2. Ma­nip­u­lat­ing elements with JavaS­cript: By using HTML classes, pro­gram­mers can easily select specific groups of elements and ma­nip­u­late them through JavaS­cript code. This is es­pe­cially useful for dynamic in­ter­ac­tions and user in­ter­faces that need to respond to user behaviour.

What is the syntax of HTML classes?

Defining a class in HTML is straight­for­ward. Simply add the class attribute to the element you want to assign a class to and provide it with a name. The name should describe the purpose or function of the class. Here’s what the code looks like:

<p class="Testclass">This text belongs to the class called ‘Testclass’.</p>
html

In the example above, an HTML paragraph element was assigned to the ‘Testclass’ class.

Tip

Class names are case-sensitive. For example, ‘testclass’ and ‘Testclass’ would be con­sidered two different HTML classes.

A more detailed example shows how useful classes are:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>HTML classes example</title>
    <style>
        .highlight {
            background-color: blue;
        }
        .center {
            text-align: center;
        }
    </style>
</head>
<body>
    <h1 class="center">Here’s the HTML class example</h1>
    <p class="highlight">This text is highlighted because it belongs to the ‘highlight’ class.</p>
    <p class="highlight center">This text is highlighted and centred because it belongs to the ‘highlight’ and ‘center’ classes.</p>
</body>
</html>
html

The layout and styling of the para­graphs and headings in the example above are con­trolled by HTML classes, which are ref­er­enced in the CSS. Keep in mind that to select an HTML class in CSS, you need to prefix the class name with a dot (.).

Tip

A single HTML element can belong to several classes. Simply list them one after the other, without sep­ar­at­ors.

What is the dif­fer­ence between HTML classes and HTML IDs?

Alongside the class attribute, HTML also offers the HTML attribute id. While both at­trib­utes have similar functions, it’s important not to confuse them, as there are several key dif­fer­ences between the two:

  • Unique­ness: An ID must be unique within an HTML document, meaning no two elements can share the same ID. In contrast, classes can be shared by multiple elements in the same document.
  • Ref­er­en­cing in CSS: To style classes in CSS, a preceding dot (.) is used, while for styling IDs, a preceding hashtag (#) is used.
  • Spe­cificity: IDs have a higher spe­cificity than classes. This means that if both an ID and a class are applied to the same HTML element and define con­flict­ing styles, the styles defined by the ID will take pre­ced­ence. In this case, the class styles may be over­rid­den.
Register your domain name
Launch your business on the right domain
  • Free WordPress with .co.uk
  • Free website pro­tec­tion with one Wildcard SSL
  • Free Domain Connect for easy DNS setup
Go to Main Menu