HTML class Attribute

The HTML class attribute is used to specify a class for an HTML element.
Multiple HTML elements can share the same class.
Using The class Attribute
The class attribute is often used to point to a class name in a style sheet. It can also be used by a JavaScript to access and manipulate elements with the specific class name.
In the following example we have three <div> elements with a class attribute with the value of "Animal". All of the three <div> elements will be styled equally according to the .Animal style definition in the head section:
Example
<!DOCTYPE html>
<html>
<head>
<style>
.Animal{
background-color: hsl(198, 87%, 50%);
color: white;
border: 2px solid black;
margin: 20px;
padding: 20px;
}
</style>
</head>
<body>
<div class="Animal">
<h2>Dog</h2>
<p>The dog is a domesticated descendant of the wolf.</p>
</div>
<div class="Animal">
<h2>Donkey</h2>
<p>The donkey has been used as a working animal for at least 5000 years.</p>
</div>
<div class="Animal">
<h2>Monkey</h2>
<p>It is thought the New World monkeys started as a drifted "Old World monkey" group</p>
</div>
</body>
</html>

In the following example we have two <span> elements with a class attribute with the value of "text". Both <span> elements will be styled equally according to the .text style definition in the head section:
Example
<!DOCTYPE html>
<html>
<head>
<style>
.text {
font-size: 120%;
color: hsl(129, 92%, 46%);
}
</style>
</head>
<body>
<h1>My <span class="text">name</span>is XXX</h1>
<p>This is some <span class="text">notes</span></p>
</body>
</html>

The Syntax For Class
To create a class; write a period (.) character, followed by a class name. Then, define the CSS properties within curly braces {}:
Example
Create a class named “Animal”:
<!DOCTYPE html>
<html>
<head>
<style>
.city {
background-color: hsl(148, 88%, 44%);
color: white;
padding: 10px;
}
</style>
</head>
<body>
<h2>The class Attribute</h2>
<p>Use CSS to style elements with the class name "Animal":</p>
<h2 class="city">Dog</h2>
<p>Dog is a animal.</p>
<h2 class="city">Cat</h2>
<p>This cat eyes are too good.</p>
<h2 class="city">Horse</h2>
<p>Horse is used for riding.</p>
</body>
</html>

No comments:
Post a Comment