How to use The class Attribute in HTML?
The class attribute specifies one or more class names for an HTML element.
The class name can be used by CSS and JavaScript to perform certain tasks for elements with the specified class name.
HTML elements can have more than one class name each class name must be separated by a space.
To select elements in CSS with a specific class write a period (.) character followed by the name of the class.
E.g:
<style> .name { background-color: red; color: white; padding: 20px; } </style> </head> <body> <h2>class Attribute</h2> <p>Use CSS to style elements with the class name "name":</p> <h2 class="name">John</h2> <p>John is a Doctor.</p> <h2 class="name">Michel</h2> <p>Michel is a Lawyer.</p> </body> |
class Attribute in JavaScript
JavaScript can access elements with a specified class name by using the getElementsByClassName() method.
E.g:
<script> function myFunction() { var x = document.getElementsByClassName("name"); for (var i = 0; i < x.length; i++) { x[i].style.display = "none"; } } </script> |
HTML
Ayesha
Tech writer at newsandstory
Total 831 views