Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

Web Development - Classes and IDs


HTML Classes and IDs

Classes and IDs allow you to give names to HTML elements. They help you style specific parts of a page with CSS or access them with JavaScript.


The class Attribute

The class attribute is used to define a group of elements with the same name. You can style all elements with the same class using CSS.

Example

Using class in HTML:

<p class="intro">This is an introductory paragraph.</p>
<p class="intro">This is another paragraph with the same class.</p>
Try it Yourself »

You can then target this class in CSS using a dot (.):

Example

Styling a class with CSS:

.intro {
  color: blue;
  font-size: 20px;
}
Try it Yourself »

Multiple Classes

You can assign multiple classes to the same element by separating them with spaces.

Example

Using multiple classes:

<p class="intro highlight">This paragraph has two classes.</p>
Try it Yourself »

This allows flexible styling by combining different class rules.


The id Attribute

The id attribute is used to uniquely identify a single element. Each id must be unique within a page.

Example

Using id in HTML:

<p id="main">This paragraph has a unique ID.</p>
Try it Yourself »

In CSS, IDs are targeted using a hash symbol (#):

Example

Styling an ID with CSS:

#main {
  color: red;
  text-align: center;
}
Try it Yourself »

Difference Between Class and ID

A class can be used on multiple elements. An id is used on one element only.

Example

Comparing class and id:

<p class="highlight">This uses a class.</p>
<p id="unique">This uses an ID.</p>

Example CSS

.highlight { color: green; }
#unique { color: orange; }
Try it Yourself »

Using Classes and IDs in the Same Page

You can use both class and id in the same page to style different parts or to interact with JavaScript.

Example

Combining class and id:

<div id="main-content" class="content-box">
  <h2>Welcome!</h2>
  <p>This is the main content section.</p>
</div>
Try it Yourself »

Using IDs for Bookmarks

The id attribute can also be used as a link target (bookmark) within the same page.

Example

Using id for bookmarks:

<a href="#section2">Go to Section 2</a>

<h2 id="section2">Section 2</h2>
<p>Welcome to section 2!</p>
Try it Yourself »

JavaScript and IDs

IDs are also useful when working with JavaScript. You can use document.getElementById() to access an element by its ID.

Example

Using an ID with JavaScript:

<p id="demo">Hello World!</p>
<button onclick="changeText()">Click me</button>

<script>
function changeText() {
  document.getElementById("demo").innerHTML = "You clicked the button!";
}
</script>
Try it Yourself »

Best Practices

  • Use class for reusable styles.
  • Use id for unique elements.
  • Do not use the same id on more than one element.
  • Use meaningful names (e.g., header, main-content).

Next, you'll learn about HTML Forms - how to collect user input with text fields, buttons, and checkboxes.

Next » HTML Forms


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.