DEV Community

Cover image for All CSS Selectors in One Place: Learn to Target Any Element
Muhammad Usman
Muhammad Usman

Posted on

8 1 1 1 1

All CSS Selectors in One Place: Learn to Target Any Element

I will be talking about selectors in CSS, and how selectors are used to select an element on which style is going to be applied. Now, what kind of elements can we apply style on, and how can we select those elements?

For example, element with an ID, element with a class, we can target the element by its name. We can also decide that if there is a particular type of element in a particular type of element, then only the styling is applied.

I will be providing you the complete list of CSS selectors in this guide. This is a very important list of CSS. So increase your excitement, and let's go.

1. Universal Selector (*)

* {
    margin: 0;
    padding: 0;
}
Enter fullscreen mode Exit fullscreen mode
<p>All elements will have no margin or padding.</p>
Enter fullscreen mode Exit fullscreen mode

→ Selects all elements on the page.

2. Type Selector (element)

p {
    color: blue;
}
Enter fullscreen mode Exit fullscreen mode
<p>This text is blue.</p>
<p>Another blue text.</p>
Enter fullscreen mode Exit fullscreen mode

→ Selects all <p> elements.

3. Class Selector (.classname)

.highlight {
    background-color: yellow;
}
Enter fullscreen mode Exit fullscreen mode
<p class="highlight">This paragraph has a yellow background.</p>
Enter fullscreen mode Exit fullscreen mode

→ Selects all elements with the highlight class.

4. ID Selector (#idname)

#main-title {
    font-size: 24px;
}
Enter fullscreen mode Exit fullscreen mode
<h1 id="main-title">This is a large title.</h1>
Enter fullscreen mode Exit fullscreen mode

→ Selects the element with the id="main-title".

5. Attribute Selectors

[attribute] → Selects elements with the given attribute

[data-info] {
    border: 2px solid red;
}
Enter fullscreen mode Exit fullscreen mode
<p data-info="123">This has a custom attribute.</p>
Enter fullscreen mode Exit fullscreen mode

[attribute=value] → Selects elements with the exact attribute value

input[type="text"] {
    border: 1px solid blue;
}
Enter fullscreen mode Exit fullscreen mode
<input type="text" placeholder="Enter text">
<input type="password" placeholder="Enter password">
Enter fullscreen mode Exit fullscreen mode

→ Selects only text inputs, not password inputs.

[attribute~=value] → Selects elements where the attribute contains the word (space-separated)

[class~="btn"] {
    background-color: red;
}
Enter fullscreen mode Exit fullscreen mode
<button class="btn primary">Click Me</button>
Enter fullscreen mode Exit fullscreen mode

→ Selects elements with the word "btn" in the class list.

[attribute|=value] → Selects elements with the attribute value or value followed by a hyphen (-)

[class|="lang"] {
    color: green;
}
Enter fullscreen mode Exit fullscreen mode
<p class="lang-en">This is English.</p>
<p class="lang-fr">This is French.</p>
Enter fullscreen mode Exit fullscreen mode

→ Matches lang-en and lang-fr because they start with lang.

[attribute^=value] → Selects elements where the attribute starts with a value

a[href^="https://"] {
    color: green;
}
Enter fullscreen mode Exit fullscreen mode
<a href="https://example.com">Secure Link</a>
Enter fullscreen mode Exit fullscreen mode

→ Selects all links that start with https://.

[attribute$=value] → Selects elements where the attribute ends with a value

img[src$=".png"] {
    border: 3px solid blue;
}
Enter fullscreen mode Exit fullscreen mode
<img src="logo.png">
Enter fullscreen mode Exit fullscreen mode

→ Selects all images ending in .png.

[attribute*=value] → Selects elements where the attribute contains a value

a[href*="example"] {
    font-weight: bold;
}
Enter fullscreen mode Exit fullscreen mode
<a href="https://example.com">Visit Example</a>
Enter fullscreen mode Exit fullscreen mode

→ Selects all links that contain "example" anywhere in the URL.

6. Grouping Selector (selector1, selector2)

h1, h2, h3 {
    font-family: Arial, sans-serif;
}
Enter fullscreen mode Exit fullscreen mode

→ Selects all <h1>, <h2>, and <h3> elements.

7. Combinators

Descendant Combinator (parent child)

div p {
    color: red;
}
Enter fullscreen mode Exit fullscreen mode
<div>
    <p>This is red.</p>
</div>
Enter fullscreen mode Exit fullscreen mode

→ Selects all <p> inside <div>.

Child Combinator (parent > child)

div > p {
    color: blue;
}
Enter fullscreen mode Exit fullscreen mode
<div>
    <p>This is blue.</p>
    <span><p>This is not blue.</p></span>
</div>
Enter fullscreen mode Exit fullscreen mode

→ Only direct <p> inside <div> turns blue.

Adjacent Sibling Combinator (element1 + element2)

h1 + p {
    font-style: italic;
}
Enter fullscreen mode Exit fullscreen mode
<h1>Heading</h1>
<p>This is italic.</p>
Enter fullscreen mode Exit fullscreen mode

→ Styles the first <p> after <h1>.

General Sibling Combinator (element1 ~ element2)

h1 ~ p {
    color: green;
}
Enter fullscreen mode Exit fullscreen mode
<h1>Heading</h1>
<p>Green text.</p>
<p>Also green.</p>
Enter fullscreen mode Exit fullscreen mode

→ Styles all <p> elements after <h1>.

8. Pseudo-Classes

User Interaction

button:hover { background: red; }
button:active { background: yellow; }
Enter fullscreen mode Exit fullscreen mode

Form & Input State

input:checked { border: 2px solid green; }
input:disabled { background: lightgray; }
Enter fullscreen mode Exit fullscreen mode

Structural

p:first-child { font-weight: bold; }
p:last-child { color: orange; }
p:nth-child(2) { text-decoration: underline; }
Enter fullscreen mode Exit fullscreen mode

9. Pseudo-Elements

p::before { content: "👉 "; color: red; }
p::after { content: " ✅"; color: green; }
Enter fullscreen mode Exit fullscreen mode
<p>Select this text.</p>
Enter fullscreen mode Exit fullscreen mode

10. CSS Grid Selectors

div:nth-child(2 of .box) { background: yellow; }
Enter fullscreen mode Exit fullscreen mode
<div class="box">Box 1</div>
<div class="box">Box 2</div> <!-- This is yellow -->
Enter fullscreen mode Exit fullscreen mode

11. Special & Experimental

:lang(en) { font-style: italic; }
:dir(rtl) { text-align: right; }
:has(img) { border: 2px solid black; }
Enter fullscreen mode Exit fullscreen mode
<p lang="en">This is English.</p>
Enter fullscreen mode Exit fullscreen mode

So, that's the complete list of CSS selectors and how they work! Selectors are the foundation of styling in CSS, allowing us to target elements in different ways—by ID, class, attribute, relationship, or even user interaction. Mastering them gives you full control over your styles, making your designs more precise and efficient.

Now that you know how to select any element and apply styles, you can start experimenting and combining selectors to create powerful styles. Keep exploring, keep styling, and have fun with CSS!

Don’t forget to share it with your fellow developers and friends, so it can reach to a wide audience.

📍 Find me on: LinkedIn | Medium | Bluesky

Image of Datadog

Master Mobile Monitoring for iOS Apps

Monitor your app’s health with real-time insights into crash-free rates, start times, and more. Optimize performance and prevent user churn by addressing critical issues like app hangs, and ANRs. Learn how to keep your iOS app running smoothly across all devices by downloading this eBook.

Get The eBook

Top comments (4)

Collapse
 
netcodensazzadali profile image
netcodensazzadali

great!

Collapse
 
web_dev-usman profile image
Muhammad Usman • Edited

Thank you!

Collapse
 
awaisalwaisy profile image
Alwaisy al-waisy

Develop it's interactive verison if you do frontend engineers will love that project.

Collapse
 
jwp profile image
JWP

Excellent

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more