--- # vim: spell spelllang=en title: 'Embracing Modern Css' slug: 'embracing-modern-css' date: '2025-07-06T14:29:26+02:00' draft: true categories: - dev tags: - CSS summary: | entrer le résumé description: | entrer la description --- I recently stumbled upon a note on a page of [Plain Vanilla] in which I learned that nested CSS is a thing. It's a thing that's been around for quite some time, but I did not know about it (I'm quite late in my RSS reader). This allowed me to catch up on some of the recent evolutions of CSS. [Plain Vanilla]: https://plainvanillaweb.com/pages/styling.html ## Nested CSS Nesting CSS rules is one of the main reasons I've been using [SASS] for 15 years. I've always preferred to write nested rules to group together coherent units of CSS. For example, ```scss a { text-decoration: none; &:hover { text-decoration: underline; } } header { nav { ul { list-style: none; li { text-align: center; } } } } ``` makes more sense to me than ```css a { text-decoration: none; } a:hover { text-decoration: underline; } header nav ul { list-style: none; } header nav ul li { text-align: center; } ``` I have a few reasons for this: * It's easier to read because selectors are sorter and the hierarchy is easier to grasp; * I can move around a group of selector without forgetting a declaration; * I can use my IDE code folding based on indent to close a group and navigate long CSS files; Since the [CSS Nesting Module] is [Baseline Widely Available] and is supported by [90% of users], It can be used to write nested CSS. So now, this is a thing in CSS: ```css a { text-decoration: none; &:hover { text-decoration: underline; } } header { nav { ul { list-style: none; li { text-align: center; } } } } ``` The [CSS file] of this site has been rewritten using nested CSS. [SASS]: https://sass-lang.com [CSS Nesting Module]: https://drafts.csswg.org/css-nesting/ [Baseline Widely Available]: https://webstatus.dev/features/Nesting [90% of users]: https://caniuse.com/css-nesting [CSS file]: https://bcarlin.net/static/css/bcarlin.css ## CSS Variables To me, variables are essential to ensure a coherent user interface. They allow to reuse colors, sizes, spacing, and so on. This is also a reason why I've been using [SASS]. It allowed me to write CSS with reusable variables : ```scss $color-error: red; $color-success: green; label { &.error { color: $color-error; } &.success { color: $color-success; } } .notification { &.error { background-color: $color-error; } &.success { background-color: $color-success; } } ``` I missed the [CSS Custom Properties] module from 2017, which allowed me to write the same thing in pure CSS: ```css :root { --color-error: red; --color-success: green; } label { &.error { color: var(--color-error); } } .notification { &.error { background-color: var(--color-error); } } ``` [CSS Custom Properties]: https://www.w3.org/TR/css-variables/ ## Classless CSS Maybe I an going backwards here, given the popularity of utility-first CSS frameworks like [TailwindCSS]. This one is not really a CSS feature *per se*, but it is a way to write CSS, where semantically correct HTML is automatically styled correctly. To some extent, it is, however, backed by some [CSS Selectors Level 4] which are now Widely implemented across browsers, such as `:has`, `:is`, `:where`, `:not` and so on. I used to use [BootstrapCSS] in my projects because it is complete and easy to use, but I never liked the way it imposed a heavy CSS Structure on my source. For this site, I was looking for something lighter and came across [PicoCSS] which styles 90% of my site without changing anything to my templates. I already had a meaningful semantic HTML base structure: ```html