
Tell me, do you dropdown? Maybe you popout? Popover? Well, whatever your inclination, if you're a developer trying to reveal content from user interaction you should read this. Put down that JavaScript and check out these new and upcoming features coming to HTML and CSS. Let's pop off, queen.
For as long as I can remember, it's never been super easy or simple to code a button to reveal a piece of content without thinking "what if". What if the user clicks outside the button? Press escape? Animation?? The list goes on. With that list, additional JavaScript comes along for the ride to remedy these what-ifs.
Forgive my cynicism, but such a simple concept should be reasonably possible with HTML and a sprinkle of CSS. We have <details> and <summary> (I love you two) but we need more. Well, thankfully, we have a slew of features that have either landed or are on the way to HTML and CSS to remedy these issues.
The popover attribute has recently been made available in all browsers. Coupled with the popovertarget attribute, they allow interactive elements to reveal and hide other elements in a similar fashion to how the <dialog> element behaves, but without any JavaScript needed.
<button popovertarget="popover">Open popover</button>
<div popover id="popover">Hello world!</div>These attributes can be used with pretty much any element, however it's recommended you use them in conjunction with the aforementioned <dialog> element for semantics. Check out this article from Hidde de Vries, a design systems consultant, on the ideal semantics for the popover attribute.
Popovers allow us to reveal and hide content, but what about positioning it relative to the interacted element? That's where CSS Anchor comes in, a way to help developers robustly position elements relative to each other. Michelle Barker expertly demonstrates these features together in her article on CSS IRL.
<style>
button {
anchor-name: --button;
}
[popover] {
position-anchor: --button;
top: anchor(--button bottom);
left: anchor(--button left);
}
button, [popover] {
margin: 0;
}
</style>
<button popovertarget="my-popover">Open Popover</button>
<div popover id="my-popover">Greetings, one and all!</div>While popover is now stable in all browsers, CSS Anchoring is still in the works for some browsers. Determined to use CSS Anchoring in your project? You can utilize this polyfill by the folks at OddBird for improved browser compatibility.
calc-size()This is a big one and somewhat ties in with the heritage of our company name. For eons, frontend developers have been challenged by seemingly a trivial task, animating the size of an element to and from auto. With this new feature, not only will we be able to use auto height in CSS but also use it with other values. You can read more about it here on MDN.
It's early days for this new feature, but I felt it was important to share it alongside previously mentioned features. In case you were wondering, zeroheight is named after a CSS bug that causes elements to appear with "0 height", something we'll be able to animate to and from with auto height with this new function.
As I mentioned earlier, <details> and <summary> are pretty great at creating a simple JavaScript-free toggle to hide and reveal content. However, it's not perfect. Animating it is difficult, as well as modifying its presentation or layout.
Marker styling is one of the options coming up for <details> where you'll be able to style the arrow beside the toggle in any way you want.
Up until writing this I presume no updated were planned, but I was wrong! Stephanie Stimac has put together a great article that goes into better detail than I have.
Design systems developers rejoice! How many times have you battled with the <select> element to create accurate UI for the designs you've been supplied? I know I have on a few occasions, resulting in me going back to the designer, tail between my legs, to let them know it's an all-or-nothing task.
<style>
select,
::picker(select) {
appearance: base-select;
}
</style>
<select>
<button>
Select
<selectedoption></selectedoption>
</button>
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
</select>Well, just like <details> and <summary>, the <select> element is getting some love to help UI people create more elaborate custom designs for their dropdown selects. If you want to learn more check out this article from Jake Archibald from Shopify.
So, what features have most impressed you? Have you used any of these in production code? We'd love to know via our social media. I hope you found this roundup helpful!