Class Attribute in HTML
A class attribute can be defined within <style> tag or in separate file using the (.) character.
In an HTML document, we can use the same class attribute name with different elements.
Defining an HTML class
To create an HTML class, firstly define style for HTML class using <style> tag within <head> section as following example:
We have define style for a class name "headings", and we can use this class name with any of HTML element in which we want to provide such styling. We just need to follow the following syntax to use it.
Example 1:Test it Now
Another Example with different class name
Let's use a class name "Fruit" with CSS to style all elements.Test it Now
Here you can see that we have used the class name "fruit" with (.) to use all its elements.
Note: You can use class attribute on any HTML element. The class name is case-sensitive.
Let's hide all the elements with class name "fruit" when the user click on the button.Test it Now
You can use multiple class names (more than one) with HTML elements. These class names must be separated by a space.
Let's style elements with class name "fruit" and also with a class name "center".Test it Now
You can see that the first element <h2> belongs to both the "fruit" class and the "center" class.
Same class with Different Tag
You can use the same class name with different tags like <h2> and <p> etc. to share the same style.
Example:Test it Now