Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A :focus-within workaround for edge

I am currently using the pseudo selector :focus-within in chrome however according to caniuse.com it's unavailable in Edge and IE, I found a neat workaround for it:

.focus-within-class > input:focus

However it combining it with focus-within like:

.focus-within > input:focus,
.focus-within:focus-within > input

does not work in Edge and the following solution does not work in chrome:

.focus-within > input:focus

How can I make :focus-within cross browser?

like image 671
Joss Bird Avatar asked Jun 10 '18 22:06

Joss Bird


People also ask

What is difference between focus and active?

:focus is when an element is able to accept input - the cursor in a input box or a link that has been tabbed to. :active is when an element is being activated by a user - the time between when a user presses a mouse button and then releases it.

Can I use focus visible?

Update – November 2021. The :focus-visible pseudo-class is now supported in all major browsers apart from Safari.

How do I focus in SCSS?

:focus is a pseudo-class used to select and style elements, usually links and form elements, that have been focused by the user, either by "tabbing" using the keyboard or by clicking. Form elements, such as <input> , <button> , and <textarea> can receive focus either by tabbing using the keyboard or by clicking.

What does focus within mean?

The :focus-within CSS pseudo-class matches an element if the element or any of its descendants are focused. In other words, it represents an element that is itself matched by the :focus pseudo-class or has a descendant that is matched by :focus . (This includes descendants in shadow trees.)


1 Answers

Couple of things. First, the "workaround" that you cite isn't really equivalent to :focus-within as the resulting rules apply to the <input> element. You could just as easily do

input:focus {
    /* rules here */
}

without bothering about the special classes. The real :focus-within pseudo-classs allows you to define rules that apply to the containing element, not the <input>

But if the particular workaround does have utility for you, you'll need to break your rules into separate blocks. That's because browsers silently ignore any CSS they don't understand. So when Edge sees

.focus-within > input:focus,
.focus-within:focus-within > input

it notices that there's a pseudo class that it doesn't understand (:focus-within), so it ignores the entire rule block, as you've noticed. If you split the rules into two blocks, Edge will only ignore the block it doesn't understand.

.focus-within > input:focus {
    /* rules here, which Edge will apply */
}

.focus-within:focus-within > input
    /* same rules here, which Edge will ignore */
}
like image 147
Stephen Thomas Avatar answered Nov 13 '22 17:11

Stephen Thomas