Curriculum
Course: HTML Basic
Login

Curriculum

HTML Basic

HTML Introduction

0/1

HTML Editors

0/1

HTML Elements

0/1

HTML Attributes

0/1

HTML Headings

0/1

HTML Paragraphs

0/1

HTML Styles

0/1

HTML Formatting

0/1

HTML Quotation

0/1

HTML Comments

0/1

HTML Colors

0/1

HTML Favicon

0/1

HTML Page Title

0/1

HTML Block and Inline

0/1

HTML Iframes

0/1

HTML Java Script

0/1

HTML File Paths

0/1

HTML - The Head Element

0/1

HTML Style Guide

0/1

HTML Entities

0/1

HTML Symbols

0/1
Text lesson

HTML class Attribute

Multiple HTML elements can possess the same class.

Using The class Attribute

The class attribute frequently references a class name in a style sheet. Additionally, it can be utilized by JavaScript to access and modify elements with the designated class name.

In the provided example, three <div> elements feature a class attribute set to “city”. Consequently, all three <div> elements will be uniformly styled based on the .city style definition within the head section.

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>London</h2>
  <p>London is the capital of England.</p>
</div>

<div class=”city”>
  <h2>Paris</h2>
  <p>Paris is the capital of France.</p>
</div>

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

</body>
</html>

In this example, two <span> elements each possess a class attribute set to “note”. As a result, both <span> elements will be uniformly styled based on the .note style definition found in the head section.

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: You can apply the class attribute to any HTML element.

Note: The class name is case-sensitive!

The Syntax For Class

To define a class, begin with a period (.) followed by the class name. Then, enclose the CSS properties within 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”>London</h2>
<p>London is the capital of England.</p>

<h2 class=”city”>Paris</h2>
<p>Paris is the capital of France.</p>

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

</body>
</html>

Multiple Classes

HTML elements have the ability to be associated with multiple classes.

To designate multiple classes, delineate the class names with spaces, for instance, <div class=”city main”>. Consequently, the element will be styled according to the rules defined in all specified classes.

In the given example, the initial <h2> element is assigned both the city and main classes, thus inheriting CSS styles from both classes.

Example

<h2 class=”city main”>London</h2>
<h2 class=”city”>Paris</h2>
<h2 class=”city”>Tokyo</h2>

Different Elements Can Share Same Class

Various HTML elements can reference the same class name.

In the provided example, both <h2> and <p> elements are associated with the “city” class, thus inheriting the same style.

Example

<h2 class=”city”>Paris</h2>
<p class=”city”>Paris is the capital of France</p>

Use of The class Attribute in JavaScript

JavaScript can utilize the class name to execute specific tasks on particular elements.

Elements with a specific class name can be accessed by JavaScript using the getElementsByClassName() method.

Example

Click a button to conceal all elements having 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>

Summary of the Chapter

  • The HTML class attribute designates one or multiple class names for an element.
  • Classes are utilized by CSS and JavaScript to target and interact with particular elements.
  • The class attribute is applicable to any HTML element and is case sensitive.
  • Diverse HTML elements can reference the same class name.
  • JavaScript employs the getElementsByClassName() method to retrieve elements with a specified class name.

 

Click to Learn