HTML ClassesClass Attribute in HTMLThe HTML class attribute is used to specify a single or multiple class names for an HTML element. The class name can be used by CSS and JavaScript to do some tasks for HTML elements. You can use this class in CSS with a specific class, write a period (.) character, followed by the name of the class for selecting elements. A class attribute can be defined within |
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.
Let's use a class name "Fruit" with CSS to style all elements.
Here you can see that we have used the class name "fruit" with (.) to use all its elements.
You can use JavaScript access elements with a specified class name by using the getElementsByClassName() method.
Let's hide all the elements with class name "fruit" when the user click on the button.
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".
You can see that the first element
You can use the same class name with different tags like
etc. to share the same style.