Applying CSS :focus-within
Gift Egwuenu

Gift Egwuenu @lauragift21

About: Developer Advocate at Cloudflare.

Location:
Netherlands
Joined:
May 15, 2017

Applying CSS :focus-within

Publish Date: Feb 13 '20
172 21

In my quest to learn something new every week. I came across a not so new pseudo-class element :focus-within. Let's take a look at how it works and how to apply it to our styles.

The :focus-within pseudo-class represents an element that is paired with the :focus pseudo-class or has a descendant that is matched by :focus.

NB: the focus here means when you click, tap or tab through an element such as a form input or a link.

I like how Chris Coyier explains it in simple terms:

It selects an element if that element contains any children that have :focus.

How is this different from :focus?

The :focus pseudo-element works differently, an element receives focus when the :focus element is applied to it, but, in a case when you have a collection of child elements it is not common to use :focus pseudo-class to select the parent element. The :focus pseudo-class applies only to the focused element itself.

This is what :focus-within is able to solve. We can use :focus-withinpseudo-class if we want to select an element that contains a focused element or elements that has descendants matched by :focus. Let's see how to achieve that.

Applying :focus-within

:focus-within is useful for different use-cases. You can think of using it:

  • To highlight an entire form when one of its input fields is in focus.
  • Highlight rows of a table and change the background color when a user clicks on it.

Here's an example, for highlighting an entire <form> container when the user focuses on one of its <input> fields.

Browser Support

As of 2020, This CSS feature is widely supported only IE browsers don't have support yet. Here's a table of current browser that fully supports :focus-within.

Can I use

Final Thoughts

The :focus-within element is an accessibility win for developers and users on the web. We should always keep it in mind while coding it's way better to reach for this than use JavaScript to accomplish the same task.

Resources

CSS Focus Within Spec
MDN Docs
CSS Tricks :focus-within

Originally published on my blog

Comments 21 total

  • Waylon Walker
    Waylon WalkerFeb 13, 2020

    This is such a cool psuedo element. Thanks for sharing. I will definitely be thinking of this in the future.

  • Vaibhav Khulbe
    Vaibhav KhulbeFeb 13, 2020

    CSS is coming up with such awesome pseudo-classes! There's a LOT to discover...thanks for sharing 😄

    • Gift Egwuenu
      Gift EgwuenuFeb 14, 2020

      I know right I'm having fun discovering them :)

  • Bugsy Sailor
    Bugsy SailorFeb 13, 2020

    Awesome tip! I did know this existed.

  • Maximiliano Ortiz
    Maximiliano OrtizFeb 13, 2020

    I definitely have used :focus in the past but I didn't know this one could be used for a different approach, CSS still has many ways to teach us new stuff everyday! I love it.

  • Gerardo Alcantara
    Gerardo AlcantaraFeb 13, 2020

    Great Post I will use it with the A11y compliance pin board
    pin.it/9RGxxAi

  • Calvin
    CalvinFeb 13, 2020

    This is lovely. Thanks

  • Chimezie Innocent
    Chimezie InnocentFeb 14, 2020

    Took me two reads to grasp it and its difference from :focus...Thanks for this, new knowledge acquired

  • Cyd
    CydFeb 14, 2020

    Yasss I love focus-within, I discovered one bug though. I usually use focus within to show the same state as hover so I use it like this:

    .ul {
      &:focus-within, &:hover {
        // code to show sub nav or something
      }
    }
    

    On edge though, not only is focus-within not supported, but it actually breaks the hover state! 🤯
    You have to add the hover on a separate line and shouldn’t use focus-within in combination with other selectors; as in comma separated on the same line.

    • Gift Egwuenu
      Gift EgwuenuFeb 14, 2020

      Oh really. I didn't know about this but thanks for sharing it.

    • Okechukwu Obi
      Okechukwu ObiFeb 20, 2020

      Is this not saas

  • Thomas Broyer
    Thomas BroyerFeb 15, 2020

    only IE browsers don't have support yet

    Yet? Yet⁉️

    🤣

    (Great post, thx)

  • Charbel Sarkis
    Charbel SarkisFeb 15, 2020

    Another tool added to my repertoire

  • Dani Riera
    Dani RieraFeb 18, 2020

    Great post!! Thanks for sharing!

  • Okechukwu Obi
    Okechukwu ObiFeb 20, 2020

    Very awesome gift ...I am definitely using this....and I though I know CSS at a high scale...lol

  • Priya Pareek
    Priya PareekApr 15, 2020

    How to insert the images in the post?

    • Priya Pareek
      Priya PareekApr 15, 2020

      Thank you so much for your reply but my question is how to keep the screenshot of our explanation in our post which we are posting on this website.

Add comment