Skip to content

HTML Classes

Hridaya edited this page Feb 4, 2021 · 1 revision

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 JavaScript to access and manipulate elements with the specific class name.

For Example, we have three

elements with a class attribute with the value of "city". All of the three
elements will be styled equally according to the .city style definition in the head section.

class="city"

To add multiple classes you write them like this:

class="city main"

Use of The class Attribute in JavaScript

The class name can also be used by JavaScript to perform certain tasks for specific elements.

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