close
The Wayback Machine - https://web.archive.org/web/20240909115305/https://www.geeksforgeeks.org/css-class-selector/
Open In App

CSS Class Selector

Last Updated : 29 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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:

BERJAYA

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:

BERJAYA

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.


Previous Article
Next Article

Similar Reads

CSS :autofill pseudo-class Selector
The CSS :autofill pseudo-class refers to an input element that had its value automatically filled by the browser, such as when a user saves their form data for future use. The :autofill pseudo-class can be used to style the appearance of an input element that has been auto-filled, to indicate to the user that their previously saved data has been lo
2 min read
How to exclude particular class name from CSS selector ?
In CSS, to exclude a particular class, we can use the pseudo-class :not selector also known as negation pseudo-class or not selector. This selector is used to set the style to every element that is not the specified by given selector. Since it is used to prevent specific items from lists of selected items. Syntax::not(Class_name) { // CSS Property}
2 min read
CSS :has() pseudo-class Selector
The CSS :has() pseudo-class is a relatively new feature that allows developers to select elements based on their children. It is a powerful tool for more specific and dynamic styling and can be used in conjunction with other pseudo-classes and selectors. The :has() pseudo-class is used to select parent elements that contain specific child elements.
2 min read
What does the “+” (plus sign) CSS selector mean?
The "+" sign selector is used to select the elements that are placed immediately after the specified element but not inside the particular elements. Note: The IE8 and earlier versions &lt;/DOCTYPE&gt; must be declared to work element + element selector. Syntax: element + element { // CSS property } Example: &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;he
1 min read
How to use a not:first-child selector in CSS?
This selector is used to select every element which is not the first-child of its parent element. It is represented as an argument in the form of :not(first-child) element. Syntax: :not( element ) { // CSS property } Example: &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;not first child selector&lt;/title&gt; &lt;style&gt; h1 { color
1 min read
How content property is used with :after selector in CSS ?
In this article, we will learn how content property is used with the :after selector in CSS. The :after selector is used to insert something after any selected element. And the content property is used to specify the content that will be inserted. So we use the content property in ::after selector to put something after the selected element. Approa
1 min read
CSS element~element Selector
The element ~ element selector in CSS is used to match the second element if it follows the first element (it doesn't need to be immediate). They both should have the same parent element. Syntax: element ~ element { //CSS Property}Example 1: In the below program you can see that "p ~ ul" will select and style only the second unordered list which co
2 min read
CSS element+element Selector
The element + element selector in CSS is used to style every element that is placed immediately after (not inside) the first specified element. Syntax: element + element { //CSS Property}Example 1: In the below program the "p + p" selector selects and styles every pair of consecutive paragraph elements. If there are more than 2 consecutive p elemen
2 min read
CSS element &gt; element Selector
The element &gt; element selector in CSS is used to style only those elements which are the children of the specific parent. The operand on the left of &gt; is the parent and the operand on the right is the children element. Note: Elements that are not directly a child of the specified parent, are not selected. Syntax: element1 &gt; element2 { //CS
2 min read
What is greater-than sign (&gt;) selector in CSS?
The greater than sign (&gt;) selector in CSS is used to select the element with a specific parent. It is called as element &gt; element selector. It is also known as the child combinator selector which means that it selects only those elements which are direct children of a parent. It looks only one level down the markup structure and not further d
2 min read
How to add space (" ") after an element using :after selector in CSS ?
The :after selector in CSS is used to add the same content multiple times after the content of other elements. It inserts something after the content of each selected element. Syntax: :after { // CSS Property}Example 1: This example uses :after selector to add space after an element. C/C++ Code &lt;!-- HTML code to add space after the selected elem
1 min read
How to select text input fields using CSS selector ?
CSS attribute selector is used to target input text fields. The input text fields of type 'text' can be targeted by using input[type=text]. Note: It is specified that the default attribute values may not always be selectable with attribute selectors, one could try to cover other cases of markup for which text inputs are rendered. input:not([type]):
2 min read
What is the use of asterisk (*) selector in CSS ?
The asterisk (*) is known as the CSS universal selectors. It can be used to select any and all types of elements in an HTML page. The asterisk can also be followed by a selector while using to select a child object. This selector is useful when we want to select all the elements on the page. For example: * { property : value; } While selecting elem
2 min read
Role of CSS selector
In this article, we will discuss how CSS selector is important while we are designing the web page. The role of CSS selector in the responsive web page is very important. Let us discuss one by one.Role of Child Selector: In Child Selector, properties of any element is the immediate parent of another element. Syntax: if article &gt; p It defines tha
2 min read
CSS :dir Selector
:dir is used to select and change the properties of the text element having direction left to right or right to left. Syntax: :dir(ltr|rtl) Parameters: It accepts the following two parameters. ltr: This keyword basically looks for elements having direction from left to right.rtl: This keyword basically looks for elements having direction from right
2 min read
CSS ::marker Selector
The ::marker selector in CSS is used to change the marker properties. Here marker is the marker box of a list item. In an unordered list, it is a bullet and in an ordered list, it is a number. Syntax: ::marker { properties } Property values: All font-properties: This can change properties like font size, font-family, etc.Color and content: This can
2 min read
CSS :any-link Selector
The :any-link selector is used to style every &lt;a&gt;, &lt;area&gt;, or &lt;link&gt; element that has an "href" attribute .It will skip applying CSS properties for those &lt;a&gt;, &lt;area&gt;, &lt;href&gt; tags in which "href" is not defined. Hence, it matches all elements that match :link or :visited. Syntax: :any-link{ /*css properties*/ } Th
2 min read
CSS :placeholder-shown Selector
The CSS :placeholder-shown pseudo-class selects the input element or textarea element that is having placeholder text. The main difference between :placeholder-shown and ::placeholder is that :placeholder-shown is for selecting the input itself when the placeholder text is being shown whereas ::placeholder is about giving style to the placeholder t
1 min read
How to define the name of the keyframe that binds to the selector in CSS?
There has been a rise in animated websites these days. Writing and running animations on a web page requires the use of keyframes to set some kind of rules for the frame in which that animation is being performed. While writing the required properties in the keyframes, we basically assign a value that varies from 0% to 100%. The 0% denotes the star
2 min read
CSS :is() Selector
A CSS pseudo-class is a keyword added to a selector that specifies a special state of the selected element(s). CSS :is() pseudo-class selector takes a selector list as an argument and selects any element that can be selected by one of the selectors in that list. It is useful to select elements in less code and more efficiently. Syntax::is() Example
1 min read
What is contextual selector in CSS ?
In this article, we will learn about the contextual selector in CSS &amp; understand the declaration syntax with the help of code examples.A contextual selector is defined as a selector which considers the context where the style is to be applied. In simple words, the specified style is applied to an element only if the element is in the specified
3 min read
How to use a "not equal" CSS attribute selector ?
Cascading Style Sheets(CSS) fondly referred to as CSS, is a simply designed language intended to simplify the process of making web pages presentable. CSS allows you to apply styles to web pages. More importantly, CSS enables you to do this independent of the HTML that makes up each web page. CSS is easy to learn and understand, but it provides pow
2 min read
What is mouse down selector in CSS ?
In this article, we are going to learn about the mouse-down selector in CSS. The mouse-down selector means the method to detect whether the primary mouse button is clicked or not. In CSS, the function of the mouse-down selector is done by the active pseudo-class. We can add this pseudo-class to the selector of the element to add the mouse-down sele
2 min read
What is Descendant Selector in CSS ?
In this article, we will see the descendant selector, provided by CSS, along with understanding its implementation through examples. A CSS Selectors are used to select HTML elements based on their element name, id, attributes, etc, which facilitates to select more than one element simultaneously. Descendant Selector is one of the type of Combinator
3 min read
Is there any selector for elements containing certain text in CSS ?
In this article, we will learn about CSS selectors for elements containing certain text. Selectors are used to finding the HTML elements you want to style. We can achieve this task by using an attribute-value selector. CSS [attribute-value] Selector: In the [attribute-value] selector, you can choose any attribute name from the HTML document and the
1 min read
How to create a function `generateSelector` to generate CSS selector path of a DOM element ?
In this article, we will learn about CSS selectors, and we will also implement a `generateSelector()` function that returns a string value to provide a valid selector to a target DOM element. What is a CSS Selector? A CSS selector is used to select a group of elements (or nodes in the DOM tree) that follow a certain pattern. Example: Here is an exa
5 min read
How does a browser determine what elements match a CSS selector ?
When a browser loads a web page, it first parses the HTML and creates a Document Object Model (DOM) tree. The DOM tree is a hierarchical representation of the HTML elements on a web page. Each element in the DOM tree is represented as a node, and the relationships between nodes represent the structure of the HTML document. Once the DOM tree and the
4 min read
CSS :not(:last-child):after Selector
The : not(:last-child): after CSS selector is employed in front-end web development to style elements, specifically adding content after each child element except the last one. This selector is useful when we want to target elements that cannot be directly selected. It is commonly used to enhance styling and visual presentation in HTML layouts. App
3 min read
How to Generate CSS Selector Automatically ?
Accurately selecting elements on a webpage with CSS is important in web design. Manually creating these selectors can be slow and prone to mistakes, especially for complex layouts. Modern web browsers like Google Chrome provide built-in developer tools that allow you to inspect and manipulate the HTML and CSS of a webpage. Using Browser Developer T
2 min read
Difference between :first-child and :first-of-type selector in CSS
The :first-child and :first-of-type selectors in CSS are used to target specific child elements within a parent. The :first-child selector targets an element if it is the first child of its parent, regardless of type. On the other hand, the :first-of-type selector targets the first occurrence of a specified element type within its parent. Understan
2 min read