Why you should be using new CSS features today - part 2
Why you should be using new CSS features today - part 2
By Brecht De Ruyte
7 min read
In the previous part of this article, I tried to tickle your brain a bit to unleash your curiosity and drive your determination to try some new CSS features today. That's exactly what I'll be doing in this part, writing some new stuff today so you can slowly polish new CSS skills very sneaky and hidden in the code, like a ninja.
- Authors
- Name
- Brecht De Ruyte
- linkedinBrecht De Ruyte
- twitter@utilitybend
- Website
- websiteBlog
Let’s talk browsers
Because of interop 2022/2023 browsers are working together to make our life easier. They have a list of new CSS features they want to release by the end of the year. Although some browsers favor one over the other. Safari for instance is very far ahead when it comes to the CSS color module level 5, while Firefox is implementing subgrid and Chrome is a step ahead on new pseudo selectors and scroll timelines.
When using new CSS features, it’s all about knowing which browsers you want to support, especially, which versions. For the sake of this article (and the world), I won’t be mentioning Internet Explorer as its lifecycle ended on June 15, 2022.
As Safari is now considered an evergreen browser as well (with auto-updates). We will consider most fallbacks based on the latest releases of browsers.
Some of the easy things we can do today
Sometimes, the easiest thing to do is to polyfill something for older browsers. I will mostly be referring to PostCSS when talking about polyfills, as I believe this to be one of the best tools out there at the moment for using new CSS features.
Here are a few of my favorite ones that you can use today:
CSS cascade layers
CSS cascade layers really released with a bang. Currently it has full support in all the major browsers, so you shouldn’t have any issues with them if you only support those.
However... When it comes to cascade layers there is a problem, and it’s quite a big one. When using them to full extent they are completely style-breaking for browsers that don’t support them. There is no graceful downgrade here and that could potentially be a big issue. Thankfully, we can use this polyfill for PostCSS in case we need to support older browsers. Later on when more people have updated to versions that support these layers, we can just remove the polyfill and see our full layers in action in our built CSS.
The polyfill will of course just rebuild your CSS to a version without cascade layers and this is how it works: It looks for any layer @-rules and records the order in which the layer is defined. The most specific selector gets remembered as well and with those two elements in place it will determine the specificity adjustments for each layer before it transforms the CSS. It will create some “fake extra specificity” by adding meaningless :not() pseudos to make it all work. Not the prettiest output, but it works perfectly.
div {
background: green;
}
@layer somelayer {
div {
background: red;
}
}
@layer someotherlayer {
div {
background: blue;
}
}
/* output css */
div:not(#\#):not(#\#) {
background: green; /* none layered has the highest specificity */
}
div {
background: red;
}
div:not(#\#) {
background: blue;
}
Not sure what cascade layers are? You can check out this article by Dave Bitter or view this video by Bram.us on CSS-day
Container queries
This is in my opinion one of the harder things to polyfill at the moment. There isn’t any support for this in Firefox and Opera for now, which means we have to use a polyfill if we want to use it.
There is a polyfill by Chromelabs which can be found here but there are some issues with it that can’t go unnoticed.
The author of the polyfill states: “For the best user experience, it's recommended that you initially only use the polyfill for content below-the-fold” and that comes with a reason.
The polyfill only triggers after the DOM has loaded and when the browser loads the javascript based on the support flag. You will notice a layout shift when this happens and I’m not really happy to see things like that happen on an evergreen browser. Sure, when firefox has support and you only use it to target older versions, it might be a good idea. But for now, maybe it’s best to use this when implementing something extra for browsers that support it? I’ll leave that up to you. You can always use a feature query in CSS for this:
@supports (container-type: inline-size) {
/* container queries stuff here */
}
Or if you really want to go with a polyfill, you could temporarily show a loader while the plugin is being loaded like so:
@supports not (container-type: inline-size) {
.container {
display: none;
}
.loader {
display: flex;
}
}
There is a bit of good news though: Since November 2022, container queries are available in Firefox Nightly behind a flag.
Want to read more about this feature? Check out this article about everything container queries by Maarten Van Hoof
The :has() relational pseudo class
This is currently supported in all browsers besides Firefox and in simple use-cases it can be easily fixed by your own hand. I actually have done this before when toggling a pane with Javascript.
The CSS looked something like this:
body.panel-open {
overflow-y: hidden;
}
@supports (selector(:has(*))) {
body:has(.info-pane.is--open) {
overflow-y: hidden;
}
}
Inside the JS, I added the following when triggering the panel to open:
if (!CSS.supports('selector(:has(*))')) {
document.body.classList.toggle('panel-open')
}
Is this an unnecessary step? Absolutely! I could have just used the body class for both cases. But I did gain a bit more experience with using this relational pseudo class in practice and that’s what matters here. In case you missed out on what this pseudo class has to offer. I wrote about it on the tech_hub before.
Logical properties
Logical properties already have great support in all browsers, but I wanted to mention it anyway. The biggest flaw that they have is that they don’t downgrade very well. I’m mostly thinking about all the old iPads floating around. I had this problem before where we found out that we need to polyfill this for older devices as there were just too many of those floating around.
Luckily, fixing this this is easy by adding postcss-logical to our PostCSS config which will convert margin-block to a margin-top and bottom, with a direction pseudo attached to it (if needed). There is a small trade-off as there won’t be any shorthand created. Nevertheless, if you’re working on a project with multiple writing modes that has to support older browsers, this is the way to go.
CSS nesting
It’s only been recently since this became available in Chrome Canary and the spec is still changing, so I would not use this in your day-to-day projects. But it’s worth to take a look at it as this is something people have been asking for.
But maybe if you need to create a little one-pager or something internal for your coworkers that won’t see the light of day for long, you can always try using this postCSS plugin when building your files. It’s not perfect, but it works pretty great and it’s good practice.
Why should you play with CSS nesting if you’ve been using Sass for years? Well, take a look at the spec, there are quite a few differences.
A bit of trivia: Currently they are experimenting to remove the use of an ampersand (&) when the selector doesn’t start with an alphabetical character, how cool is that?! You can test this in Chrome Canary with the web experiments flag on.
Honorable mention: Masonry
I keep on mentioning masonry as one of the things I look the most forward to in new CSS features. Maybe that’s because I’ve hacked more Javascript (and even jQuery) libraries in the past than I can remember. It’s currently only available in Firefox Nightly behind a flag but if you need a simple masonry in the future, why not use a feature query? That way you can easily remove that big JS library later on.
@supports (grid-template-rows: masonry) {
.masonry-grid {
display: grid;
gap: 5px;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: masonry;
}
}
const supportMasonry = CSS.supports('grid-template-rows', 'masonry')
if (!supportMasonry) {
/*
dont't support masonry in CSS?
init your masonry here in JS
*/
}
I do hope we see some progress in this feature soon as it has been over a year since I first played around with it.
As a final conclusion
So why should we be using new CSS features today?
- Because we can
- Because we can learn and be a step ahead
- Because it helps us to write better code in the future / for the future
And last, but not least. You can get involved by filing issues and commenting at the CSS working group Github. Everything CSS and HTML is very open.
If there is one thing I want people to keep an eye on in 2023, then it must be open-ui.org. Which will make us finally be able to style things such as tooltips, select menus and more without the use of JS. They do this with an eye on accessibility and it looks very promising. Currently available in Chrome Canary behind a flag (but still quite buggy). I’ve taken a commitment to help them more in the future by joining meetings and creating demos.
So are you excited about all this new stuff in CSS? Feel free to have a chat! I love to geek out on these things.
And as this might be my last article on the tech_hub of the year, I Wish you all a Merry Christmas and a happy New Year.
.christmas:has(.snow) {
list-style: '❄️';
list-style-position: outside;
}
Upcoming events
The Test Automation Meetup
PLEASE RSVP SO THAT WE KNOW HOW MUCH FOOD WE WILL NEED Test automation is a cornerstone of effective software development. It's about creating robust, predictable test suites that enhance quality and reliability. By diving into automation, you're architecting systems that ensure consistency and catch issues early. This expertise not only improves the development process but also broadens your skillset, making you a more versatile team member. Whether you're a developer looking to enhance your testing skills or a QA professional aiming to dive deeper into automation, RSVP for an evening of learning, delicious food, and the fusion of coding and quality assurance! 🚀🚀 18:00 – 🚪 Doors open to the public 18:15 – 🍕 Let’s eat 19:00 – 📢 First round of Talks 19:45 – 🍹 Small break 20:00 – 📢 Second round of Talks 20:45 – 🍻 Drinks 21:00 – 🙋♀️ See you next time? First Round of Talks: The Power of Cross-browser Component Testing - Clarke Verdel, SR. Front-end Developer at iO How can you use Component Testing to ensure consistency cross-browser? Second Round of Talks: Omg who wrote this **** code!? - Erwin Heitzman, SR. Test Automation Engineer at Rabobank How can tests help you and your team? Beyond the Unit Test - Christian Würthner, SR. Android Developer at iO How can you do advanced automated testing for, for instance, biometrics? RSVP now to secure your spot, and let's explore the fascinating world of test automation together!
| Coven of Wisdom - Amsterdam
Go to page for The Test Automation MeetupCoven of Wisdom - Herentals - Winter `24 edition
Worstelen jij en je team met automated testing en performance? Kom naar onze meetup waar ervaren sprekers hun inzichten en ervaringen delen over het bouwen van robuuste en efficiënte applicaties. Schrijf je in voor een avond vol kennis, heerlijk eten en een mix van creativiteit en technologie! 🚀 18:00 – 🚪 Deuren open 18:15 – 🍕 Food & drinks 19:00 – 📢 Talk 1 20:00 – 🍹 Kleine pauze 20:15 – 📢 Talk 2 21:00 – 🙋♀️ Drinks 22:00 – 🍻 Tot de volgende keer? Tijdens deze meetup gaan we dieper in op automated testing en performance. Onze sprekers delen heel wat praktische inzichten en ervaringen. Ze vertellen je hoe je effectieve geautomatiseerde tests kunt schrijven en onderhouden, en hoe je de prestaties van je applicatie kunt optimaliseren. Houd onze updates in de gaten voor meer informatie over de sprekers en hun specifieke onderwerpen. Over iO Wij zijn iO: een groeiend team van experts die end-to-end-diensten aanbieden voor communicatie en digitale transformatie. We denken groot en werken lokaal. Aan strategie, creatie, content, marketing en technologie. In nauwe samenwerking met onze klanten om hun merken te versterken, hun digitale systemen te verbeteren en hun toekomstbestendige groei veilig te stellen. We helpen klanten niet alleen hun zakelijke doelen te bereiken. Samen verkennen en benutten we de eindeloze mogelijkheden die markten in constante verandering bieden. De springplank voor die visie is talent. Onze campus is onze broedplaats voor innovatie, die een omgeving creëert die talent de ruimte en stimulans geeft die het nodig heeft om te ontkiemen, te ontwikkelen en te floreren. Want werken aan de infinite opportunities van morgen, dat doen we vandaag.
| Coven of Wisdom Herentals
Go to page for Coven of Wisdom - Herentals - Winter `24 editionMastering Event-Driven Design
PLEASE RSVP SO THAT WE KNOW HOW MUCH FOOD WE WILL NEED Are you and your team struggling with event-driven microservices? Join us for a meetup with Mehmet Akif Tütüncü, a senior software engineer, who has given multiple great talks so far and Allard Buijze founder of CTO and founder of AxonIQ, who built the fundaments of the Axon Framework. RSVP for an evening of learning, delicious food, and the fusion of creativity and tech! 🚀 18:00 – 🚪 Doors open to the public 18:15 – 🍕 Let’s eat 19:00 – 📢 Getting Your Axe On Event Sourcing with Axon Framework 20:00 – 🍹 Small break 20:15 – 📢 Event-Driven Microservices - Beyond the Fairy Tale 21:00 – 🙋♀️ drinks 22:00 – 🍻 See you next time? Details: Getting Your Axe On - Event Sourcing with Axon Framework In this presentation, we will explore the basics of event-driven architecture using Axon Framework. We'll start by explaining key concepts such as Event Sourcing and Command Query Responsibility Segregation (CQRS), and how they can improve the scalability and maintainability of modern applications. You will learn what Axon Framework is, how it simplifies implementing these patterns, and see hands-on examples of setting up a project with Axon Framework and Spring Boot. Whether you are new to these concepts or looking to understand them more, this session will provide practical insights and tools to help you build resilient and efficient applications. Event-Driven Microservices - Beyond the Fairy Tale Our applications need to be faster, better, bigger, smarter, and more enjoyable to meet our demanding end-users needs. In recent years, the way we build, run, and operate our software has changed significantly. We use scalable platforms to deploy and manage our applications. Instead of big monolithic deployment applications, we now deploy small, functionally consistent components as microservices. Problem. Solved. Right? Unfortunately, for most of us, microservices, and especially their event-driven variants, do not deliver on the beautiful, fairy-tale-like promises that surround them.In this session, Allard will share a different take on microservices. We will see that not much has changed in how we build software, which is why so many “microservices projects” fail nowadays. What lessons can we learn from concepts like DDD, CQRS, and Event Sourcing to help manage the complexity of our systems? He will also show how message-driven communication allows us to focus on finding the boundaries of functionally cohesive components, which we can evolve into microservices should the need arise.
| Coven of Wisdom - Utrecht
Go to page for Mastering Event-Driven Design