Why CSS :focus-within is amazing

I'm a full-stack developer from South Africa πΏπ¦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa πΏπ¦. I love writing about JavaScript, HTML and CSS.
Thanks for this comment Adam, Will keep sharing these cool finds with you guys
Thanks for making me aware of this. It comes with no surprise to me that IE does not support this. I'll be forever happy when the browser is not used anymore. :D
100% Let's hope this browser just disappears for good, not even mentioned in canIuse etc...
Yes, imaging looking at all those tables and there is no red column anymore!
Awesome! Can't believe I got to teach you something new π
Really nice article on the :focus-within pseudo-class! Really good use of it! π₯³
I would just like to make a few clarifications, if you don't mind.
::before and ::after are pseudo-elements and not pseudo-selectors. And technically :focus-within is referred to as a pseudo-class.
Pseudo-elements should generally be used with double :: to tell them apart from pseudo-classes which should be referred to with a single :. They are all used together in CSS selectors.
I have never used :focus-within. It's really interesting. Thank you, Chris, for writing this post.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

This is not a :focus selector, which will highlight when you focus on an element. But a selector to fire when something within is focussed.
In our case, we'll be focusing on forms (I see what you did there π)
The end result will look like this:

As for our HTML, we wil create a form with 2 inputs.
<form>
<label for="username">Username</label>
<input type="text" name="username" />
<br /><br />
<label for="password">Password</label>
<input type="password" name="username" />
</form>
That's all we need for this specific demo.
The :focus-within is a pseudo-selector, like :before or :after.
Let's first add some basic styling.
body {
display: flex;
min-height: 100vh;
justify-content: center;
align-items: center;
}
form {
border: 1px dashed #333;
padding: 25px;
}
This will make sure our form is absolutely centered on the page and has a small border to showcase our effect later on.
Now we want to draw the users attention to the form, but making it a fancy colour when they focus an input field.
form {
border: 1px dashed #333;
padding: 25px;
transition: background 0.3s ease;
}
form:focus-within {
background: #f4d35e;
}
This will change the background of our form to yellow.
You can see this in this Codepen.
Yes, we can even make it more awesome by using a box-shadow hack we can create a kind of modal effect!
form {
border: 1px dashed #333;
padding: 25px;
transition: all 0.3s ease;
box-shadow: 0 0 0 100vmax rgba(0, 0, 0, 0);
}
form:focus-within {
background: #f4d35e;
box-shadow: 0 0 0 100vmax rgba(0, 0, 0, 0.7);
}
We create a box-shadow that is 100% of the viewport biggest position (width or height). Initially, this will be at 0 opacity.
When we have a :focus-within we change the opacity to 70%.
The effect is a lightbox-like effect β¨.
The :focus-within selector actually has pretty good support, considering IE is dead already.

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter