Smashing Magazine - #362: CSS

With defensive CSS, conditional styles, CSS styles queries and CSS variables secrets. Issue #362 July 19, 2022 View in the browser 💨

Smashing Newsletter

Buenos días Smashing Friends,

The state of CSS in 2022 is nothing short of breathtaking. With so many features being built and shipped regularly in modern browsers, it might feel like it’s an early Christmas almost every month. From cascade layers now supported in all modern browsers to :has and container queries landing soon.

2014 Material Design color palettes
Coming soon: color-contrast will ask browsers to pick an accessible color on its own.

In this newsletter, we look at some fine details of CSS: from some of the hidden treasures of CSS variables to how to make CSS more reliable and flexible with color-mode and defensive CSS. We hope you’ll find some useful snippets here.

You will also find plenty of articles all around CSS in our CSS section on SmashingMag, and of course, CSS will be the topic debated and discussed in fine detail in our upcoming SmashingConfs. But for now, happy reading, and happy CSSing, everyone!

Vitaly (@smashingmag)


1. Defensive CSS

When we build our sites and applications, we often have a particular mock-up in mind. In that mock-up, words often have the almost perfect length, images are perfectly sized, and the content is available all the way. In practice though, it’s not always going to be the case. Wording quickly becomes lengthy, the size of images changes, and on suboptimal connections, the page renders with plenty of unexpected layout shifts.

Defensive CSS

Defensive CSS tries to fix that. Ahmad Shadeed’s new project provides practical CSS tips that help in building scalable, future-proof user interfaces. Ahmad provides plenty of useful tips on common challenges such as text appearing over an image, component spacing, and long content. A great resource worth learning to ensure that we don’t accumulate technical debt when building our UI components. (vf)


2. CSS Variables Secrets

We think that CSS Custom Properties are just that: a few CSS variables that we can dynamically adjust in the browser. As it turns out, they can do so much more. Our friends at CSS Day from Amsterdam have released videos of CSS Day talks to watch for free. One of them is Lea Verou’s deep-dive on CSS Variables Secrets.

CSS Variables Secrets

In the talk, Lea goes into fine detail showing four demos and uncovering some technical gotchas of using CSS variables extensively for the outlined button, the glossy button, the bar chart and the speech bubble. Worth watching!

As a bonus, you can also look into Andy Bell’s session on “Be the browser’s mentor, not its micromanager”, in which Andy goes into detail on all things CSS layout, fluid type, space, and modern CSS capabilities to build resilient frontends. (vf)


From our sponsor

Manage Your Next Project With Basecamp

Manage Your Next Project With Basecamp
Basecamp is the all-in-one toolkit for working remotely. Organize teams, projects, schedules, files, and everything else your team needs. Try managing your next project with Basecamp. We’ll even give you one free month to try it.


3. Conditionally Styling Components With CSS

Whenever we want to introduce some logic to CSS, chances are high that we’ll be experimenting with checkboxes and radio buttons. In fact, if you want to change the styling of components conditionally, based on the number of siblings, you could use quantity selectors in CSS or CSS comparison functions.

Conditionally Styling Components With CSS

But what if you wanted to change the styling based on how many checkboxes are checked? Preethi’s article on Conditionally Styling Selected Elements goes into all the fine details on how to make it work by combining :before and :after pseudo-elements with :nth-of-type selectors.

A fascinating read with plenty of convoluted selectors — but it works! You can also check the demo with a calendar using that technique. Genius! (vf)


4. Upcoming Online Workshops

That’s right! We run online workshops on frontend and UX, be it accessibility, performance, navigation, or landing pages. In fact, we have a couple of workshops coming up soon, and we thought that, you know, you might want to join in as well.

Smashing Online Events
With online workshops, we aim to give you the same experience and access to experts as in an in-person workshop from wherever you are.

As always, here’s an overview of our upcoming workshops:


5. Better Multi-Lingual Websites With CSS

Imagine that you are working on a project which has to support Arabic, Hebrew and Persian languages. The way you write your CSS needs to accommodate these languages, and this goes for all the fine details, i.e. from margins and paddings to alignment and fonts. Where do you start?

Better Multi-Lingual Websites With CSS

In “How to make your multilingual website suitable for RTL with only HTML and CSS,” Declan Rek highlights a few critical adjustments you’ll need to make: from CSS logical properties to Unicode ranges.

And if you need a slightly more comprehensive guide, Ahmad Shadeed has published RTL Styling 101, a very comprehensive one with common mistakes, use cases, examples and RTL design considerations. (vf)


6. Better Scrolling With CSS

The scrollbar seems to be one of those little things that we can’t do much with. Surely we can change the color of the scrollbar with scrollbar-color and its width with scrollbar-width, but that’s pretty much it, isn’t it?

Better Scrolling With CSS

Well, not quite. If a website supports a dark theme, we probably want the scrollbar to be visible there as well. We can use the color-scheme property or meta tag to defer to OS preference and override it based on the user’s selected theme. You can also find a 10-min video introduction to color-scheme by Zoran Jambor.

We can also prevent layout shifts with scrollbar-gutter: stable and conditionally style scrollbars based on whether the user is on a high-accuracy pointing device (e.g. mouse) or a low-accuracy one (e.g. touchscreen) with @media (pointer: fine) {}. You can find a lot more in Mayank’s detailed blog post on “Better scrolling through modern CSS”, with plenty of useful tips and techniques — all related to scrollbars.

Ah, and if you want even more fun with scrolling, Lynn Fisher has got you covered with a piece on Fun Parallax Scrolling with CSS. (vf)


From our sponsor

Northwestern’s Online MS in Information Design and Strategy

Northwestern’s Online MS in Information Design and Strategy
Prepare for a range of dynamic communication roles and build the skills needed to lead communication strategy, translate complex data, and drive user interactions. Choose from specializations in content strategy, UX/UI, communication with data, and learning design. Learn more.


7. CSS Style Queries

For years, we’ve been dreaming of the ability to query container width rather than the browser width. Container queries are now coming to the browsers, landing in Chrome 106 and Safari 16 later this year. That’s a major milestone, of course, but why stop there?

CSS Style Queries

As Una Kravets writes in “Style Queries,” we could also allow querying the computed values of the container in which a component lives. For example, if the background of the parent container is black, you might want to change the text color to white. Or we could change the styles of the children based on the container’s flex-direction. Or even query CSS custom properties and change the layout based on them.

Of course, we aren’t quite there yet, and the main focus lies on shipping container queries first, but if you can think of some specific use cases where they would come in handy, leave a note on Una’s post or underneath Bramus’ thread on Twitter. (vf)


8. Previous Sibling CSS Selector

With the relational :has selector landing in browsers, we can use it to target the parents of a child. For example, if we want to select buttons that have an element with .icon as a child, we could do that with button:has(.icon) { }. And if we wanted to select article elements that have an h2 element followed by a paragraph element, we could do that with article:has(h2 + p) { }. But what if we wanted to select all previous siblings of an element?

Previous Sibling CSS Selector

For example, if we wanted to select every link that is the only child but also lives within a paragraph that shares the same parent with hr? We could do that with p:has(+ hr) a:only-child { }.

In “A Previous Sibling Selector,” Jim Nielsen shows the use case where he came up with this selector, and even provides a CodePen demo of it at work. At the moment, :has is only supported in Safari, but its support is steadily growing, landing in Chrome 105–106. Heads up to Stefan for the link! Neat! (vf)


9. New On Smashing Job Board


10. Recent Smashing Articles


That’s All, Folks!

Thank you so much for reading and for your support in helping us keep the web dev and design community strong with our newsletter. See you next time!


This newsletter issue was written and edited by Cosima Mielke (cm), Vitaly Friedman (vf) and Iris Lješnjanin (il).


Sent to truly smashing readers via Mailchimp.
We sincerely appreciate your kind support. You
rock.

Follow us on Twitter Join us on Facebook

unsubscribe update preferences view in your browser

Key phrases

Older messages

#361: State of the Industry

Tuesday, July 12, 2022

With adaptive pointers, the state of design systems, front-end, component encyclopedia and service design tools. Issue #361• July 12, 2022 • View in the browser 💨 Smashing Newsletter Goedendag Smashing

#360: Web Performance

Tuesday, July 5, 2022

On priority hints, third-parties, speculative prerendering, dealing with LCP and preload scanner. Issue #360• July 5, 2022 • View in the browser 💨 Smashing Newsletter Namastē Smashing Friends,

#359: A Mixed Bag of Goodies

Tuesday, June 28, 2022

From book covers and machine learning to accessibility and moodboards. A mix of goodies for everyone! Issue #359• June 28, 2022 • View in the browser 💨 Smashing Newsletter Hyvää päivää Smashing Friends

#358: Design Tools and Advice

Tuesday, June 21, 2022

With tips on colors in design systems, comparing design tools and taking a closer look at how emotional design works. Issue #358• June 21, 2022 • View in the browser 💨 Smashing Newsletter Ahoy Smashing

#357: The Web is Fun!

Tuesday, June 14, 2022

With exciting and obscure websites — some of them not necessarily useful but remind us why we love the web so much in the first place. Issue #357• June 14, 2022 • View in the browser 💨 Smashing

You Might Also Like

Car UX, DevTools Tips, CSS Patterns, iOS404, Internet Cables

Thursday, April 18, 2024

The 5 best design links, every day. Curated by a selection of great editors. Email not displaying properly? View browser version. Sidebar April 18 2024 Steering the future: a new vision for car UX

Big Tents, Pistachio Palettes, and Other Late-Breaking Milan Discoveries

Wednesday, April 17, 2024

View in your browser | Update your preferences We've had a remodel! From now on, you'll be hearing from AD PRO in your inbox twice a week—once with a deep dive into trends to watch and subjects

163 / Dieter Rams inspired Framer components, America's national parks and more free resources…

Wednesday, April 17, 2024

Product Disrupt Logo Product Disrupt Half-Monthly Apr 2024 • Part 1 View in browser Welcome to Issue 163! I'm comfortable talking to a human on camera, but talking at the camera, not so much. The

Logo System, BCWA Identity, War Robots, 84—24, 19th-Century Atlas

Wednesday, April 17, 2024

The 5 best design links, every day. Curated by a selection of great editors. Email not displaying properly? View browser version. Sidebar April 17 2024 Logo System: Explore 400+ logo designs logosystem

Emma Roberts's Home: AD100 Firm Pierce & Ward Share Their Secret Sources

Tuesday, April 16, 2024

Plus, the best exterior house colors View in your browser | Update your preferences Architectural Digest AD PRO Logo exterior of two residential buildings painted with blue trim with a brick patio and

#452: Design Workflow

Tuesday, April 16, 2024

How to choose the right idea, navigate difficult conversations and communicate design. Issue #452 • Apr 16, 2024 • View in the browser Smashing Newsletter Iyi akşamlar Smashing Friends, Design workflow

Mario & Pareto, faces.js, Sleep vs Blog, Dark Mode, DOM Folding

Tuesday, April 16, 2024

The 5 best design links, every day. Curated by a selection of great editors. Email not displaying properly? View browser version. Sidebar April 16 2024 Mario meets Pareto mayerowitz.io Discover how to

Gap, Teenagers, Artistic Intelligence, Should Designers Code, CSS in RSCs

Tuesday, April 16, 2024

The 5 best design links, every day. Curated by a selection of great editors. Email not displaying properly? View browser version. Sidebar April 15 2024 Gap is the new Margin frontendmasters.com In 2020

How to Combat Allergies at Home—Designers Weigh In

Monday, April 15, 2024

Plus, 5 gorgeous greenhouses from the AD Archive View in your browser | Update your preferences Architectural Digest AD PRO Logo a conservatory-style greenhouse at the back of a lush floral garden

Accessibility Weekly #392: Socks, Lies, and Accessibility

Monday, April 15, 2024

April 15, 2024 • Issue #392 View this issue online or browse the full issue archive. Featured: Socks, lies, and accessibility "Global Accessibility Awareness Day (GAAD) is around the corner.