HTML class
Created By: chatGPT
In HTML, a class is used to define a group of elements that share the same style or attributes. You can assign a class to any HTML element using the
class
attribute. This allows for easier manipulation of styles and scripts.<div class="container">
<h1 class="title">Hello World</h1>
<p class="description">This is a simple example of using classes in HTML.</p>
</div>
You can define the styles for these classes using CSS. By targeting the class name in your CSS, you can apply specific styles to all elements with that class.
.container {
margin: 20px;
border: 1px solid #ccc;
}
.title {
font-size: 24px;
color: blue;
}
description {
font-size: 16px;
color: grey;
}
In JavaScript, you can also select elements by their class name using methods like
getElementsByClassName
or querySelectorAll
. This is useful for dynamically changing the content or style of elements.var elements = document.getElementsByClassName('description');
for (var i = 0; i < elements.length; i++) {
elements[i].textContent = 'Updated Content';
}