Class
How to Reuse Styles
A CSS class
is an identifier (selector) used to apply styles to HTML elements or to select elements using JavaScript.
It is primarily used to apply the same CSS styles to multiple HTML elements.
Key Features of a Class
-
A class is defined in CSS with a name starting with a
.
and multiple elements can share the same class name to share styles -
Multiple classes can be applied to a single HTML element
-
Class names should be defined without spaces, using letters, numbers, hyphens (
-
), and underscores (_
)
For example, the following CSS class sets the background color to yellow and the text color to blue.
.highlighted { background-color: yellow; color: blue; }
This class can be applied in an HTML document to apply the same style to h1, h2, and p tags as shown below.
<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 along with the highlighted parts of the code.
All cards' background color will be applied as coral.
A CSS class is an identifier used to apply styles to an HTML element.
Lecture
AI Tutor
Publish
Design
Upload
Notes
Favorites
Help