DEV Community

Cover image for HTML Classes Explained: How to Use the class Attribute in HTML
Rachit Joshi
Rachit Joshi

Posted on

HTML Classes Explained: How to Use the class Attribute in HTML

INTRODUCTION

HTML classes are one of the most commonly used features in web development. They help developers group HTML elements and apply the same styling or behavior to multiple elements.

If you are learning HTML and CSS, understanding the class attribute is essential because classes make it easier to organize and style web pages.

What Is an HTML Class?

An HTML class is an attribute that assigns a name to one or more HTML elements.

For example:

This is a highlighted paragraph.

Here, highlight is the class name.

The same class can be used on multiple elements:

First paragraph

Second paragraph

A heading

This allows you to apply the same CSS rules to all three elements.

HTML Class Syntax

The basic syntax is:


Content

For example:

Welcome to my website!
Enter fullscreen mode Exit fullscreen mode

A class name can be used with many different HTML elements.

Using HTML Classes with CSS

Classes become especially useful when combined with CSS.

HTML:

Welcome to my website.

CSS:

.message {
color: blue;
font-size: 20px;
}

The dot (.) before message tells CSS that message is a class selector.

You can also style several elements with the same class:

HTML Tutorial

Learn HTML step by step.

.title {
color: green;
}

Both elements will receive the same text color.

Using Multiple Classes

An HTML element can have more than one class.

For example:

Important information

Here, the element has two classes:

box
important

You can define separate CSS rules for each:

.box {
padding: 20px;
}

.important {
font-weight: bold;
}

The element will receive the styles from both classes.

Using the Same Class on Different Elements

The same class can be assigned to different HTML elements.

For example:

Learn HTML

HTML is the foundation of web pages.

Start Learning

CSS:

.heading {
font-family: Arial, sans-serif;
}

The same CSS rule will apply to all three elements.

This makes classes useful when you want to maintain consistent styling across a webpage.
**
Using Classes with JavaScript**

HTML classes can also be used with JavaScript to find and manipulate elements.

For example:

Hello World!

JavaScript:

const element = document.querySelector(".message");

element.textContent = "Welcome to JavaScript!";

JavaScript can also select multiple elements using a class:

const items = document.querySelectorAll(".item");

This is useful when creating interactive web pages.

HTML Class vs ID

Both class and id can identify HTML elements, but they are generally used for different purposes.

*Class
*

A class can be used on multiple elements:

First paragraph

Second paragraph

ID

An ID is intended to uniquely identify an element:

This is the main paragraph.

In CSS, classes are selected using .:

.text {
color: blue;
}

IDs are selected using #:

main-text {

color: red;
Enter fullscreen mode Exit fullscreen mode

}

For reusable styling, classes are usually the better choice.

Using Classes with External CSS

In real-world websites, CSS is often placed in a separate file.

HTML:

Learn HTML

Start building websites with HTML.

CSS in style.css:

.card {
border: 1px solid #ccc;
padding: 20px;
border-radius: 8px;
}

Using an external stylesheet keeps HTML and CSS organized and makes it easier to maintain larger projects.

Best Practices for HTML Classes

Here are a few useful practices when working with classes:

  1. Use meaningful names

Prefer:

instead of:

  1. Keep class names readable

Use names that describe the purpose of the element or its styling.

  1. Reuse classes when appropriate

If multiple elements need the same styling, create one reusable class instead of repeating CSS.

  1. Avoid unnecessary classes

Only add classes when they provide a useful purpose.

  1. Use consistent naming

For larger projects, a consistent naming style makes HTML and CSS easier to understand.

Simple Complete Example

Here is a small example combining HTML and CSS classes:

<!DOCTYPE html>


HTML Classes Example
<style>
    .title {
        color: darkblue;
    }

    .description {
        font-size: 18px;
    }

    .button {
        padding: 10px 20px;
        cursor: pointer;
    }
</style>
<h1 class="title">Learn HTML Classes</h1>

<p class="description">
    HTML classes help us group and style elements.
</p>

<button class="button">Learn More</button>

In this example, each element uses a class to connect the HTML structure with CSS styling.

Conclusion

HTML classes are an important part of modern web development. They allow developers to group elements, reuse styles, work with CSS efficiently, and interact with elements through JavaScript.

Once you understand how the class attribute works, you can create cleaner and more maintainable HTML and CSS code.

If you're learning HTML, practicing classes with small projects is a great way to understand how HTML and CSS work together.

Top comments (0)