How div class and id is useful in HTML ?

The HTML

tag represents a division or section in an HTML document. This tag is used as a container, inside which we can embed any HTML elements such as texts, images, tables, audio, video, etc. The <div> element is a block-level element that provides structure and layout control to web pages.

If we want to style or interact with the

element, we can assign an id or class attribute. These attributes make <div> elements extremely useful for CSS styling and JavaScript manipulation.

Note ? By default, most browsers always add a new line break before and after the

element.

HTML id Attribute

In HTML, using the id attribute (global attribute), we can specify a unique identifier for any HTML element. We cannot assign more than one element with the same id in an HTML document, so the id must be unique within the whole document.

Using this id, we can style or interact with that particular HTML element using CSS and JavaScript. The id attribute is primarily used when you need to target a specific, individual element.

Syntax

Following is the syntax for defining an id to an HTML element ?

<div id="mypara1">Welcome to Tutorialspoint</div>

We can access the id attribute in CSS using the #id selector. Following is the syntax ?

#mypara1 {
   color: red;
}

HTML class Attribute

In HTML, the class attribute (global attribute) can be used to specify a class for any HTML element. We can assign the same class to multiple HTML elements. The class name is case-sensitive.

We can style or manipulate elements with the specific class name using CSS and JavaScript. The class attribute is ideal when you want to apply the same styles to multiple elements.

Syntax

Following is the syntax for defining a class to an HTML element ?

<h1 class="myheading1">Simply easy learning</h1>

We can access the class attribute in CSS using the .class selector. Following is the syntax ?

.myheading1 {
   color: yellowgreen;
}
Class vs ID Attributes ID Attribute ? Must be unique ? CSS: #myId ? One element only ? High CSS specificity ? JavaScript: getElementById Class Attribute ? Can be reused ? CSS: .myClass ? Multiple elements ? Lower CSS specificity ? JS: getElementsByClassName

Basic Div Elements Without Attributes

Example

In the following example, we are creating 4 <div> elements without assigning them class and id attributes ?

<!DOCTYPE html>
<html>
<head>
   <title>DIV in HTML</title>
   <style>
      div {
         border: solid 1px black;
         padding: 10px;
         margin: 5px;
         font-family: Arial, sans-serif;
      }
   </style>
</head>
<body>
   <div>
      <p>Tutorialspoint</p>
   </div>
   <div>
      <p>simply</p>
   </div>
   <div>
      <p>easy</p>
   </div>
   <div>
      <p>learning</p>
   </div>
</body>
</html>

The output shows four <div> elements, each with a black border. The browser adds a new line break before and after each <div> element ?

???????????????????
? Tutorialspoint  ?
???????????????????
???????????????????
? simply          ?
???????????????????
???????????????????
? easy            ?
???????????????????
???????????????????
? learning        ?
???????????????????

Using Class Attribute with Div Elements

Example

In the example below, we are assigning a class attribute to every <div> element in the HTML document and styling them using CSS ?

<!DOCTYPE html>
<html>
<head>
   <title>DIV with Class Attributes</title>
   <style>
      div {
         border: solid 2px black;
         padding: 15px;
         margin: 10px;
         font-family: Arial, sans-serif;
         text-align: center;
      }
      .mydiv1 {
         background-color: lightcoral;
         color: white;
      }
      .mydiv2 {
         background-color: lightgreen;
         color: darkgreen;
      }
      .mydiv3 {
         background-color: lightseagreen;
         color: white;
      }
      .mydiv4 {
         background-color: lightslategrey;
         color: white;
      }
   </style>
</head>
<body>
   <div class="mydiv1">
      <p>Tutorialspoint</p>
   </div>
   <div class="mydiv2">
      <p>simply</p>
   </div>
   <div class="mydiv3">
      <p>easy</p>
   </div>
   <div class="mydiv4">
      <p>learning</p>
   </div>
</body>
</html>

In the above program, we styled the div elements using their class names in the <style> tags. Each div has a unique background color applied through its class ?

???????????????????????
?   Tutorialspoint    ? (light coral background)
???????????????????????
???????????????????????
?      simply         ? (light green background)
???????????????????????
???????????????????????
?       easy          ? (light sea green background)
???????????????????????
???????????????????????
?     learning        ? (light slate grey background)
???????????????????????

Using ID Attribute with Div Elements

Example

Here, we are adding CSS to the <div> elements with the help of their id attributes ?

<!DOCTYPE html>
<html>
<head>
   <title>DIV with ID Attributes</title>
   <style>
      div {
         border: solid 2px black;
         padding: 15px;
         margin: 10px;
         font-family: Arial, sans-serif;
         text-align: center;
      }
      #mydiv1 {
         background-color: lightslategrey;
         color: white;
         font-weight: bold;
      }
      #mydiv2 {
         background-color: lightseagreen;
         color: white;
         font-style: italic;
      }
      #mydiv3 {
         background-color: lightgreen;
         color: darkgreen;
         text-decoration: underline;
      }
      #mydiv4 {
         background-color: lightcoral;
         color: white;
         text-transform: uppercase;
      }
   </style>
</head>
<body>
   <div id="mydiv1">
      <p>Tutorialspoint</p>
   </div>
   <div id="mydiv2">
      <p>simply</p>
   </div>
   <div id="mydiv3">
      <p>easy</p>
   </div>
   <div id="mydiv4">
      <p>learning</p>
   </div>
</body>
</html>

In the above program, we styled the div elements using their ID attributes in the <style> tags. Each div has unique styling applied through its individual id ?

???????????????????????
?   Tutorialspoint    ? (grey background, bold text)
???????????????????????
???????????????????????
?      simply         ? (sea green background, italic)
???????????????????????
???????????????????????
?       easy          ? (green background, underlined)
???????????????????????
???????????????????????
?     LEARNING        ? (coral background, uppercase)
???????????????????????

JavaScript Interaction with Div Class and ID

Example

Following example demonstrates how JavaScript can interact with <div> elements using class and id attributes ?

<!DOCTYPE html>
<html>
<head>
   <title>JavaScript with Div Class and ID</title>
   <style>
      .highlight { background-color: yellow; padding: 10px; margin: 5px; }
      #special { border: 3px solid red; background-color: lightblue; padding: 10px; }
      button { margin: 5px; padding: 8px 16px; font-family: Arial, sans-serif; }
   </style>
</head>
<body style="font-family: Arial, sans-serif; padding: 10px;">
   <div class="highlight">This div has a class 'highlight'</div>
   <div class="highlight">This div also has the same class</div>
   <div id="special">This div has an ID 'special'</div>
   
   <button onclick="changeClass()">Change Class Elements</button>
   <button onclick="changeId()">Change ID Element</button>
   
   <script>
      function changeClass() {
         var elements = document.getElementsByClassName("highlight");
         for (var i = 0; i < elements.length; i++) {
            elements[i].style.backgroundColor = "lightgreen";
         }
      }
      
      function changeId() {
         document.getElementById("special").innerHTML = "Content changed via ID!";
      }
   </script>
</body>
</html>

This example shows how JavaScript can target multiple elements by class name or a single element by its unique ID. The class button changes all elements with class "highlight", while the ID button targets only the element with ID "special".

Key Differences Between Class and ID

Updated on: 2026-03-16T21:38:54+05:30

585 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements