πŸŽ‰ 75% of content is free forever β€” Unlock Premium from $10/mo β†’
CW
Search courses…
πŸ’Ό Servicesℹ️ Aboutβœ‰οΈ ContactView Pricing Plansfrom $10

Custom Selectors

Web Styles🟒 Free Lesson

Advertisement

Custom Selectors

CSS custom selectors, @custom-selector, and extending selector patterns.

Overview

Custom selectors extend CSS selector capabilities.

Key Concepts

  • @custom-selector β€” Define custom selectors
  • :--custom β€” Custom pseudo-class syntax
  • Selector patterns β€” Reusable patterns
  • @scope β€” Scoped selectors
  • Nesting β€” Nested selectors

Code Examples

/* Custom selector (proposal) */
@custom-selector :--heading h1, h2, h3, h4, h5, h6;

:--heading {
  font-weight: bold;
  line-height: 1.2;
}

:--heading:first-child {
  margin-top: 0;
}

/* Custom interactive states */
@custom-selector :--interactive a, button, [role="button"];

:--interactive {
  cursor: pointer;
  transition: all 0.2s ease;
}

:--interactive:hover {
  opacity: 0.8;
}

:--interactive:focus-visible {
  outline: 2px solid var(--primary);
}

/* Nesting selectors */
.card {
  & .title {
    font-size: 1.5rem;
    
    &:hover {
      color: var(--primary);
    }
  }
  
  & .content {
    color: #666;
    
    & p {
      margin-bottom: 1rem;
    }
  }
  
  &:hover {
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  }
}

/* :is() and :where() */
:is(h1, h2, h3) {
  font-weight: bold;
}

:where(.button, .link) {
  cursor: pointer;
  transition: all 0.2s ease;
}

/* :has() selector */
.form-group:has(:invalid) {
  border-color: red;
}

.card:has(img) {
  display: grid;
  grid-template-columns: 200px 1fr;
}

/* Logical selectors */
.item:first-child:last-child {
  /* Only child */
}

.item:not(:last-child)::after {
  content: ',';
}

/* Attribute selectors */
[data-theme="dark"] {
  --bg: #1a1a1a;
  --text: #ffffff;
}

[data-type^="icon-"]::before {
  content: attr(data-type);
}

[href$=".pdf"]::after {
  content: ' (PDF)';
}

/* State selectors */
.input:is(:focus, :hover) {
  border-color: var(--primary);
}

.checkbox:checked + label {
  font-weight: bold;
}

Practice

Refactor CSS to use custom selectors and modern patterns.

⭐

Premium Content

Custom Selectors

Unlock this lesson and 900+ advanced tutorials with a Premium plan.

🎯End-to-end Projects
πŸ’ΌInterview Prep
πŸ“œCertificates
🀝Community Access

Already a member? Log in

Need Expert Web Development Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement