CSS Portal

CSS Class Selector

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
Class
CSS SelectorThis CSS class selector is supported by all modern browsers.
Updated 3rd January 2026

Description

The class selector is one of the most commonly used selectors in CSS, allowing developers to target and style HTML elements that have a specific class attribute. Unlike element selectors that select all instances of a particular div or p tag, the class selector provides more granular control, enabling styles to be applied only to elements assigned a particular class. This makes it extremely useful for applying the same styles to multiple elements across a page or site without affecting other elements of the same type.

A key advantage of the class selector is its flexibility. Multiple classes can be assigned to a single element, allowing the combination of different style rules. For example, an element can have both a highlight class and a rounded class, and the styles for both classes will apply. This modular approach promotes reusability and cleaner code. The class selector is also frequently used in conjunction with other selectors, such as ID selectors or pseudo-classes like :hover, to target elements more specifically.

In practice, the class selector is applied by prefixing the class name with a dot (.) in CSS. For example, a button element with <code>class="primary-button"</code> can be styled using .primary-button { background-color: blue; color: white; }. This makes it easy to maintain consistent styling for buttons across an entire website while keeping the CSS manageable. Additionally, combining the class selector with descendant or child selectors can provide even finer control over the styling of nested elements.

Syntax

.class { css declarations; }

Example

<body>

<h2 class="highlight-text">This heading is highlighted.</h2>

<p>This is a normal paragraph with no special styling.</p>

<p class="highlight-text">This paragraph also uses the same class!</p>

</body>
/* The period indicates this is a class selector */
.highlight-text {
background-color: yellow;
color: #333;
padding: 5px;
border-radius: 4px;
font-weight: bold;
}
Open in editor

Browser Support

The following information will show you the current browser support for the CSS Class selector. Hover over a browser icon to see the version that first introduced support for this selector.

This CSS class selector is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee