simevidas, (edited )
@simevidas@mastodon.social avatar

X:has(Y) = X that contains Y
X:has(> Y) = X that has a child Y
X:has(> :last-child:nth-child(n)) = X that has n children (for n > 0)
X:has(+ Y) = X that has a next sibling Y
X:not(:has(Y)) = X that doesn’t contain Y
X:has(Y):has(Z) = X that contains Y and Z
X:has(Y, Z) = X that contains Y or Z

What other useful (simple) selector patterns with :has() are there?

(https://ishadeed.com/article/css-has-guide)

css,
@css@front-end.social avatar

@simevidas Probably not simple but useful: https://css-tip.com/number-elements-has-selector/

Actually, if you use them a lot they become simple.

simevidas, (edited )
@simevidas@mastodon.social avatar

@css If we know that an element never contains text node children, then :not(:has(*)) can be used as an alternative to :blank that ignores white space. I guess that can be useful.

edit: I meant :empty, not :blank.

ollicle,
@ollicle@mastodon.social avatar
simevidas,
@simevidas@mastodon.social avatar

@ollicle @css My mistake. :blank is for inputs. I meant :empty. :empty is defined to ignore white space in the newest spec, but the way it’s implemented in browsers, it doesn’t match in that case.

ollicle,
@ollicle@mastodon.social avatar

@simevidas @css ignoring white space is a welcome and useful change!

simevidas,
@simevidas@mastodon.social avatar

X:not(:has(:not(Y))):has(Y) = X that contains only Y

Is this a good pattern? Should I add it to the above list?

AmeliaBR,
@AmeliaBR@front-end.social avatar

@simevidas Ooof. That would definitely always require a comment to explain it & prevent someone (maybe yourself) from bungling it up in the future.

Definitely not for a list of "simple" :has selectors, but perhaps sometimes valid? Something like:

:is(ul, ol):not(:has(> :not(li))
/* find and flag any list that has non-list-item direct children */

simevidas, (edited )
@simevidas@mastodon.social avatar

@AmeliaBR I misremembered the terminology (https://drafts.csswg.org/selectors/#structure). What I meant is compound, not simple. I wanted to exclude complex selectors (e.g., X:has(Y) Z) to keep the list short.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • random
  • DreamBathrooms
  • mdbf
  • ethstaker
  • magazineikmin
  • GTA5RPClips
  • rosin
  • thenastyranch
  • Youngstown
  • osvaldo12
  • slotface
  • khanakhh
  • kavyap
  • InstantRegret
  • Durango
  • provamag3
  • everett
  • cisconetworking
  • Leos
  • normalnudes
  • cubers
  • modclub
  • ngwrru68w68
  • tacticalgear
  • megavids
  • anitta
  • tester
  • JUstTest
  • lostlight
  • All magazines