Home 〉 HTML Tutorials 〉 HTML Class & ID Attribute Explained with Examples | Use Multiple Classes and IDs in One Tag
HTML Class & ID Attribute Explained with Examples | Use Multiple Classes and IDs in One Tag !
Do you know the difference between class and id attributes in HTML?
This blog post explains the 'class' and 'id' attributes in detail, covering syntax, usage, and valid examples. You'll also learn how to assign multiple classes to one HTML tag and why assigning multiple ids is invalid. The post includes internal CSS styling examples to improve web design and structure. It also shares related topics and a helpful Wikipedia link. The detailed blog post on HTML class & id is as follows.
When building websites using HTML, the class and id attributes are essential tools that help you style, identify, and control elements using CSS or JavaScript. These attributes provide a structured way to apply styling or scripts to elements, making your HTML code efficient and organized.
Syntax ✍
<tag class="className">Content</tag>
<tag class="className className1">Content</tag>
Syntax ✍
<tag id="uniqueId">Content</tag>
The 'id' attribute assigns a unique identifier to an HTML element. It must be unique across the HTML document and is often used for styling specific elements or targeting them via JavaScript.
Example 📄
<div class="highlight">Welcome to our HTML tutorial</div>
The 'div' element has a class name 'highlight', which can be used in CSS to apply styles like color or padding to any element with this class.
Example 📄
<p id="intro">This paragraph has a unique ID.</p>
The paragraph uses the 'id' attribute to uniquely identify it for custom styling or scripting.
Example 📄
<div class="box shadow">This box has multiple classes</div>
The 'div' element is using two classes: 'box' and 'shadow'. You can target each class in CSS to apply multiple styles to a single element.
<!DOCTYPE html>
<html>
<head>
<style>
.box {
padding: 20px;
border: 1px solid #ccc;
background-color: #f9f9f9;
}
.shadow {
box-shadow: 2px 2px 8px rgba(0,0,0,0.2);
}
</style>
</head>
<body>
<div class="box shadow">This box has multiple classes</div>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
Example 📄
<div id="main" id="secondary">Invalid Example</div>
This is invalid HTML. An element cannot have more than one 'id'. Only one unique 'id' is allowed per element in the entire HTML document.
Here is how you can style the elements internally using a '<style>' tag:
<!DOCTYPE html>
<html>
<head>
<style>
.highlight {
color: #ffffff;
background-color: #007BFF;
padding: 10px;
font-size: 18px;
border-radius: 5px;
}
#intro {
font-size: 16px;
color: #333333;
margin: 10px 0;
}
</style>
</head>
<body>
<div class="highlight">Welcome to our HTML tutorial</div>
<p id="intro">This paragraph has a unique ID.</p>
</body>
</html>
output 📌
This paragraph has a unique ID.
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
The 'class' and 'id' attributes are essential for applying custom styles and managing your HTML layout effectively. While 'class' allows you to reuse styles across multiple elements, 'id' gives you a way to uniquely target specific content. Remember, you can apply multiple classes but only one id to an element. These attributes, combined with internal CSS, allow developers to build structured, maintainable, and beautiful web pages.
Yes, you can use both on the same element. 'id' must be unique, while 'class' can be shared.
Yes, you can assign multiple classes to an element using space-separated values.
HTML standards require that the 'id' be unique for each element and not repeated.
The 'id' is often used to target and manipulate HTML elements in JavaScript.