Horje
What is HTML class Attribute

Definition and Usage

The class attribute specifies one or more class names for an element.

The class attribute is mostly used to point to a class in a style sheet. However, it can also be used by a JavaScript (via the HTML DOM) to make changes to HTML elements with a specified class.


Browser Support

Syntax

<element class="classname">

Attribute Values

Value Description
classname Specifies one or more class names for an element. To specify multiple classes, separate the class names with a space, e.g. <span class="left important">. This allows you to combine several CSS classes for one HTML element.

Naming rules:

  • Must begin with a letter A-Z or a-z
  • Can be followed by: letters (A-Za-z), digits (0-9), hyphens ("-"), and underscores ("_")

How to Use of the class attribute in an HTML document -

Note that this is an important paragraph. :)
index.html
Example: HTML
<!DOCTYPE html>
<html>
<head>
<style>
h1.intro {
  color: blue;
}

p.important {
  color: green;
}
</style>
</head>
<body>

<h1 class="intro">Header 1</h1>
<p>A paragraph.</p>
<p class="important">Note that this is an important paragraph. :)</p>

</body>
</html>

Output should be:

How to Use of the class attribute in an HTML document -

How to add Add multiple classes to one HTML element

See the Example.
index.html
Example: HTML
<!DOCTYPE html>
<html>
<head>
<style>
h1.intro {
  color: blue;
  text-align: center;
}

.important {
  background-color: yellow;
}
</style>
</head>
<body>

<h1 class="intro important">Header 1</h1>
<p>A paragraph.</p>

</body>
</html>

Output should be:

How to add Add multiple classes to one HTML element

How to use JavaScript to add a yellow background color to the first element with class="example" (index 0)

First div element with class="example". Second div element with class="example". Click the button to add a yellow background color to the first div element with class="example" (index 0).
index.html
Example: HTML
<!DOCTYPE html>
<html>
<body>

<div class="example">First div element with class="example".</div>
<div class="example">Second div element with class="example".</div>

<p>Click the button to add a yellow background color to the first div element with class="example" (index 0).</p>

<button onclick="myFunction()">Try it</button>

<script>
function myFunction() {
  let x = document.getElementsByClassName("example");
  x[0].style.backgroundColor = "yellow";
}
</script>

</body>
</html>

Output should be:

How to use JavaScript to add a yellow background color to the first element with class="example" (index 0)

How to use JavaScript to add the "mystyle" class to an element with id="myDIV"

Click the button to add the "mystyle" class to DIV. I am a DIV element

index.html
Example: HTML
<!DOCTYPE html>
<html>
<head>
<style>
.mystyle {
  margin: 20px;
  width: 300px;
  height: 50px;
  background-color: coral;
  color: white;
  font-size: 25px;
}
</style>
</head>
<body>

<p>Click the button to add the "mystyle" class to DIV.</p>

<button onclick="myFunction()">Try it</button>

<div id="myDIV">
I am a DIV element
</div>
<br>

<script>
function myFunction() {
  document.getElementById("myDIV").classList.add("mystyle");
}
</script>

</body>
</html>

Output should be:

How to use JavaScript to add the "mystyle" class to an element with id="myDIV"




html class attribute

Related Articles
What is HTML accesskey Attribute HTML Global Attributes
What is HTML class Attribute HTML Global Attributes
What is HTML contenteditable Attribute HTML Global Attributes
What is HTML data-* Attributes HTML Global Attributes
What is HTML dir Attribute HTML Global Attributes
What is HTML draggable Attribute HTML Global Attributes
What is HTML enterkeyhint Attribute HTML Global Attributes
What is HTML hidden Attribute HTML Global Attributes
What is HTML id Attribute HTML Global Attributes
What is HTML inert Attribute HTML Global Attributes
What is HTML inputmode Attribute HTML Global Attributes
What is HTML lang Attribute HTML Global Attributes
What is HTML popover Attribute HTML Global Attributes
What is HTML spellcheck Attribute HTML Global Attributes
What is HTML style Attribute HTML Global Attributes
What is HTML tabindex Attribute HTML Global Attributes
What is HTML title Attribute HTML Global Attributes
What is HTML translate Attribute HTML Global Attributes
What are the HTML Global Attributes HTML Global Attributes

Single Articles
How to Use of the class attribute in an HTML document - HTML Global Attributes
How to add Add multiple classes to one HTML elementHTML Global Attributes
How to use JavaScript to add a yellow background color to the first element with class="example" (index 0)HTML Global Attributes
How to use JavaScript to add the "mystyle" class to an element with id="myDIV"HTML Global Attributes

Read Full:
HTML Global Attributes
Type:
Develop
Category:
Web Tutorial
Sub Category:
HTML Global Attributes
Uploaded by:
Admin
Views:
29


Reffered: https://www.w3schools.com/tags/att_global_class.asp