HTML Classes

HTML Classes Tutorial

1. Class Attribute in HTML

The 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 <style> tag or in a separate file using the (.) character.

In an HTML document, we can use the same class attribute name with different elements.

2. Defining an HTML class

To create an HTML class, firstly define style for HTML class using <style> tag within <head> section as following example:

3. Example:

  1. <head>  
  2.     <style>  
  3.         .headings  
  4.             color: lightgreen;  
  5.             font-family: cursive;  
  6.             background-color: black; }  
  7.    < style>  
  8. <head>  

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.  

<tag class="ghf"> content tag>

4. Example 1:

 

  1. <html>  
  2. <head>  
  3.     <style>  
  4.         .headings{   
  5.             color: lightgreen;  
  6.             font-family: cursive;  
  7.             background-color: black; }  
  8.     <style>  
  9. <head>  
  10. <body>  
  11. <h1 class="headings">This is first headingh1>  
  12. <h2 class="headings">This is Second headingh2>  
  13. <h3 class="headings">This is third headingh3>  
  14. <h4 class="headings">This is fourth headingh4>  
  15. <body>  
  16. <html>  

5. Another Example with different class name

Let's use a class name "Fruit" with CSS to style all elements.

 
  1. <style>    
  2. .fruit {    
  3.     background-color: orange;    
  4.     color: white;    
  5.     padding: 10px;    
  6. }     
  7. style>    
  8.     
  9. <h2 class="fruit">Mangoh2>    
  10. <p>Mango is king of all fruits.p>    
  11.     
  12. <h2 class="fruit">Orangeh2>    
  13. <p>Oranges are full of Vitamin C.p>    
  14.     
  15. <h2 class="fruit">Appleh2>    
  16. <p>An apple a day, keeps the Doctor away.p>  

Here you can see that we have used the class name "fruit" with (.) to use all its elements.

 

6. Class Attribute in JavaScript

You can use JavaScript access elements with a specified class name by using the getElementsByClassName() method.

7. Example:

Let's hide all the elements with class name "fruit" when the user click on the button.

 
  1. <!DOCTYPE html>    
  2. <html>    
  3. <body>    
  4.     
  5. <h2>Class Attribute with JavaScript</h2>    
  6. <p>Click the button, to hide all elements with the class name "fruit", with JavaScript:</p>    
  7.     
  8. <button onclick="myFunction()">Hide elements</button>    
  9.     
  10.     
  11. <h2 class="fruit">Mango</h2>    
  12. <p>Mango is king of all fruits.</p>    
  13.     
  14. <h2 class="fruit">Orange</h2>    
  15. <p>Oranges are full of Vitamin C.</p>    
  16.     
  17. <h2 class="fruit">Apple</h2>    
  18. <p>An apple a day, keeps the Doctor away.</p>    
  19.     
  20. <script>    
  21. function myFunction() {    
  22.   var x = document.getElementsByClassName("fruit");    
  23.   for (var i = 0; i < x.length; i++) {    
  24.     x[i].style.display = "none";    
  25.   }    
  26. }    
  27. </script>    
  28.     
  29. </body>    
  30. </html>    

8. Multiple Classes

You can use multiple class names (more than one) with HTML elements. These class names must be separated by a space.

9. Example:

Let's style elements with class name "fruit" and also with a class name "center".

 
  1. <html>    
  2. <style>    
  3. .fruit {    
  4.     background-color: orange;    
  5.     color: white;    
  6.     padding: 10px;    
  7. }     
  8.     
  9. .center {    
  10.     text-align: center;    
  11. }    
  12. style>    
  13. <body>    
  14.     
  15. <h2>Multiple Classesh2>    
  16. <p>All three elements have the class name "fruit". In addition, Mango also have the class name "center", which center-aligns the text.p>    
  17.     
  18. <h2 class="fruit center">Mangoh2>    
  19. <h2 class="fruit">Orangeh2>    
  20. <h2 class="fruit">Appleh2>    
  21.     
  22. <body>    
  23. <html>    

You can see that the first element

belongs to both the "fruit" class and the "center" class.

 

 

10. 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.

11. Example:

  1. <!DOCTYPE html>    
  2. <html>    
  3. <style>    
  4. .fruit {    
  5.   background-color: orange;    
  6.   color: white;    
  7.   padding: 10px;    
  8. }     
  9. </style>    
  10. <body>    
  11. <h2>Same Class with Different Tag</h2>    
  12. <h2 class="fruit">Mango</h2>    
  13. <p class="fruit">Mango is the king of all fruits.</p>    
  14. </body>    
  15. </html>