CSS Variables
Which CSS variables should you use for each project?
For example we can use these variables to style our section heading elements or the overall body like this:
Color variables
Select colors as described on this page. Define them in one place to make them easily reuseable, this way we do not have slight variations in color when do not want that, and it makes it easier for the overall design to feel coherent. And if needed we can change the colors at one place, this is especially useful when we want to have different color themes such as light and dark mode.
Typography variables
For the typography we define which font to use, the basics of its styling and which font sizes and weight we are going to use.
If you need to squeeze out some more loading speed you can optimize your font.
Spacing variables
Define spacing sizes. We want to keep reusing these values. Elements of the same group will be placed closer together, while items of different groups have larger spacing. By defining values we can achieve a design that looks much cleaner and does not look for feel arbitrary.
This 10 minute Youtube video on rules for good spacing goes deeper into how to apply these correctly.
Border variables
Define borders for reuse. Consider defining specific colors in addition to radius and width.
Shadow variables
Shadows for text-shadow
and box-shadow
.
Other variables
Consider adding variables for animations when you use them at multiple places on your website.
I also like to define the minimum height of my header and footer. This makes it easier to use properties like scroll-padding
, which we can use to make section header elements (<h1>, <h2>..
) show up below our header navigation after clicking an anchor (<a>
) element instead of behind it.
Multi color themes
Now the snippet only needs a few adjustments to also allow us to swap between different color profiles like light and dark mode.
(wip)
Sources & References
- On media queries:
- Kevin Powell on respinsive web design: https://youtu.be/x4u1yp3Msao
- Kevin Powell on using media queries and container queries: https://youtu.be/2rlWBZ17Wes
- MDN docs on media queries: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_media_queries/Using_media_queries
- CSS variables as used in the Starlight project on Github: https://github.com/withastro/starlight/blob/main/packages/starlight/style/props.css
- Kevin Powell general styling variables example on Astro blog project: https://youtu.be/Thudicbgqtg?t=1163
- On how to use spacing variables well for good spacing: https://youtu.be/9ElrcTtAxzA