[Robin]:<\/strong> My hunch is that 2022 will be The Year of the Container Query.<\/em> Everyone\u2019s excited about them, and for good reason: this new and fancy addition to CSS makes a component respond to whatever container they\u2019re placed in. For the first time ever we\u2019ll be able to create truly isolated components that can shrink and expand depending wherever you put them, like this<\/a>:<\/p>\n\n\n\n
See how this weather component has entirely changed because it\u2019s in a sidebar? That\u2019s the power of container queries right there.<\/p>\n\n\n\n
Although there\u2019s no word when they\u2019ll ship in browsers yet, it seems like they\u2019re gathering steam in the community and they\u2019re an official draft spec now<\/a>. I have a feeling that container queries are going to move fast<\/em> and ship in browsers pretty darn quickly (although this is just a hunch).<\/p>\n\n\n\n
But the big news of the week here is that Surma<\/a> made a polyfill<\/a>\u2014a chunk of code that lets other code work on older browsers\u2014and it rather magically allows us to use container queries today!<\/p>\n\n\n\n
To get started, we first need to load the script:<\/p>\n\n\n\n
\/\/ Test if container queries are supported already by the browser\nconst supportsContainerQueries = \"container\" in document.documentElement.style;\n\n\/\/ If not, load the polyfill\nif (!supportsContainerQueries) {\n import(\"https:\/\/cdn.skypack.dev\/container-query-polyfill\");\n}<\/code><\/pre>\n\n\n\n
(Although I would probably just download the polyfill and throw it in here instead of making another request to a CDN that can break, this is just a quick example.)<\/p>\n\n\n\n
Next up, we can write our HTML\u2026<\/p>\n\n\n\n
<div class=\"weather-wrap\">\n <dl class=\"weather\">\n <!-- the rest of our component markup goes here -->\n <\/dl>\n<\/div><\/code><\/pre>\n\n\n\n
We need that extra div of
.weather-wrap<\/code> to apply our container query logic to. Now, we can apply our styles that “activates” the container:<\/p>\n\n\n\n
.weather-wrap {\n container-type: inline-size;\n container-name: weather-wrapper;\n}<\/code><\/pre>\n\n\n\n
Two new CSS properties here. Fear not!<\/p>\n\n\n\n
When we say
container-type: inline-size<\/code> that translates to “I want this container query to be used on the inline axis (so for English-speaking languages that means left\/right or perhaps you can think of this as width).
inline-size<\/code><\/a> is an odd thing that I\u2019m still trying to get my head around (more on that in a future newsletter).<\/p>\n\n\n\n
.weather {\n display: flex;\n}\n\n@container weather-wrapper size(max-width: 700px) {\n .weather {\n flex-direction: column;\n }\n}<\/code><\/pre>\n\n\n\n
The way I think about it is prep work first: tell the browser you want a wrapper that acts as a container query, then somewhere else in your CSS you can give it instructions. It\u2019s a bit weird but not too<\/em> weird and unfamiliar. If you squint at the code above it does look odd but you can read it and parse it, even if you\u2019ve never seen anything like it before. And that\u2019s neat!<\/p>\n\n\n\n
If you\u2019re interested in reading more about this polyfill then I\u2019d recommend checking out Bramus\u2019s post on the matter<\/a> since it clarified a few things for me.<\/p>\n\n\n\n
\n\n\nThe UI Fund<\/h3>\n\n\n
Exciting news<\/a>:<\/p>\n\n\n\n
\n\n\nCSS Underlines Are Too Thin and Too Low in Chrome<\/h3>\n\n\n
Not so long ago this was a problem we\u2019d just have to silently weep over and then get on with our day. But I had completely forgotten that we can now wield the power of
text-decoration-thickness<\/code> and
text-underline-offset<\/code>, as \u0160ime Vidas shows in this great article about how to fix
link underlines with CSS<\/a> across browsers.<\/p>\n\n\n\n
<\/figure>\n\n\n\n
There\u2019s still a few bugs here though, as Vidas shows. But dang it\u2019s good to have these in your back pocket\u2014especially in odd cases where you want to edit the style on really big links on headers.<\/p>\n\n\n\n
\n\n\nA Small Guide for Naming Stuff in Front-end Code<\/h3>\n\n\n
Lots good stuff in here.<\/a> Frank Taylor has written his own set of guidelines for when he writes CSS in the future and a lot of it makes a ton of sense to me. Like this:<\/p>\n\n\n\n
This is why Block, Element, Modifier<\/a>, or BEM, became so popular\u2014just from looking at the CSS you could kind of visualize the markup that it applied to under the hood. Here Frank argues that we should write CSS so that we know how it\u2019s being applied, for example by writing something like this:<\/p>\n\n\n\n
<input type=\"submit\" class=\"jsSubmitButton\" \/><\/code><\/pre>\n\n\n\n
We can tell that there are no styles being applied, but that JavaScript is looking for it. I think something like this sounds a little obvious, perhaps a little silly, but it would be extremely helpful to see something like that when it comes to tests. Lots of front-end testing is done by searching for a class\u2014and I\u2019m beginning to think we should start writing something like this:<\/p>\n\n\n\n
<a class=\"test-checkout m-checkout\">Checkout<\/a><\/code><\/pre>\n\n\n\n
To show that, hey, our testing suite is looking for this specific element but also that\u2019s separate from our module styles,
m-checkout<\/code>, where colors and fonts and what not can be applied.<\/p>\n\n\n\n
\n\n\n
Future Fonts<\/h3>\n\n\n
This isn\u2019t an ad, but gosh darn it\u2019s sure going to sound like one. I love Future Fonts<\/a>\u2014it\u2019s where you can go and buy fonts from type designers at reduced prices early on in their development and then you\u2019ll get updates as they release changes. It\u2019s such a neat publishing model for fonts.<\/p>\n\n\n\n
<\/figure>\n\n\n\n
\n\n\n\n<\/a><\/figure>\n\n\n\n
Sponsor<\/p>\n\n\n
Is the new AWS RUM tool an industry game-changer? Let’s find out<\/a><\/h2>\n\n\n
Real User Monitoring for Amazon CloudWatch<\/a> was recently announced at AWS re:Invent 2021, adding to their existing suite of over 200 products and services. But how does it stack up? Today, we\u2019re going to try it out so you don’t have to.<\/p>\n\n\n\n