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 JavaScript.

HiDrive Cloud Storage
Store and share your data on the go
  • Store, share, and edit data easily
  • Backed up and highly secure
  • Sync with all devices

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­ut­es you should be aware of when learning HTML. Classes allow de­vel­op­ers to style or ma­nip­u­late multiple elements without having to address them in­di­vid­u­al­ly.

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­vel­op­ers. They are essential, par­tic­u­lar­ly for two areas of ap­pli­ca­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­sis­tent and at the same time, make the CSS code modular.
  2. Ma­nip­u­lat­ing elements with JavaScript: By using HTML classes, pro­gram­mers can easily select specific groups of elements and ma­nip­u­late them through JavaScript code. This is es­pe­cial­ly useful for dynamic in­ter­ac­tions and user in­ter­faces that need to respond to user behavior.

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­sid­ered 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 centered 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­a­ra­tors.

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­ut­es 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­enc­ing in CSS: To style classes in CSS, a preceding dot (.) is used, while for styling IDs, a preceding hashtag (#) is used.
  • Speci­fici­ty: IDs have a higher speci­fici­ty 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 prece­dence. In this case, the class styles may be over­rid­den.
Domain Name Reg­is­tra­tion
Build your brand on a great domain
  • Free Wildcard SSL for safer data transfers 
  • Free private reg­is­tra­tion for more privacy
  • Free Domain Connect for easy DNS setup
Go to Main Menu