in , ,

HTML class Attribute

HTML class Attribute
HTML class Attribute

The HTML class attribute is used to determine a class for a HTML element.

Different HTML elements can have a similar class.

The class is an attribute which determines one or more class names for a HTML element.

The class attribute can be used on any HTML element.

The class name can be used by CSS and JavaScript to play out specific tasks for elements with the specified class name.


In this tutorial, you will learn-

Using The class Attribute

The class attribute is frequently used to highlight a class name in a style sheet. It can likewise be used by a JavaScript to access and manipulate elements with the particular class name.

In the accompanying example we have three <div> elements with a class attribute with the value of “city”. The entirety of the three <div> elements will be styled similarly as indicated by the .city style definition in the head area:

Example

<!DOCTYPE html>
<html>
<head>
<style>
.city {
  background-color: tomato;
  color: white;
  border: 2px solid black;
  margin: 20px;
  padding: 20px;
}
</style>
</head>
<body>

<div class="city">
  <h2>Islamabad</h2>
  <p>Islamabad is the capital of Pakistan.</p>
</div>

<div class="city">
  <h2>London</h2>
  <p>London is the capital of England.</p>
</div>

<div class="city">
  <h2>Tokyo</h2>
  <p>Tokyo is the capital of Japan.</p>
</div>

</body>
</html>

In the accompanying example we have two <span> elements with a class attribute with the value of “note”. Both <span> elements will be styled similarly as per the .note style definition in the head segment:

Example

<!DOCTYPE html>
<html>
<head>
<style>
.note {
  font-size: 120%;
  color: red;
}
</style>
</head>
<body>

<h1>My <span class="note">Important</span> Heading</h1>
<p>This is some <span class="note">important</span> text.</p>

</body>
</html>

Tip: The class attribute can be used on any HTML element.

Note: The class name is case sensitive!


The Syntax For Class

To create a class; write a period (.) character, followed by a class name. Then, at that point, characterize the CSS properties inside curly braces {}:

Example
Create a class named “city”:

<!DOCTYPE html>
<html>
<head>
<style>
.city {
  background-color: tomato;
  color: white;
  padding: 10px;
}
</style>
</head>
<body>

<h2 class="city">Islamabad</h2>
<p>Islamabad is the capital of Pakistan.</p>

<h2 class="city">London</h2>
<p>London is the capital of England.</p>

<h2 class="city">Tokyo</h2>
<p>Tokyo is the capital of Japan.</p>

</body>
</html>

Multiple Classes

HTML elements can have a place with more than one class.

To define multiple classes, separate the class names with a space, for example <div class=”city main”> . The elements will be styled by every one of the classes indicated.

In the accompanying example, the first <h2> elements has a place with both the city class and furthermore to the main class, and will get the CSS styles from both of the classes:

Example

<h2 class="city main">Islamabad </h2>
<h2 class="city">London </h2>
<h2 class="city">Tokyo</h2>

Different Elements Can Share Same Class

Different HTML elements can highlight a similar class name.

In the accompanying example, both <h2> and <p> points to the “city” class and will have a similar style:

Example

<h2 class="city">London</h2>
<p class="city">London is the capital of England</p>

Use of The class Attribute in JavaScript

The class name can likewise be used by JavaScript to play out specific tasks for specific elements.

JavaScript can access elements with a particular class name with the getElementsByClassName() method:

Example
Click on a button to hide all elements with the class name “city”:

<script>
function myFunction() {
  var x = document.getElementsByClassName("city");
  for (var i = 0; i < x.length; i++) {
    x[i].style.display = "none";
  }
}
</script>

Chapter Summary

  • The HTML class attribute specifies one or more class names for an element
  • Classes are used by CSS and JavaScript to select and access specific elements
  • The class attribute can be used on any HTML element
  • The class name is case sensitive
  • Different HTML elements can point to the same class name
  • JavaScript can access elements with a specific class name with the getElementsByClassName() method

Related:

HTML Lists

HTML Unordered Lists

HTML Ordered Lists

HTML Other Lists

HTML Block and Inline Elements


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

HTML Blocks

HTML Block and Inline Elements

HTML id Attribute

HTML id Attribute