HTML ID & Classes

In HTML, ID and Class attributes are used to uniquely identify and group elements for styling and scripting.

HTML ID Attribute

The id Attribute assigns a unique identifier to an element, asign no two elements have the same ID.

Syntax


          <element id="yourID">Content</element>
     

Example


          <p id="intro">This is an preface paragraph.</p>
     

Styling with CSS


          #intro {
          color: blue;
          font-size: 18px;
          }
     

HTML Class Attribute

The class Attribute is used to assign multiple elements to the same class for styling or JavaScript functions.

Syntax


          <element class="yourClass">Content</element>
     

Example


          <p class="highlight">This is a highlight paragraph.</p>
          <p class="highlight">This is another highlight paragraph.</p>
       

Styling with CSS


          .highlight {
          color: white;
          font-weight: bold;
          }
       

Differences Between ID & Class

Feature ID Class
Uniqueness      Can be used only once per element.      Can be used on multiple elements.
Selectors #id .class
Operation Identifies a specific/Unique element. Groups elements with the same style.

HTML ID & Class

In HTML, the ID and Class attributes play a important part in organizing, styling, and scripting elements, enabling developers/inventors to target them effectively.

1️ HTML ID Attribute

The ID Attribute provides a distinct identifier for an HTML element, asign that no two elements can have the same ID.

crucial Features of ID

Syntax


          <element id="uniqueID">Content</element>
     

Example


          <p id="introduction">This is an introduction paragraph.</p>
     

Styling ID with CSS


          #heighligh {
          color: blue;
          font-size: 18px;
          font-weight: bold;
          }
     

Using ID in JavaScript


         document.getElementById("introduction").innerHTML = " streamlined Text!";
     

2️ HTML Class Attribute

The class Attribute allows for the grouping of multiple elements, simplifying the process of styling and scripting.

Crucial Features of Class