HTML ID And Class

HTML ID And Class

You can use both an HTML ID and class in one HTML element. An ID is a unique identifier for an element and should be used only once on a page, while a class can be used multiple times. To apply both an ID and class to an element, you can write the ID selector first, followed by a period (.) and the class selector:

ID:

An HTML ID is a unique identifier that is used to specify a specific element on a web page. The ID is defined by the id attribute and can be used in CSS and JavaScript to select and manipulate the element.

Here’s an example of using an ID in HTML:

<h1 id="page-title">Welcome to My Website</h1>

In this example, the heading element has an ID of “page-title”. This ID can be used in CSS to style the heading specifically:

css
#page-title {
color: blue;
font-size: 36px;
}

And in JavaScript, it can be used to access and manipulate the element:

javascript
const pageTitle = document.getElementById("page-title");
pageTitle.innerHTML = "New Page Title";

It’s important to note that each ID on a page should be unique, as using the same ID multiple times can cause unexpected behavior in CSS and JavaScript.

For Regular Off – Campus Job, Updates Join:- Click Here

Class

An HTML class is a way to group similar elements together in HTML and assign them a common style in CSS. A class can be applied to multiple elements on a page, unlike an ID which should be unique. The class is defined by the class attribute.

Here’s an example of using a class in HTML:

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

In this example, two paragraphs have the class “highlight”. This class can be used in CSS to style both paragraphs similarly:

css
.highlight {
background-color: yellow;
font-weight: bold;
}

And in JavaScript, it can be used to access and manipulate all elements with the class:

javascript
const highlights = document.getElementsByClassName("highlight");
for (let i = 0; i < highlights.length; i++)
{
highlights[i].innerHTML = "This is a modified highlight.";
}

Using classes in HTML allows you to separate the structure of your content from the presentation of your content, making it easier to maintain and update your website.

Different between HTML ID And Class

HTML ID:

  1. Unique: An ID must be unique within a web page, meaning that no two elements can have the same ID.
  2. Selector: An element with an ID can be selected using the “#” symbol followed by the ID name in CSS. For example, if an element has an ID of “header”, it can be selected using “#header” in CSS.
  3. Style: An ID can be used to apply a unique style to a single element on a web page.
  4. Usage: IDs are used when a unique style is needed for a single, specific element on a web page.

HTML Class:

  1. Non-Unique: A class can be used on multiple elements on a web page, meaning that several elements can have the same class.
  2. Selector: An element with a class can be selected using the “.” symbol followed by the class name in CSS. For example, if an element has a class of “highlight”, it can be selected using “.highlight” in CSS.
  3. Style: A class can be used to apply a style to multiple elements on a web page.
  4. Usage: Classes are used when a style needs to be applied to multiple elements on a web page.

class and id

NEXT Topic