Class
A CSS Class
is a selector
used to apply styles to HTML elements or select elements with JavaScript.
It is primarily used to apply the same CSS styles to multiple HTML elements.
Key Features of Class
In CSS, a class is defined by a name starting with a .
and can be assigned to multiple elements to apply consistent styling.
You can apply multiple classes to a single HTML element, and class names should be defined using letters, numbers, hyphens (-
), and underscores (_
), without spaces.
For example, here is a CSS class that makes the background color yellow and the text color blue.
highlighted CSS class
.highlighted {
background-color: yellow;
color: blue;
}
This class can be applied in an HTML document like this to style h1, h2, and p tags with the same styles.
Applying the CSS class
<h1 class="highlighted">Highlighted H1</h1>
<h2 class="highlighted">Highlighted H2</h2>
<p class="highlighted">Highlighted Paragraph 1</p>
<p class="highlighted">Highlighted Paragraph 2</p>
Follow the emphasized parts of the code and try entering it.
Want to learn more?
Join CodeFriends Plus membership or enroll in a course to start your journey.