Code has been added to clipboard!

How to Use the HTML Class Attribute Correctly

Reading time 3 min
Published Jun 23, 2017
Updated Jan 23, 2020

TL;DR – HTML class attribute specifies a class name. You can use it to select and access HTML elements for various tasks performed using CSS and JavaScript.

Defining an HTML Class

Adding HTML class is no different to any other attribute. You have to include it in the opening tag and define the value wrapped in quotes:

Syntax <tag class="name">content</tag>
Example <div class="bigger">Wash your hands!</tag>

In HTML, you can also define multiple classes for a single element. All of them must be included in the same pair of quotes and separated using spaces:

Syntax <tag class="name1 name2 name3 name4">content</tag>
Example <div class="harder better faster stronger">Wash your hands!</tag>

Note: remember HTML classes are case-sensitive.

Using HTML Classes for CSS

The most common use for HTML classes is CSS styling. You can create a specific style and assign it to a particular class in HTML. All elements that have that class specified will inherit the style:

Example
<style>
  .green {
    color: green;
    margin-left: 20px;
  }
</style>

All the styling properties must be surrounded with <style> tags and placed in the head section of the document.

Note: remember to always include a period (.) in front so the system recognizes it as a CSS selector.

Most Common Properties for Styling Classes

The color property defines the text color for the element. You can define colors by their names, RGB or HEX values:

Example
<style>
  .clr-g {
      color: green;
  }
</style>

To specify the background color, you can use background-color:

Example
<style>
  .bg-c {
      background-color: #f3f3f3;
  }
</style>

Tip: use a color picker to get the values for the exact tone you need.

To align an element, use text-align. There are three possible values for this property: left, center or right:

Example
<style>  
  .align-c {
      text-align: center;
  }
</style>

The padding property creates space between the element's content and its border:

Example
<style>
  .bg-c {
      background-color: #f3f3f3;
  }
  .pad {
      padding: 20px 0 20px 0;
  }
</style>

To add space between the element's border and the content outside it, you can use the margin property:

Example
<style>
  .bg-c {
      background-color: #f3f3f3;
  }
  .mar {
      margin: 0 0 0 50px;
  }
</style>

Working With Classes in JavaScript

If you have some knowledge in JavaScript, you might recognize the className property. Developers use it to set or return the name of a class for an HTML element.

To access elements that have a particular HTML class name specified, you need to use the document.getElementsByClassName() method in JavaScript:

Example
var sampleElem1 = document.getElementsByClassName("sampleStyle")[0].className;
var sampleElem2 = document.getElementById("sampleDiv").className;

HTML Class: Useful Tips

  • Some beginners get confused between using HTML class and id attributes, as both can be used to access elements. Remember that an element can only have one ID which must be unique. Classes can be applied to multiple elements and also combined.
  • You can use any Unicode character for a class name (e.g., for music-related sections). However, it's not exactly a recommended practice: most developers advise staying with semantic names.
Basics
Introduction
Syntax
Editors
Basic Examples
Head Section
<!DOCTYPE>
Tags and Elements
Semantic Elements
Tags Reference
Attributes
Comments
Block and Inline Elements
Forms
Form Elements
Input
Responsive Web Design
Inline Scripts
Uniform Resource Locator
Redirect
XHTML
Geolocation
Drag and Drop
Local Storage
Web Workers
Server-Sent Events
Character Encoding
Text Formatting
Quotation and Citation Elements
Headings
Paragraphs
Links
Tables
Lists
Symbols
Space
Tab
Styles
Computer Code
Layout
Classes
Colors
Images
iframes
Audio Player
Video Player
YouTube Videos
Multimedia
Canvas
SVG
<!-- -->
<a>
<abbr>
<acronym> DEPRECATED
<address>
<applet> DEPRECATED
<article>
<aside>
<audio>
<b>
<base>
<basefont> DEPRECATED
<bdi>
<bdo>
<big> DEPRECATED
<blink> DEPRECATED
<blockquote>
<body>
<br>
<button>
<canvas>
<caption>
<center> DEPRECATED
<cite>
<code>
<col>
<colgroup>
<datalist>
<dd>
<del>
<details>
<dfn>
<dialog>
<dir> DEPRECATED
<div>
<dl>
<dt>
<em>
<embed>
<fieldset>
<figcaption>
<figure>
<font> DEPRECATED
<footer>
<form>
<frame> DEPRECATED
<frameset> DEPRECATED
<h1> – <h6>
<head>
<header>
<hr>
<html>
<i>
<iframe>
<img>
<input>
<ins>
<kbd>
<keygen> DEPRECATED
<label>
<legend>
<li>
<link>
<main>
<map>
<mark>
<menu>
<menuitem> DEPRECATED
<meta>
<meter>
<nav>
<noframes> DEPRECATED
<noscript>
<object>
<ol>
<optgroup>
<option>
<output>
<p>
<param>
<pre>
<progress>
<q>
<rp>
<rt>
<ruby>
<s>
<samp>
<script>
<section>
<select>
<small>
<source>
<span>
<strike> DEPRECATED
<strong>
<style>
<sub>
<summary>
<sup>
<table>
<tbody>
<td>
<tfoot>
<th>
<thead>
<time>
<title>
<tr>
<track>
<tt> DEPRECATED
<u>
<ul>
<var>
<video>
<wbr>