![]() |
Definition and UsageThe The Browser SupportSyntax
Attribute Values
|
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>
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>
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>
Click the button to add the "mystyle" class to DIV. I am a DIV element
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>
html class attribute |
How to Use of the class attribute in an HTML document - | HTML Global Attributes |
How to add Add multiple classes to one HTML element | HTML 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