#cssGotcha

Ana Tudor 🐯anatudor
2024-10-24

`:has()` cannot be nested!

From the spec: `:has()` is not valid within `:has()`.

<div class='wrap'>
  <div class='item'><div class='a'></div>
  <div class='item'><div class='b'></div>
  <div class='item'><div class='a'></div>
</div>
<div class='wrap'>
  <div class='item'><div class='b'></div>
  <div class='item'><div class='a'></div>
  <div class='item'><div class='b'></div>
</div>
<style>
  /* selects *the only item* inside a wrap containing a */
  .item:nth-child(1 of :has(.a)):nth-last-child(1 of :has(.a)) {}
  /* doesn't work to select the wrap with only one item containing a */
  .wrap:has(.item:nth-child(1 of :has(.a)):nth-last-child(1 of :has(.a))) {}
</style>
Ana Tudor 🐯anatudor
2024-08-17

gotcha: if you set a list of gradient stops on an element:
.a { --l: var(--c0), var(--c1) }

... and expect to be able to get *a different gradient* if you just modify --c0 & --c1 on its .b children... I have bad news.😾

You need to set:
.a, .b { --l: var(--c0), var(--c1) }

Live demo on @codepen
codepen.io/thebabydino/pen/rNE

Particularly annoying when that gradient is used on tens of various children. 😿

Screenshot of the demo illustrating the problem: --c0 and --c1 are modified on the child divs, but the gradient still uses their values from the parent.
Ana Tudor 🐯anatudor
2024-07-19

(or gotcha) If you want to get the correct result when using smth like
atan2(var(--y), var(--x))
with --y and --x having different units...

then you need to register --x and --y as length values!

Or how I lost > 30min trying to figure out why my angle was 4° off...

.boo {
	--x: 1ch;
	--y: 8lh;
	/* at this point, the below
	 * > gives the incorrect result of 82° in Chrome because 
	 * 	 it just strips units and basicaly computes atan2(8, 1)
	 * > doesn't work at all in Firefox */
	--a: atan2(var(--y), var(--x));
  
	/* use this angle for gradients, etc. */
}

/* but if we register both --x and --y as <length> values */
@property --x {
	syntax: '<length>';
	initial-value: 0px;
	inherits: true
}

@property --y {
	syntax: '<length>';
	initial-value: 0px;
	inherits: true
}

/* then atan2(var(--y), var(--x)) = atan2(8lh, 1ch) 
 * gives the correct result of 86° cross-browser */

Client Info

Server: https://mastodon.social
Version: 2025.04
Repository: https://github.com/cyevgeniy/lmst