CSS .class Selector
Description
The CSS class selector is used to select HTML elements with a specific class attribute. To select elements with a specific class, write a period (.) character, followed by the class name.
For example, the following CSS selector will select all div
elements with the class my-class
:
.my-class {
/* CSS styles here */
}
You can also use the class selector to select multiple HTML elements of different types. For example, the following CSS selector will select all div
and p
elements with the class my-class
:
.my-class div, .my-class p {
/* CSS styles here */
}
Class selectors are very useful for styling your web pages, as they allow you to reuse CSS styles for multiple elements. This can save you a lot of time and effort, and make your code more maintainable.
Syntax
.class { css declarations; }
Example
.intro {
color: #3366ff;
background: #ffff99;
}
In this example, the elements with the class .intro will have their color and background styles changed.
Give it a Try
Click the button below to experiment with this selector.
Browser Support
The following table will show you the current browser support for the CSS class
selector.
Desktop | |||||
![]() |
![]() |
![]() |
![]() |
![]() |
|
Yes | Yes | Yes | Yes | Yes |
Tablets / Mobile | |||||
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
Yes | Yes | Yes | Yes | Yes | Yes |