The Wayback Machine - https://web.archive.org/web/20240717024534/https://www.geeksforgeeks.org/css-selectors/
Open In App

CSS Selectors

Last Updated : 13 Jun, 2024
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

CSS selectors target the HTML elements on your pages, allowing you to add styles based on their ID, class, type, attribute, and more. This guide will help you to understand the intricacies of CSS selectors and their important role in enhancing the user experience of your web pages. Understanding these selectors—such as the universal selector, attribute selector, pseudo-class selector, and combinator selectors—enables more efficient and dynamic web design.

Types of CSS Selectors

CSS selectors come in various types, each with its unique way of selecting HTML elements. Let’s explore them:

CSS Selectors Description

Simple Selectors

It is used to select the HTML elements based on their element name, id, attributes, etc

Universal SelectorSelects all elements on the page.
Attribute SelectorTargets elements based on their attribute values.
Pseudo-Class SelectorSelects elements based on their state or position, such as :hover for hover effects.
Combinator SelectorsCombine selectors to specify relationships between elements, such as descendants ( ) or child (>).
Pseudo-Element SelectorSelects specific parts of an element, such as ::before or ::after.

Simple Selectors

Simple selectors contains the below classes.

Simple Selector Description
Element SelectorSelects HTML elements based on their tag names.
Id SelectorTargets an HTML element with a specific id attribute.
Class SelectorSelects elements with a particular class attribute.

Example: In this example, we will write the code to understand selectors and their uses in a better way.

HTML
<!DOCTYPE html>
<html lang="en">
    <head>
        <title>CSS Selectors</title>
        <link rel="stylesheet" href="style.css" />
    </head>

    <body>
        <h1>Sample Heading</h1>
        <p>This is Content inside first paragraph</p>
        <div id="div-container">
            This is a div with id div-container
        </div>
        <p class="paragraph-class">
            This is a paragraph with class paragraph-class
        </p>
    </body>
</html>

Note: We will apply CSS rules to the above Example.

Element Selector

The element selector selects HTML elements based on the element name (or tag) for example p, h1, div, span, etc.

NOTE : The following code is used in the above Example. You can see the CSS rules applied to all <p> tags and <h1> tags.

CSS:

h1 {
color: red;
font-size: 3rem;
}
p {
color: white;
background-color: gray;
}

Output:

CSS-Element-Selectors-Example-output

CSS Element Selector output

Id Selector

The id selector uses the id attribute of an HTML element to select a specific element. An id of the element is unique on a page to use the id selector.

Note: The following code is used in the above Example using the id selector.

CSS:

#div-container{
color: blue;
background-color: gray;
}

Output:

CSS-Id-selectors-example-output

CSS ID Selectors example output

Class Selector

The class selector selects HTML elements with a specific class attribute.

Note: The following code is used in the above Example using the class selector. To use a class selector you must use ( . ) followed by class name in CSS. This rule will be applied to the HTML element with the class attribute “paragraph-class“

CSS:

.paragraph-class {
color:white;
font-family: monospace;
background-color: purple;
}

Output:

CSS-Class-Selectors-Example-Output

CSS Class Selector Example Output

Universal Selector

The Universal selector (*) in CSS is used to select all the elements in an HTML document. It also includes other elements which are inside under another element.

Note: The following code is used in the above Example using the universal selector. This CSS rule will be applied to each and every HTML element on the page: 

CSS:

* {
color: white;
background-color: black;
}

Output:

CSS-Universal-Selector-Example-Output

CSS Universal Selector Example Output

Group Selector

The Group selector is used to style all comma-separated elements with the same style.

Note: Suppose you want to apply common styles to different selectors, instead of writing rules separately you can write them in groups as shown below.

CSS:

#div-container, .paragraph-class, h1{
color: white;
background-color: purple;
font-family: monospace;
}

Output:

CSS-Group-Selector-Example-Output

CSS Group Selector Example Output

Attribute Selector

The attribute selector [attribute] is used to select the elements with a specified attribute or attribute value.

Note: The following code is used in the above Example using the attribute selector. This CSS rule will be applied to each and every HTML element on the page:

CSS:

[href] {
background-color: lightgreen;
color: black;
font-family: monospace;
font-size: 1rem;
}

Output:

CSS-Attribute-Selectros-Example-Output

CSS Attribute Selectros Example Output

Pseudo-Class Selector

It is used to style a special type of state of any element. For example- It is used to style an element when a mouse cursor hovers over it.

Note: We use a single colon(:) in the case of a Pseudo-Class Selector.

Syntax:

Selector:Pseudo-Class {
Property: Value;
}

CSS:

h1:hover{
background-color: aqua;
}

Output:

CSS-Pseudo-Selector-Example-Output

CSS Pseudo Selector Example Output

Pseudo-Element Selector

It is used to style any specific part of the element. For Example- It is used to style the first letter or the first line of any element.

Note: We use a double colon(::) in the case of a Pseudo-Element Selector.

Syntax:

Selector:Pseudo-Element{
Property:Value;
}

CSS:

p::first-line{
background-color: goldenrod;
}

Output:

CSS-Pseudo-Element-Selector-Example-Output

CSS Pseudo-Element Selector Example Output


CSS selectors is crucial for effective web design. These selectors allow developers to precisely target and style HTML elements, creating more dynamic and visually appealing web pages. By utilizing various selectors—simple, combinator, pseudo-class, and pseudo-element—you can significantly enhance the aesthetics and functionality of your websites. Incorporating these selectors into your CSS toolkit will elevate your web development skills and improve user experience.



Previous Article
Next Article

Similar Reads

Define Selectors in jQuery and What are the types of selectors?
Selectors in jQuery: Selectors in jQuery are functions that primarily focus on selecting particular HTML elements and modifying them as per the action they need to perform. These selectors select particular HTML elements as per their name, element type, class, id, attributes or values of attributes, and much more. In other words, we can say that se
4 min read
CSS Selectors Complete Reference
CSS selectors are used to select HTML elements based on their element name, id, class, attribute, and many more. Example: Below the HTML/CSS code shows the functionality of ::after selector. C/C++ Code &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;style&gt; p::after { content: &quot; - Remember this&quot;; background-color: blue; } &lt;/style
4 min read
Advanced Selectors in CSS
Selectors are used for selecting the HTML elements in the attributes. Some different types of selectors are given below: Adjacent Sibling Selector: It selects all the elements that are adjacent siblings of specified elements. It selects the second element if it immediately follows the first element. Syntax: It selects ul tags that immediately follo
5 min read
Which characters are valid in CSS class names/selectors?
It is very easy to choose a valid class name or selectors in CSS just follow the rule. A valid name should start with an underscore (_), a hyphen (-) or a letter (a-z)/(A-Z) which is followed by any numbers, hyphens, underscores, letters.It cannot start with a digit, starting with the digit is acceptable by HTML5 but not acceptable by CSS.Two hyphe
2 min read
Why are dashes preferred for CSS selectors / HTML attributes ?
CSS is a hyphen-delimited syntax language which means while writing HTML attributes we can use hyphen(-). Example: font-size, line-height, background-color, etc... There are various reasons to preferring hyphen(-): In CSS, one can use underscore(_) instead of hyphen(-) for CSS selectors(class, id, span, ...), but preference is given based on its ea
2 min read
How to place background image using ::before pseudo selectors in CSS ?
Given a web page and the task is to place the background image on web page using ::before pseudo selector. The ::before pseudo selector is used to place something before the content of the selected items. Syntax: .container::before{ content: ''; background: url(image file); position:absolute; top:0px; left:0px; } Approach: The ::before pseudo selec
2 min read
Why do browsers match CSS selectors from right to left ?
Cascading Style Sheets 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 independently of the HTML that makes up each web page. It describes how a webpage should look: it prescribes color
3 min read
How CSS Styles Conflicts Between Selectors ?
CSS conflicts between selectors occur when two or more selectors have conflicting styles applied to the same element, leading to unexpected results. In such cases, the browser has to decide which style to apply to the element. In this article, we'll understand what causes conflicting styles in CSS, and how to avoid and resolve them. Causes of Confl
4 min read
Wildcard Selectors (*, ^ and $) in CSS for classes
Wildcard selectors can be used with attribute selectors. The asterisk (*) matches any character, the caret (^) matches the start, and the dollar sign ($) matches the end of an attribute value. For example, .class* selects all elements with a class attribute containing "class" anywhere, .class^ selects those starting with "class," and .class$ select
3 min read
Difference Between Descendant and Child Selectors in CSS
The Descendant selector and child selector in CSS are both used to target specific elements within the document structure, but they differ in their level of specificity and targeting. Descendant SelectorIt uses a space to select all descendants of a particular ancestor. In the below example, It selects all matching elements, regardless of their dep
1 min read
three90RightbarBannerImg