HTML Classes
In HTML, a class is a way to specify a group of elements that share a common attribute. The class attribute is used to assign one or more classes to an HTML element.
To define a class, you can use the class attribute in the opening tag of the HTML element. The syntax for defining a class is as follows:
Where tagname is the HTML tag of the element and classname is the name of the class you want to assign.
For example, to define a class called "highlight" for a paragraph element, you would use the following code:
You can also assign multiple classes to an HTML element by separating them with a space, like this:
Using classes makes it easier to apply styles to groups of elements. You can define a style for a class in CSS and then apply that style to any element with that class.
Here's an example of how you can use classes to style a group of elements:
<style>
.highlight {
background-color: yellow;
}
.bold {
font-weight: bold;
}
</style>
<p class="highlight bold">This text will be highlighted and bold.</p>
<p class="highlight">This text will be highlighted.</p>
<p class="bold">This text will be bold.</p>
In this example, we define two classes in CSS (highlight and bold) and then apply them to various elements. The first paragraph element will be highlighted and bold, the second will be highlighted, and the third will be bold.