Get it?! Like “advice”, but for CSS.
When should you nest CSS?
There’s a simple answer and a slightly more complicated answer. The simple answer is “avoid nesting.” The more practical, but also more complex answer is “nest pseudo-selectors, parent modifiers, media queries, and selectors that don’t work without nesting.”
The big idea behind avoiding nesting (which is a native CSS feature now, if you hadn’t heard) is that it can lead to specificity increases that just aren’t necessary. Like:
.card {
.content {
.byline {
}
}
}
That .byline
selector probably doesn’t gain anything by being nested like that. Break it out of there and it’ll be more re-usable and easier to override if you need to.
But this:
.card {
@container (width > 60ch) {
}
}
Is probably good! It just saves you from having to re-write the .card
selector again. Scott gets more in-depth though with more examples and I largely agree.
How do you adjust an existing color light and darker?
I’m a biiiig fan of the relative color syntax, which is great at this job, but before I go on a tangent about that, it’s not well supported yet so let’s not. It’s on the Interop 2024 list though!
Better supported is color-mix()
, and Cory LaViska has the story on using it for this job:
Using
color-mix()
, we can adjust the tint/shade based on the background color, meaning we don’t need to manually select lighter/darker colors for those states. And because we’re using OKLCH, the variations will be perceptually uniform, unlike HSL.
By mixing white and black into colors, and doing it in the OKLCH color space, we can essentially tint and shade the colors and know that we’re doing it evenly across any color we have. This is as opposed to the days when a lot of us tried to use darken()
and such in Sass only to find extremely different results across colors.
How are the final values of Custom Properties calculated?
Custom properties – aka “CSS variables” – seem fairly straightforward. However, there are some behaviors to be aware of regarding how the browser computes the final values. A misunderstanding of this process may lead to an unexpected or missing value and difficulty troubleshooting and resolving the issue.
Custom Properties follow the cascade and are computed at runtime, for one thing, which is the whole reason that they cannot be preprocessed ahead of time. But it’s more complex than that. What if the value is valid for a custom property (most anything is), but not valid for the way you are trying to use it?
This is a real head scratcher:
html { color: red; }
p { color: blue; }
.card { --color: #notacolor; }
.card p { color: var(--color); }
Turns out .card p
will actually be red
(I would have guessed blue), but Stephanie explains:
The
.card p
will be the inheritedcolor
value ofred
as provided by thebody
. It is unable to use the cascaded value ofblue
due to the browser discarding that as a possible value candidate at “parse time” when it is only evaluating syntax.
How do you accommodate people who struggle with transparent interfaces?
Adam Argyle explains it can be like this, using this media query you may or may not have heard of:
.example {
--opacity: .5;
background: hsl(200 100% 50% / var(--opacity));
@media (prefers-reduced-transparency: reduce) {
--opacity: .95;
}
}
Adam had lots of practical examples in the post, and does consider that word reduced, and how it doesn’t mean absolutely none ever.
What units should you use for spacing properties?
Me, I just use rem
usually as that’s what I use for nearly everything else. But Ashlee M Boyer argues that while stuff like text makes good sense to use relative units, spacing need not scale at that same rate:
When a user is customizing their viewing experience, they thing that’s most important to them and their task at hand is the content. Spacing isn’t often vital for a user to perform their task, so it doesn’t need to grow or scale at the same rate as the content itself.
When spacing between content grows, it eats up vital real estate and becomes harder to manage.
Ashlee proves it with a before video and an after video, after being moving relative units to absolute units for spacing.
How do you make every Sass file automatically include common imports?
This one hits home for me, as someone with a codebase with easily hundreds of Sass files that all start with something like @import "@codepen/variables";
Wouldn’t it be cool if Sass could just assume we wanted to do that for every file?
Austin Gil covered this a while back on doing it with Vite. When you define your Vite config, you can like:
css: {
preprocessorOptions: {
scss: {
additionalData: `@import "@/assets/_shared.scss";`
}
}
}
I see webpack can do it too, but I’m not sure if Sass alone can be configured to do it, although I wish it could.