Useful CSS Tips And Techniques<\/h1>\nCosima Mielke<\/address>\n 2024-06-07T11:00:00+00:00
\n 2025-03-19T12:04:52+00:00
\n <\/header>\n
If you\u2019ve been in the web development game for longer, you might recall the days when CSS was utterly confusing and you had to come up with hacks and workarounds to make things work. Luckily, these days are over and new features such as container queries<\/a>, cascade layers, CSS nesting<\/a>, the :has<\/code> selector, grid and subgrid, and even new color spaces<\/a> make CSS more powerful than ever before<\/strong>.<\/p>\nAnd the innovation doesn\u2019t stop here. We also might have style queries<\/a> and perhaps even state queries<\/a>, along with balanced text-wrapping<\/a> and CSS anchor positioning<\/a> coming our way.<\/p>\nWith all these lovely new CSS features<\/strong> on the horizon, in this post, we dive into the world of CSS with a few helpful techniques, a deep-dive into specificity, hanging punctuation, and self-modifying CSS variables. We hope they\u2019ll come in handy in your work.<\/p>\nCascade And Specificity Primer<\/h2>\n
Many fear the cascade and specificity in CSS. However, the concept isn\u2019t as hard to get to grips with as one might think. To help you get more comfortable with two of the most fundamental parts of CSS<\/strong>, Andy Bell wrote a wonderful primer on the cascade and specificity<\/a>.<\/p>\n<\/p>\n <\/p>\n
<\/p>\n
<\/a>\n The cascade and specificity aren\u2019t as complex as one might think<\/a>. (Large preview<\/a>)
\n <\/figcaption><\/figure>\nThe guide explains how certain CSS property types will be prioritized over others and dives deeper into specificity scoring<\/strong> to help you assess how likely it is that the CSS of a specific rule will apply. Andy uses practical examples to illustrate the concepts and simplifies the underlying mental model to make it easy to adopt and utilize. A power boost for your CSS skills.<\/p>\nTesting HTML With Modern CSS<\/h2>\n
Have you ever considered testing HTML with CSS instead of JavaScript? CSS selectors<\/strong> today are so powerful that it is actually possible to test for most kinds of HTML patterns using CSS alone. A proponent of the practice, Heydon Pickering summarized everything you need to know about testing HTML with CSS<\/a>, whether you want to test accessibility, uncover HTML bloat, or check the general usability.<\/p>\n<\/p>\n <\/p>\n
<\/p>\n
<\/a>\n Heydon Pickering shows how to test HTML with CSS<\/a>. (Large preview<\/a>)
\n <\/figcaption><\/figure>\nAs Heydon points out, testing with CSS has quite some benefits. Particularly if you work in the browser and prefer exploring visual regressions<\/strong> and inspector information over command line logs, testing with CSS could be for you. It also shines in situations where you don\u2019t have direct access to a client\u2019s stack: Just provide a test stylesheet, and clients can locate instances of bad patterns you have identified for them without having to onboard you to help them do so. Clever!<\/p>\nSelf-Modifying CSS Variables<\/h2>\n
The CSS spec for custom properties does not allow a custom property to reference itself<\/strong> \u2014 although there are quite some use cases where such a feature would be useful. To close the gap, Lea Verou proposed an inherit()<\/code> function<\/a> in 2018, which the CSSWG added to the specs in 2021. It hasn\u2019t been edited-in yet, but Roman Komarov found a workaround<\/a> that makes it possible to start involving its behavior.<\/p>\n<\/p>\n <\/p>\n
<\/p>\n
<\/a>\n As we are waiting for inherit()<\/code> to arrive, Roman Komarov found a workaround that allows us to access the previous state of a property<\/a>. (Large preview<\/a>)
\n <\/figcaption><\/figure>\nRoman\u2019s approach uses container-style queries as a way to access the previous state of a custom property. It can be useful when you want to cycle through various hues<\/strong> without having a static list of values, to match the border-radius<\/code> visually, or to nest menu lists, for example. The workaround is still strictly experimental (so do not use it in production!), but since it is likely that style queries will gain broad browser support before inherit()<\/code>, it has great potential.<\/p>\nHanging Punctuation In CSS<\/h2>\n
hanging-punctuation<\/code> is a neat little CSS property. It extends punctuation marks such as opening quotes<\/strong> to cater to nice, clean blocks of text. And while it\u2019s currently only supported in Safari, it doesn\u2019t hurt to include it in your code, as the property is a perfect example of progressive enhancement: It leaves things as they are in browsers that don\u2019t support it and adds the extra bit of polish in browsers that do.<\/p>\n<\/p>\n <\/p>\n
<\/p>\n
<\/a>\n Jeremy Keith shares a little gotcha<\/a> to help you fix an unintended side effect of hanging punctuation. (Large preview<\/a>) (Image credit: MDN Web Docs<\/a>)
\n <\/figcaption><\/figure>\n
\n 2025-03-19T12:04:52+00:00
\n <\/header>\n
:has<\/code> selector, grid and subgrid, and even new color spaces<\/a> make CSS more powerful than ever before<\/strong>.<\/p>\nAnd the innovation doesn\u2019t stop here. We also might have style queries<\/a> and perhaps even state queries<\/a>, along with balanced text-wrapping<\/a> and CSS anchor positioning<\/a> coming our way.<\/p>\nWith all these lovely new CSS features<\/strong> on the horizon, in this post, we dive into the world of CSS with a few helpful techniques, a deep-dive into specificity, hanging punctuation, and self-modifying CSS variables. We hope they\u2019ll come in handy in your work.<\/p>\nCascade And Specificity Primer<\/h2>\n
Many fear the cascade and specificity in CSS. However, the concept isn\u2019t as hard to get to grips with as one might think. To help you get more comfortable with two of the most fundamental parts of CSS<\/strong>, Andy Bell wrote a wonderful primer on the cascade and specificity<\/a>.<\/p>\n<\/p>\n <\/p>\n
<\/p>\n
<\/a>\n The cascade and specificity aren\u2019t as complex as one might think<\/a>. (Large preview<\/a>)
\n <\/figcaption><\/figure>\nThe guide explains how certain CSS property types will be prioritized over others and dives deeper into specificity scoring<\/strong> to help you assess how likely it is that the CSS of a specific rule will apply. Andy uses practical examples to illustrate the concepts and simplifies the underlying mental model to make it easy to adopt and utilize. A power boost for your CSS skills.<\/p>\nTesting HTML With Modern CSS<\/h2>\n
Have you ever considered testing HTML with CSS instead of JavaScript? CSS selectors<\/strong> today are so powerful that it is actually possible to test for most kinds of HTML patterns using CSS alone. A proponent of the practice, Heydon Pickering summarized everything you need to know about testing HTML with CSS<\/a>, whether you want to test accessibility, uncover HTML bloat, or check the general usability.<\/p>\n<\/p>\n <\/p>\n
<\/p>\n
<\/a>\n Heydon Pickering shows how to test HTML with CSS<\/a>. (Large preview<\/a>)
\n <\/figcaption><\/figure>\nAs Heydon points out, testing with CSS has quite some benefits. Particularly if you work in the browser and prefer exploring visual regressions<\/strong> and inspector information over command line logs, testing with CSS could be for you. It also shines in situations where you don\u2019t have direct access to a client\u2019s stack: Just provide a test stylesheet, and clients can locate instances of bad patterns you have identified for them without having to onboard you to help them do so. Clever!<\/p>\nSelf-Modifying CSS Variables<\/h2>\n
The CSS spec for custom properties does not allow a custom property to reference itself<\/strong> \u2014 although there are quite some use cases where such a feature would be useful. To close the gap, Lea Verou proposed an inherit()<\/code> function<\/a> in 2018, which the CSSWG added to the specs in 2021. It hasn\u2019t been edited-in yet, but Roman Komarov found a workaround<\/a> that makes it possible to start involving its behavior.<\/p>\n<\/p>\n <\/p>\n
<\/p>\n
<\/a>\n As we are waiting for inherit()<\/code> to arrive, Roman Komarov found a workaround that allows us to access the previous state of a property<\/a>. (Large preview<\/a>)
\n <\/figcaption><\/figure>\nRoman\u2019s approach uses container-style queries as a way to access the previous state of a custom property. It can be useful when you want to cycle through various hues<\/strong> without having a static list of values, to match the border-radius<\/code> visually, or to nest menu lists, for example. The workaround is still strictly experimental (so do not use it in production!), but since it is likely that style queries will gain broad browser support before inherit()<\/code>, it has great potential.<\/p>\nHanging Punctuation In CSS<\/h2>\n
hanging-punctuation<\/code> is a neat little CSS property. It extends punctuation marks such as opening quotes<\/strong> to cater to nice, clean blocks of text. And while it\u2019s currently only supported in Safari, it doesn\u2019t hurt to include it in your code, as the property is a perfect example of progressive enhancement: It leaves things as they are in browsers that don\u2019t support it and adds the extra bit of polish in browsers that do.<\/p>\n<\/p>\n <\/p>\n
<\/p>\n
<\/a>\n Jeremy Keith shares a little gotcha<\/a> to help you fix an unintended side effect of hanging punctuation. (Large preview<\/a>) (Image credit: MDN Web Docs<\/a>)
\n <\/figcaption><\/figure>\n