DEV Community

Cover image for CSS Smart Selectors & Pseudo Elements
Ebenezer Merdekiyos
Ebenezer Merdekiyos

Posted on

CSS Smart Selectors & Pseudo Elements

๐Ÿงฉ 1. Attribute Selectors

Select elements based on their attributes or attribute values.

๐Ÿ’ก Syntax Examples:

input[type="text"] { ... }   /* exact match */
a[href^="https"] { ... }     /* starts with */
a[href$=".pdf"] { ... }      /* ends with */
a[href*="docs"] { ... }      /* contains */
Enter fullscreen mode Exit fullscreen mode

๐Ÿง  Example:

<input type="text" placeholder="Name">
<input type="password" placeholder="Password">
<a href="https://example.com">Visit</a>
<a href="manual.pdf">Manual</a>

Enter fullscreen mode Exit fullscreen mode

๐Ÿ‘‰ Useful for styling forms, links, or any tag with attributes โ€” no extra classes needed!


๐Ÿช„ 2. Pseudo-elements (::before, ::after)

Used to generate content or add decorations using CSS only.

๐Ÿ’ก Syntax:

h2::before {
  content: "๐ŸŒŸ ";
}
a::after {
  content: " โ†—";
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿง  Example:

<h2>Welcome</h2>
<a href="#">Learn more</a>

Enter fullscreen mode Exit fullscreen mode

๐Ÿ‘‰ Great for icons, labels, badges, or automatic quotes โ€” adds personality to designs without extra HTML.


๐Ÿ–ฑ๏ธ 3. User Action Pseudo-classes

React to user interactions (hover, click, focus, etc.).

๐Ÿ’ก Common ones:

button:hover { background: #2563eb; color: white; }  /* on hover */
input:focus { border-color: #10b981; }               /* when focused */
a:active { color: red; }                             /* when clicked */
Enter fullscreen mode Exit fullscreen mode

๐Ÿง  Example:

<button>Hover me</button>
<input type="text" placeholder="Focus me">
Enter fullscreen mode Exit fullscreen mode

๐Ÿ‘‰ These make your website feel alive โ€” interactive and dynamic.


๐Ÿ‘ถ 4. Child Combinator (>)

Targets only direct children of an element (not grandchildren).

๐Ÿ’ก Syntax:

section > p {
  color: blue;
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿง  Example:

<section>
  <p>Direct child (styled)</p>
  <div>
    <p>Nested child (not styled)</p>
  </div>
</section>
Enter fullscreen mode Exit fullscreen mode

๐Ÿ‘‰ Helps you keep precise control over which elements get styled.


๐Ÿงฑ 5. Structural Pseudo-classes

Select elements based on their position in the HTML structure.

๐Ÿ’ก Syntax:

li:first-child { color: red; }
li:last-child { color: blue; }
p:only-child { font-style: italic; }
Enter fullscreen mode Exit fullscreen mode

๐Ÿง  Example:

<ul>
  <li>First</li>
  <li>Middle</li>
  <li>Last</li>
</ul>
Enter fullscreen mode Exit fullscreen mode

๐Ÿ‘‰ Super handy for lists, tables, grids, and sections โ€” no need for extra classes.


๐Ÿšซ 6. Negation Pseudo-class (:not())

Select everything except something specific.

๐Ÿ’ก Syntax:

button:not(.primary) {
  background: gray;
}

Enter fullscreen mode Exit fullscreen mode

๐Ÿง  Example:

<button class="primary">Save</button>
<button>Cancel</button>
Enter fullscreen mode Exit fullscreen mode

๐Ÿ‘‰ Styles all buttons except the one with the .primary class.
Perfect for excluding special elements from general styles.


๐Ÿ”ข 7. Nth Pseudo-class (:nth-child() / :nth-of-type())

Select elements by their order number.

๐Ÿ’ก Syntax:

li:nth-child(odd) { background: #f3f4f6; }  /* every odd item */
li:nth-child(even) { background: #e0f2fe; } /* every even item */
li:nth-child(3) { color: red; }             /* third item only */
Enter fullscreen mode Exit fullscreen mode

๐Ÿง  Example:

<ul>
  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>
  <li>Item 4</li>
</ul>
Enter fullscreen mode Exit fullscreen mode

๐Ÿ‘‰ Perfect for alternating row colors (like zebra stripes in tables).


โœ… 8. Validity Pseudo-classes (:valid, :invalid, :required)

Used in forms to style inputs based on whether theyโ€™re valid or not.

๐Ÿ’ก Syntax:

input:valid { border-color: #10b981; }
input:invalid { border-color: #ef4444; }
input:required { background: #fff7ed; }
Enter fullscreen mode Exit fullscreen mode

๐Ÿง  Example:

<input type="email" placeholder="Enter email" required>
Enter fullscreen mode Exit fullscreen mode

๐Ÿ‘‰ Adds real-time visual feedback in forms โ€” no JavaScript needed!


๐Ÿ”— 9. Relational Selector (:has())

Select elements based on what they contain.
(Modern browsers only โ€” supported in Chrome, Edge, Safari, Firefox 121+)

๐Ÿ’ก Syntax:

article:has(img) {
  border: 2px solid #2563eb;
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿง  Example:

<article>
  <img src="photo.jpg">
  <p>This article has an image.</p>
</article>
Enter fullscreen mode Exit fullscreen mode

๐Ÿ‘‰ Think of :has() as a โ€œparent-awareโ€ selector โ€” CSS can now react to children!
(For example: โ€œstyle any card that contains a button.โ€)

Top comments (1)

Collapse
ย 
hashbyt profile image
Hashbyt โ€ข

This is a fantastic breakdown of CSS selectors! Your examples make it so easy for beginners to grasp these conceptsโ€”great job!