The Wayback Machine - https://web.archive.org/web/20240914143118/https://www.geeksforgeeks.org/html-class-attribute/
Open In App

HTML Class Attribute

Last Updated : 28 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The HTML class attribute is used to assign one or more CSS classes to an HTML element. Classes define styling rules, allowing elements with the same class to share visual properties defined in CSS.

HTML class attribute Supported Tags: It supports all HTML elements.

Syntax

<tag class="classname" />

Examples of HTML Class Attribute

Here is a basic example of an HTML Class Attribute

Using the same HTML class attribute in different tags

The HTML class attribute can be applied to various tags, allowing multiple elements to share a common classification. This enables consistent styling or functionality across different types of elements, enhancing design cohesion and simplifying maintenance.

Example: This example shows the use of the classes in HTML. 

HTML
<!DOCTYPE html>
<html>

<head>
    <style>
        .country {
            background-color: black;
            color: white;
            padding: 8px;
        }
    </style>
</head>

<body>
    <h2 class="country">CHINA</h2>

    <p>
        China has the largest population
        in the world.
    </p>

    <h2 class="country">INDIA</h2>

    <p>
        India has the second largest
        population in the world.
    </p>

    <h2 class="country">UNITED STATES</h2>

    <p>
        United States has the third largest
        population in the world.
    </p>
</body>

</html>

Output:

Explanation

  • In the above example each heading (<h2>) is assigned the class “country” using the class attribute.
  • The CSS selector .country targets multiple elements with the class “country” to apply styling.
  • Styling defined for the “country” class is applied uniformly to all headings tagged with it.
  • Using class attributes ensures consistent styling across headings, simplifying design management.

Using multiple HTML class attribute

The HTML class attribute in HTML allows multiple classes to be applied to an element, separated by spaces. This enables styling and behavior to be defined and shared across multiple elements.

Example: In this example, we will use more than one class.

HTML
<!DOCTYPE html>
<html>

<head>
    <style>
        .country {
            color: white;
            padding: 10px;
        }

        .china {
            background-color: black;
        }

        .india {
            background-color: blue;
        }

        .usa {
            background-color: red;
        }

        center {
            padding: 20px;
        }
    </style>
</head>


<body>
    <center>
        <h2 class="country china">CHINA</h2>
        <h2 class="country india">INDIA</h2>
        <h2 class="country usa">UNITED STATES</h2>
    </center>
</body>

</html>

Output:

classAttributeMultipleClassName

HTML class attribute multiple classes

Explanation:

  • In the above example <h2> elements are assigned the “country” class for shared styling attributes.
  • Additional classes like “china”, “india”, and “usa” provide unique background colors.
  • Classes set background colors to black, blue, and red, with white text and padding for visual contrast.
  • The <center> tag ensures horizontal alignment of content, improving the presentation and readability of the page.

HTML Class Attribute Use Cases

1.What is use of class attribute in HTML?

The class attribute in HTML assigns elements to groups, enabling targeted styling or functionality application via CSS or JavaScript.

2. When to use the class attribute and the id attribute ?

Use class for multiple elements with shared characteristics; use id for unique identification or targeting specific elements individually.

Supported Browser: The browser supported by Class attribute are listed below : 

HTML Class Attribute – FAQs

Can an HTML element have multiple class names?

Yes, an element can have multiple class names. Just separate each class name with a space, like this: <div class=”class1 class2″>.

Can I use the class attribute with JavaScript?

Yes, the class attribute can be used with JavaScript to manipulate elements, for example, by using document.getElementsByClassName('classname') or document.querySelectorAll('.classname') to select elements with a specific class.

How do I add or remove a class from an element using JavaScript?

You can add or remove a class using JavaScript with the classList property, like this: element.classList.add('newClass') or element.classList.remove('oldClass').

Is the class attribute case-sensitive?

No, the class attribute itself is not case-sensitive, but CSS class names are case-sensitive, so you should use consistent capitalization.

Can I use the class attribute on any HTML element?

Yes, the class attribute can be applied to any HTML element, allowing for consistent styling or behavior across different parts of a webpage.



Previous Article
Next Article

Similar Reads

When to use the class attribute and the id attribute ?
The class attribute is used when multiple HTML elements share a common style or behaviour, allowing the application of a shared style to several elements. It promotes code reusability and is suitable for elements with similar characteristics. The id attribute is utilized when a unique identifier is needed for a specific HTML element. This identifie
1 min read
How href attribute is different from src attribute in HTML ?
In HTML5, the href and src attributes play distinct roles in defining paths or URLs, each associated with specific HTML elements. The href attribute, commonly found in an anchor (&lt;a&gt;) elements, points to the destination of hyperlinks, facilitating navigation. The src attribute, used with elements like &lt;img&gt; and &lt;script&gt;, specifies
1 min read
How to use the target attribute and the rel attribute in the &lt;a&gt; Tag ?
The target and rel attributes in the &lt;a&gt; (anchor) tag is used to control the behaviour of hyperlinks, specifying how the linked content should be opened and establishing relationships between the current and linked documents. Here's how to use these attributes: target Attribute:The target attribute determines where the linked content will be
2 min read
What is use of class attribute in HTML?
The "class" attribute in HTML is used to assign one or more class names to an element, allowing developers to apply styling or behavior to multiple elements with a single identifier. Unlike the "id" attribute, which must be unique within the document, the "class" attribute can be used to group elements that share similar characteristics or function
1 min read
HTML | &lt;html&gt; xmlns Attribute
The HTML &lt;html&gt; xmlns Attribute is used to specify the xml namespace for a document. Important Note: This attribute is mainly required in XHTML, not valid in HTML 4.01, and optional in HTML 5. Syntax: &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; Attribute Values: https://www.geeksforgeeks.org/ It defines the namespace to use (for XHTML d
1 min read
How to select elements not having a certain class or attribute in CSS ?
"Is it possible to choose elements that do not have a specific class or attribute? or "Is it possible for a CSS selector selecting elements not having a certain class or attribute". It is a query that we are going to discuss, the answer to that particular problem statement is yes, and we will discuss how to achieve it. In this article, we will try
3 min read
Why React uses className over class attribute ?
To all the regular DOM and SVG elements like &lt;button&gt;, &lt;li&gt;, &lt;a&gt;, &lt;div&gt;, etc., if we want to apply the CSS classes, we use the className attribute instead of class in React. That's why it warns you every time when you mistakenly write class instead of className. There are very few scenarios where the DOM property for a given
3 min read
HTML | scoped Attribute
The HTML scoped attribute is a boolean attribute that is used to specify that the styles only apply to this element's parent element and that element's child elements (not the entire document).Note : It is a deprecated attribute and it is not supported by HTML5. Supported tag: style Example: C/C++ Code &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head
1 min read
HTML | &lt;th&gt; valign Attribute
The HTML &lt;th&gt; valign Attribute is used to specify the vertical alignment of text content in a header cell. It is not supported by HTML 5. Syntax: &lt;th valign="top | middle | bottom | baseline"&gt; Attribute Value: top: It sets the table header content to top-align. middle: It sets the table header content to middle-align. bottom: It sets th
1 min read
HTML | &lt;col&gt; align Attribute
The HTML &lt;col&gt; align Attribute is used to set the horizontal alignment of text content inside the col element. It is not supported by HTML 5. Syntax: &lt;col align="left | right | center | justify | char"&gt; Attribute Values: left: It sets the text left-align. right: It sets the text right-align. center: It sets the text center-align. justif
1 min read
HTML | poster Attribute
The HTML poster Attribute is used to display the image while video downloading or when the user clicks the play button. If this image not set then it will take the first frame of video as a poster image.Applicable: It is mainly used in &lt;video&gt; element. Syntax: &lt;video poster="URL"&gt; Attribute Values: It contains a single value URL which s
1 min read
HTML | &lt;area&gt; target Attribute
The HTML &lt;area&gt; target Attribute is used to specify where to open the linked document. It works only when href attribute is present in the area elements. Syntax: &lt;area target="_blank|_self|_parent|_top|framename"&gt; Attribute Values: _blank: It opens the link in a new window._self: It is the default value. It opens the linked document in
1 min read
HTML | &lt;select&gt; autocomplete Attribute
The HTML &lt;select&gt; autocomplete attribute is used to specify that the form has autocompleted on or off. When the autocomplete attribute is set to on the browser will automatically complete the values based on which the user entered before. Syntax: &lt;select autocomplete="on | off"&gt; Attribute Values: on: It is the default value. When the au
1 min read
HTML &amp; CSS | Tabindex attribute &amp; Navigation bars
The tabindex attribute specifies the tab order of an element. "tab" button is used for navigation. The tabindex content attribute allows users to control whether an element is supposed to be focusable, whether it is supposed to be reachable using sequential focus navigation, and what is to be the relative order of the element for the purposes of se
2 min read
HTML | &lt;table&gt; bgcolor Attribute
The HTML &lt;table&gt; bgcolor Attribute is use to specify the background color of a table.Syntax: &lt;table bgcolor="color_name | hex_number | rgb_number"&gt; Attribute Values: color_name: It sets the text color by using the color name. For example "red".hex_number: It sets the text color by using the color hex code. For example "#0000ff".rgb_numb
1 min read
HTML | srcdoc Attribute
The HTML srcdoc attribute is used to specify the HTML content of the page to show in the inline frame. This attribute is anticipated to be used beside the sandbox and seamless attributes. If a browser supports the srcdoc attribute, it'll override the content laid out in the src attribute (if present). If a browser doesn't support the srcdoc attribu
1 min read
HTML &lt;video&gt; crossorigin Attribute
The HTML crossorigin attribute is used in the &lt;video&gt; element that supports a CORS request when we tried to fetch out the video files from the third-party servers or from another domain. Syntax: &lt;video crossorigin="anonymous | use-credentials"&gt; Attribute Values: anonymous: It has a default value. It defines a CORS request that will be s
1 min read
HTML | Marquee truespeed attribute
If the scrolldelay value less then 60 and truespeed mode is on then the value of scrolldelay is not effecting the scrolling. If the scrolldelay value more then 60 and truespeed mode is on then the truespeed is not effecting the scrolling. So, basically it is used to set scrollDelay attribute should be taken as an exact value or not.Syntax: &lt;marq
1 min read
What is the significance of adding autocomplete attribute in HTML Form ?
The HTML &lt;form&gt; autocomplete attribute is used to specify that the form has autocompleted on or off value. When the autocomplete attribute is set to "on", the browser will automatically complete the values based on which the user entered before. Syntax: &lt;form autocomplete="on|off"&gt; Attribute values: on: It has a default value. When the
1 min read
HTML | &lt;td&gt; abbr Attribute
The HTML &lt;td&gt; abbr attribute is used to specify the shorter version of the content. This attribute is not supported by HTML 5. It does not describe any visual effect but it is used by screen readers. Syntax: &lt;td abbr="text"&gt; Attribute Values: text: It contains the short description of cell content. Note : This attribute is not supported
1 min read
HTML reversed Attribute
It is a Boolean Attribute and is used to order the list in Descending Order(9, 8, 7, 6 .....) instead of ascending order(1, 2, 3 ....). Syntax: &lt;ol reversed&gt; &lt;li&gt; Content... &lt;/li&gt; &lt;li&gt; Content... &lt;/li&gt; ... &lt;li&gt; Content... &lt;/li&gt;&lt;/ol&gt;Applicable: &lt;ol&gt; Attributes: It includes single attributes rever
1 min read
HTML onsearch Event Attribute
This onsearch attribute works when a user presses the ENTER button or clicks on the x button. This event works with &lt;input&gt; element. Supported Tags: &lt;input type="search"&gt;Syntax: &lt;element onsearch="script"&gt;Attribute: This event contains single attribute script which contains the script to be run when event call. Example: C/C++ Code
1 min read
HTML oninput Event Attribute
This attribute works when it gets user input value. This attribute mainly fires when the user change the value of &lt;input&gt; and &lt;textarea&gt; element. This attribute is quite similar to onchange attribute but the basic difference is that the oninput event attribute occurs immediately when the value of the element changes while the onchange a
1 min read
HTML | onmouseup Event Attribute
This attribute fires when a mouse button is released over the element. The order of events occur related to the onmouseup event. onmousedownonmouseuponclick Supported Tags: All HTML elements, EXCEPT: &lt;base&gt; &lt;bdo&gt;&lt;br&gt;&lt;head&gt;&lt;html&gt;&lt;iframe&gt;&lt;meta&gt;&lt;param&gt;&lt;script&gt;&lt;style&gt; &lt;title&gt; Syntax:
1 min read
HTML | onwheel Event Attribute
This attribute works when the wheel of pointer device is rolled up or down over an element. The onwheel attribute also works when the user scrolls or zooms on an element by using mouse or touchpad. Supported Tags: It supports all HTML elements. Syntax: &lt;element onwheel = "script"&gt; Attribute Value: This attribute contains single value script a
1 min read
HTML | oncontextmenu Event Attribute
This attribute works when the user right-clicks on an element to open the context menu.Supported Tags: It supports all HTML elements. Syntax: &lt;element oncontextmenu="script"&gt; Attribute Value: This attribute contains single value script. The script to be run when oncontextmenu attribute called. It is supported by all HTML elements.Note: This a
1 min read
HTML | onfocus Event Attribute
This onfocus attribute works when the element gets focused. This event attribute is mostly used with &lt;input&gt;, &lt;select&gt;, &lt;a&gt; elements. This event attribute is supported by all HTML elements excepts &lt;base&gt;, &lt;bdo&gt;, &lt;br&gt;, &lt;head&gt;, &lt;html&gt;, &lt;iframe&gt;, &lt;meta&gt;, &lt;param&gt;, &lt;script&gt;, &lt;sty
1 min read
HTML | &lt;iframe&gt; width Attribute
The HTML &lt;iframe&gt; width attribute is used to specify the width of the &lt;iframe&gt; in terms of pixels. Syntax: &lt;iframe width="pixels"&gt; Attribute Values: It contains single value pixel which specifies the width of the iframes in terms of pixels. Below example illustrates the use of &lt;iframe&gt; width attribute in HTML: Example: C/C++
1 min read
HTML &lt;a&gt; ping Attribute
The HTML &lt;a&gt; ping Attribute is generally used to either specify a particular URL or a list of URLs that will be notified when the user will click on the hyperlink passed int HTML &lt;a&gt; href Attribute. This is achieved by sending a short HTTP post request to the specified URL as soon as the user clicks on the hyperlink. Syntax : &lt;a href
1 min read
HTML | &lt;input&gt; formtarget Attribute
The HTML &lt;input&gt; formtarget attribute is used to specify the name or a keyword which indicates where to display the response after submitting the form. This attribute overrides the target attribute of &lt;form&gt; element. Syntax: &lt;input formtarget="_blank|_self|_parent|_top|framename"&gt; Attribute Values: _blank: The input response is di
1 min read