In HTML, the class
attribute is used to define a class for an HTML element. The class can be used to apply CSS styles to multiple elements at once, or to select and manipulate elements using JavaScript.
Here’s an example of how to use the class
attribute in HTML:
<p class="important">This is an important paragraph.</p> <p class="normal">This is a normal paragraph.</p>
In this example, we’ve defined two paragraphs, one with the class important
and one with the class normal
. We can then apply CSS styles to all elements with the important
class, like this:
.important { font-weight: bold; color: red; }
This will make all elements with the important
class bold and red.
We can also select and manipulate elements with JavaScript using their class. Here’s an example:
var importantParagraphs = document.querySelectorAll('.important'); for (var i = 0; i < importantParagraphs.length; i++) { importantParagraphs[i].textContent = 'This is REALLY important!'; }
This code selects all elements with the important
class and changes their text content to “This is REALLY important!”.
In summary, the class
attribute in HTML is used to define a class for an HTML element, which can be used to apply CSS styles to multiple elements at once or to select and manipulate elements using JavaScript.