Logo

dev-resources.site

for different kinds of informations.

Useful CSS Selectors You Might Not Know

Published at
1/13/2025
Categories
css
webdev
html
tailwindcss
Author
kelen
Categories
4 categories in total
css
open
webdev
open
html
open
tailwindcss
open
Author
5 person written this
kelen
open
Useful CSS Selectors You Might Not Know

CSS selectors play a crucial role in web development for styling web pages. While many are familiar with the common selectors, there are several less common but very useful ones.

What Are CSS Selectors?

CSS selectors are patterns that help in selecting elements on a web page for styling. They can target elements based on attributes, classes, IDs, and more.

Common CSS Selectors

Here are some commonly used ones:

  • Element Selector: Targets all elements of a specific type. For example, to style all <div> elements:
  div {
    border: 1px solid black;
  }
Enter fullscreen mode Exit fullscreen mode
  • Class Selector: Selects elements with a particular class. If we have a class "text - large":
  .text - large {
    font-size: 20px;
  }
Enter fullscreen mode Exit fullscreen mode
  • ID Selector: Targets an element with a specific ID. For an element with ID "header":
  #header {
    background-color: blue;
  }
Enter fullscreen mode Exit fullscreen mode
  • Attribute Selector: Used for elements with specific attribute values. For example, to style all links that are external (using the "rel" attribute):
  a[rel="external"] {
    color: red;
  }
Enter fullscreen mode Exit fullscreen mode

Less Common but Useful CSS Selectors

Child Selector(>)

It targets direct children of an element. For a parent element with class "container":

.container > p {
  margin-left: 10px;
}
Enter fullscreen mode Exit fullscreen mode

Descendant Selector( )

This selects all descendants within an element. If we have a div with ID "main" and want to style all <span> elements inside it:

#main span {
  color: green;
}
Enter fullscreen mode Exit fullscreen mode

Adjacent Sibling Selector(+)

Selects an element that immediately follows another specific element. For example, after an <h3> element, if there's a <p> element:

h3 + p {
  font-weight: bold;
}
Enter fullscreen mode Exit fullscreen mode

General Sibling Selector(~)

Targets elements that are siblings of another element, not necessarily adjacent. If we have a div with class "item" and want to style all following siblings with class "detail":

.item ~ .detail {
  padding-top: 5px;
}
Enter fullscreen mode Exit fullscreen mode

Attribute Selector with Partial Match(^=, $=, *=)

  img[src^="https://example.com/images/"]
  {
    border-radius: 5px;
  }
Enter fullscreen mode Exit fullscreen mode
  • Ends with ($=): For all forms with a method ending with "post":
  form[method$="post"] {
    background-color: #f0f0f0;
  }
Enter fullscreen mode Exit fullscreen mode
  • Contains (*=): To style all links containing "product" in the href attribute:
  a[href*="product"] {
    text-decoration: underline;
  }
Enter fullscreen mode Exit fullscreen mode

Negation Pseudo - Class(:not())

It selects elements that do not match a certain selector. For example, all elements except those with class "hidden":

:not(.hidden) {
  display: block;
}
Enter fullscreen mode Exit fullscreen mode

Target Pseudo - Class(:target)

When the URL fragment matches an element's ID. For a section with ID "contact" in the URL:

#contact:target {
  background-color: yellow;
}
Enter fullscreen mode Exit fullscreen mode

Language Pseudo - Class(:lang())

Targets elements based on language attributes. For elements with lang="en-US":

:lang(en-US) {
  font-family: Arial, sans - serif;
}
Enter fullscreen mode Exit fullscreen mode

Has Pseudo - Class(:has())

The :has() pseudo - class is used to select elements that contain a specific child or descendant. For example, to style a div that contains an image:

div:has(> img) {
  padding: 10px;
}
Enter fullscreen mode Exit fullscreen mode

Selection Pseudo - Class(::selection)

This pseudo - class allows you to style the part of the text that the user has selected. For example, when a user selects some text within a paragraph:

p::selection {
  background - color: purple;
  color: white;
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

These less common CSS selectors offer additional ways to target and style elements precisely. They can enhance the flexibility and functionality of our CSS code, making it more powerful and efficient in creating visually appealing and well structured web pages.

More information can be found at https://en.kelen.cc/

html Article's
30 articles in total
Favicon
Learning HTML is the best investment I ever did
Favicon
[Boost]
Favicon
How to Use JavaScript to Reduce HTML Code: A Simple Example
Favicon
got Tired of analysis paralyysis so i built an extensioon to get into flow faster
Favicon
Truncating Text with Text-Overflow
Favicon
Label + Checkbox States
Favicon
Getting Started with HTML
Favicon
Create Stunning Gradual Div Reveals with JavaScript setTimeout and CSS Transitions
Favicon
3D models
Favicon
Useful CSS Selectors You Might Not Know
Favicon
[Boost]
Favicon
Building the Foundations: A Beginner’s Guide to HTML
Favicon
Shadow DOM Perfected: Experience the Power of Monster 3.100!
Favicon
Rendering Shopify Liquid Code Locally with VS Code
Favicon
Top HTML Interview Questions and Answers
Favicon
Files Paths & Images
Favicon
Reading Progress Bar
Favicon
[Boost]
Favicon
🐈‍⬛ Git and GitHub: A Beginner’s Guide to Version Control 🚀
Favicon
Using the currentColor property to change SVG color
Favicon
Introduction to HTML Elements
Favicon
Animated Gradient Background
Favicon
HTML Validation
Favicon
Seven quickest ways to center your div using CSS
Favicon
10 Common HTML and CSS Interview Questions and Answers
Favicon
HTML Semantic tags
Favicon
Making Video Creation Easy? InVideoAI.video Has the Answer
Favicon
Gallery with varied image sizes using aspect-ratio and object-fit
Favicon
Adaptive va Repsonsive dizayn farqi
Favicon
Static website forms

Featured ones: