Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. HTML Tag Dictionary
  3. id / class

id / class

id and class are global attributes that can be applied to almost all HTML elements. id gives an element a unique name to identify it, while class gives a name used to group multiple elements together. You use them to apply CSS styles or manipulate elements with JavaScript.

Syntax

<!-- Specifying an id -->
<div id="header">...</div>

<!-- Specifying a class -->
<p class="text-red">...</p>

<!-- Specifying multiple classes (space-separated) -->
<p class="text-large text-bold">...</p>

<!-- Specifying both id and class -->
<section id="about" class="section-wrapper">...</section>

Attribute list

AttributeDescription
idSpecifies a unique name that identifies the element. The same id cannot be used on more than one element per page.
classSpecifies the class name(s) the element belongs to. Multiple classes can be listed, separated by spaces. The same class name can be used on multiple elements.

Usage in CSS

SelectorDescription
#id-namePrefix the id with # to target it. Example: #header { background: #fff; }
.class-namePrefix the class with . to target it. Example: .text-red { color: red; }

Sample code

<style>
  /* id selector (#) */
  #main-title {
    font-size: 2rem;
    color: #333;
  }

  /* class selector (.) */
  .highlight {
    background-color: yellow;
  }

  /* Apply styles to multiple elements sharing the same class */
  .btn {
    padding: 8px 16px;
    border-radius: 4px;
  }
  .btn-primary {
    background-color: blue;
    color: white;
  }
</style>

<!-- Uniquely identified by id -->
<h1 id="main-title">Main Title</h1>

<!-- Apply styles to multiple elements using a class -->
<p>Regular text and <span class="highlight">highlighted text</span>.</p>

<!-- Combining multiple classes -->
<button class="btn btn-primary">Submit</button>
<button class="btn">Cancel</button>

Result

The heading gets a larger font size and color. The element with the highlight class displays a yellow background. Both buttons share the btn style, and the submit button additionally gets a blue background with white text.

Notes

An id must be unique within a page. Using the same id on more than one element is a violation of the HTML specification. If ids are duplicated, document.getElementById() in JavaScript will only return the first matching element.

A class name can be reused as many times as needed. You can apply CSS to all elements sharing a class name at once, or use document.querySelectorAll(".class-name") in JavaScript to select and manipulate them together.

Names cannot start with a digit and cannot contain spaces. When joining multiple words, use a hyphen (e.g., main-title) or an underscore (e.g., main_title).

Browser compatibility

Chrome Chrome
49+
Supported in all versions
Firefox Firefox
57+
31 ×
30
29
28
27
26
23
22
16
6
5
4
3.5
3
2
1
↑ Partial support
Safari Safari
18+
Supported in all versions
Edge Edge
80+
11 or earlier ×
IE IE
11 or earlier ×
Opera Opera
48+
14 or earlier ×
iOS Safari iOS Safari
18+
Supported in all versions
Android Browser Android Browser
37+
4 or earlier ×
Chrome Android Chrome Android
36+
17 or earlier ×
Firefox Android Firefox Android
79+
31 ×
30
29
28
27
26
22
9
8
7
6
5
4
↑ Partial support
3 or earlier ×

If you find any errors or copyright issues, please .