Chris’ Corner: It DOM Matter

“Regardless of where it is in the DOM.” That’s a phrase that goes through my mind in regard to a number of new CSS features and it’s so cool. I certainly spent most of my formative HTML & CSSin’ years being very careful about where things needed to go in the DOM specifically because CSS […]

“Regardless of where it is in the DOM.”

That’s a phrase that goes through my mind in regard to a number of new CSS features and it’s so cool. I certainly spent most of my formative HTML & CSSin’ years being very careful about where things needed to go in the DOM specifically because CSS needed them to be there. It’s still important to care about DOM structure, putting things where they should be for accessibility reasons, but it’s nice that we don’t have to structure things according to limitations of the styling language so much anymore.

A big one here is :has().

Think of this:

body:has(input[type="checkbox"][switch]:checked) .menu {

}

Perhaps that toggle switch opens a menu. I don’t have to care where the toggle switch is in the DOM and I don’t have to care where .menu is in the DOM. Neither. That will still work. Wow. If we wanted to pull off trickery like this in the past, we’d have to rely on combinators in CSS like ~ and +, which meant that the elements needed to be siblings of one another which is pretty constraining tbqh. Here’s Amit pairing this stuff with a