elibraryportal Logo

HTML Class

⟼. The class attribute is often used in CSS and JavaScript for some tasks. When you can use the class in CSS , you can use Dot (.) character, followed by the name of the class for selecting elements.

class attribute can be defined within <style> tag or use in separate file .

See in Example:

Output:-

This is first heading use of class elements


See in Example 2:

Output:-

The class Attribute

Use CSS to style elements with the class name "city":

India

India is the capital of New Delhi.

India

India is the Grate Country.

Class Attribute Use in JavaScript

When class name can be used in JavaScript then use getElementsByClassName() method to perform certain tasks for specific elements.

See in Example :

OutPut:-

Click the button to hide all elements with class name "city":

CHINA

China has the largest population in the world.

INDIA

India has the second largest population in the world.

UNITED STATES

United States has the third largest population in the world.

Next TopicHTML ID