CSS Class Selector
Last Updated :
29 Aug, 2024
The CSS .class selector is used to select all elements that belong to a specific class attribute. By using the period (.) character followed by the class name, you can apply CSS properties to all elements with that class.
Syntax:
.class {
// CSS property
} Example 1: This example demonstrates the class Selector for the specific HTML element.
HTML
<!DOCTYPE html>
<html>
<head>
<style>
.geeks {
color: green;
}
.gfg {
background-color: yellow;
font-style: italic;
color: green;
}
</style>
</head>
<body style="text-align:center">
<h1 class="geeks">
GeeksforGeeks
</h1>
<h2>.class Selector</h2>
<div class="gfg">
<p>GeeksforGeeks: A computer science portal</p>
</div>
</body>
</html>
Output:

Example 2: This example describes the space-separated class name.
HTML
<!DOCTYPE html>
<html>
<head>
<title>class selector</title>
<style>
.geeks {
color: green;
}
.gfg {
background-color: yellow;
</style>
</head>
<body style="text-align:center">
<h1 class="geeks">
GeeksforGeeks
</h1>
<h2 class="gfg">.class Selector</h2>
<p class="geeks gfg">
GeeksforGeeks: A computer science portal
</p>
</body>
</html>
Output:

The CSS class selector is a powerful tool for applying styles to specific groups of elements, enhancing the flexibility and control you have over your web design.
Supported Browsers: The browser supported by the .class selector are listed below:
- Google Chrome 1.0
- Firefox 1.0
- Microsoft Edge 12.0
- Opera 3.5
- Internet Explorer 3.0
- Safari 1.0
CSS Class Selector – FAQs
What is the purpose of the class selector in CSS?
The class selector is used to select and style all elements with a specific class attribute value.
How do I use a class selector to style an element?
Use a dot (.) followed by the class name, like .example to style all elements with the class example.
Can multiple elements have the same class in CSS?
Yes, multiple elements can share the same class, allowing for consistent styling across various elements.
Is it possible to combine a class selector with other selectors?
Yes, you can combine class selectors with other selectors, such as element selectors, like p.example to target only paragraphs with the class example.
What is the difference between a class selector and an id selector?
A class selector (.) can apply to multiple elements, while an id selector (#) should be unique to a single element within a document.
Please Login to comment...