What is a Class?
A class is an attribute that you can assign to HTML elements to group them together. Classes allow you to apply the same styles to multiple elements and can also be used for JavaScript manipulation.
How to Use Classes
You can define a class in your HTML by adding the class attribute to an element:
<div class="example">This is a div with a class.</div>
In your CSS, you can style the class by using a dot (.) followed by the class name:
.example { background-color: lightblue; }
Examples of Classes
Example 1: Multiple Elements with the Same Class
Here are multiple paragraphs styled with the same class:
This paragraph is highlighted.
This paragraph is also highlighted.
And this one is highlighted too!
Example 2: Centered Text
This text is centered using a class.
This text is not centered.
Example 3: Combining Classes
This paragraph is both highlighted and centered!