HTML class Attribute
HTML class Attribute
The class attribute is used to assign a class name to an HTML element.
It allows you to group elements and apply the same CSS styles or JavaScript actions to multiple elements.
Syntax
-
className→ Name of the class (can be reused on multiple elements)
Example Code
Output Preview
-
First paragraph → Red and bold text
-
Inside the box → Paragraph is red and bold with a yellow background and border
-
Last paragraph → Normal text
Key Points
-
Multiple elements can share the same class.
-
Multiple classes can be assigned to a single element (separated by space):
-
Mainly used for CSS styling and JavaScript manipulation.
The class attribute is essential for consistent styling and dynamic functionality on web pages.
