HTML class Attribute

HTML class attribute gives an element one or more class names. Most of the time, the class attribute points to a class in a style sheet. But JavaScript can also use it (through the HTML DOM) to change HTML elements with a certain class.

The class attribute of an HTML element is used to give it a class. The same class can be used by more than one HTML element.

Using Class Attribute

In a style sheet, the class attribute is often used to point to a class name. It can also be used by JavaScript to access and change elements with the same class name.

In the next example, there are three <div> elements that all have a class attribute with the value \”box.\” The.city style definition in the head section will be used to style all three <div> elements in the same way:

Example

<!DOCTYPE html>
<html>
<head>
<style>
.box {
background-color: blue;
color: white;
border: 3px solid orange;
margin: 20px;
padding: 20px;
border-radius:10px;
}
</style>
</head>
<body>

<div class=\”box\”>
<h2>Box 1</h2>
<p>example of class</p>
</div>

<div class=\”box\”>
<h2>Box 2</h2>
<p>example of class</p>
</div>

<div class=\”box\”>
<h2>Box 3</h2>
<p>example of class</p>
</div>

</body>
</html>

 

Output

\"class

Note that the case of the class name is important!

Tip 1: You can use the class attribute on any HTML element.

Tip 2: Our CSS Tutorial has a lot more information about CSS.

How To Write Class

To make a class, write a period (.) followed by the name of the class. Then, put the CSS properties between the curly braces :

Example

<!DOCTYPE html>
<html>
<head>
<style>
.box {
background-color: blue;
color: white;
border: 3px solid orange;
margin: 20px;
padding: 20px;
border-radius:10px;
}
</style>
</head>
<body>

<div class=\”box\”>
<h2>Box 1</h2>
<p>example of class</p>
</div>

<div class=\”box\”>
<h2>Box 2</h2>
<p>example of class</p>
</div>

<div class=\”box\”>
<h2>Box 3</h2>
<p>example of class</p>
</div>

</body>
</html>

 

Output

\"class

Multiple Classes

There can be more than one class for an HTML element.

To define more than one class, put a space between the class names, like this: div class=\”box wraper\”>. All of the classes that were given will be used to style the element.

In the following example, the first h2> element is part of both the city class and the main class, so it will get the CSS styles from both classes:

Example

<!DOCTYPE html>
<html>
<head>
<style>
.box {
background-color: blue;
color: white;
border: 3px solid orange;
margin: 20px;
padding: 20px;
border-radius:10px;
}
</style>
</head>
<body>

<div class=\”box wraper\”>
<h2>Box 1</h2>
<p>example of class</p>
</div>

<div class=\”box anotherclass\”>
<h2>Box 2</h2>
<p>example of class</p>
</div>

<div class=\”box wraper\”>
<h2>Box 3</h2>
<p>example of class</p>
</div>

</body>
</html>

 

Output

\"class

Different Elements Can Use Same Class

The same class name can be pointed to by more than one HTML element.

In the following example, both <h2> and <p> point to the \”box\” class and will have the same style:

Example

<!DOCTYPE html>
<html>
<head>
<style>
.box {
background-color: blue;
color: white;
border: 3px solid orange;
margin: 20px;
padding: 20px;
border-radius:10px;
}
</style>
</head>
<body>

<div class=\”box\”>
<h2 class=\”box\”>Box 1</h2>
<p class=\”box\”>example of class</p>
</div>

<div class=\”box\”>
<h2 class=\”box\”>Box 2</h2>
<p class=\”box\”>example of class</p>
</div>

</body>
</html>

Output

\"Different

Use of class attribute in JavaScript

JavaScript can also use the class name to do certain things for certain elements.

With the getElementsByClassName() method, JavaScript can get to elements with a certain class name:

Example

<!DOCTYPE html>
<html>
<body>

<h2>Use of The class Attribute in JavaScript</h2>

<button onclick=\”classFunction()\”>Hide elements</button>

<div class=\”box\”>
<h2>Box 1</h2>
<p>example of class</p>
</div>

<div class=\”box\”>
<h2>Box 2</h2>
<p>example of class</p>
</div>

<div class=\”box\”>
<h2>Box 3</h2>
<p>example of class</p>
</div>

<script>
function classFunction() {
var x = document.getElementsByClassName(\”box\”);
for (var i = 0; i < x.length; i++) {
x[i].style.display = \”none\”;
}
}
</script>

</body>
</html>

 

Output

\"class

 

Don\’t worry if you don\’t understand the code in the above example.

You can read our HTML JavaScript chapter or our JavaScript Tutorial to learn more about JavaScript.

Related Posts
Introduction to HTML

HTML Introduction HTML is the most common language used to mark up Web pages. What does HTML mean? Hyper Text Read more

What is HTML

HTML stands for Hyper Text Markup Language, which is a programming language used to make web pages and web apps. Read more

HTML Basic Examples

In this chapter, we\'ll show you some simple examples of HTML code. Don\'t worry if we use tags that you Read more

HTML text Editors

Editors for HTML text HTML files are text files, so any text editor can be used to make them. Text Read more

HTML Tags

Learn HTML Tags HTML tags are kind of like keywords that tell a web browser how to format and show Read more

HTML Elements

Elements make up an HTML file. These elements are what make web pages and tell you what content is on Read more

HTML Attributes

Attribute in HTML Attributes in HTML are special words that tell you more about an element. Attributes change the way Read more

HTML Headings

Headings in HTML The <h1> to <h6> tags are used to set up headings in HTML. <h1> shows which heading Read more

Scroll to Top