A :focus-within workaround for edge

强颜欢笑 提交于 2020-01-03 08:52:41

问题


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 on codepen 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?


回答1:


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 */
}


来源:https://stackoverflow.com/questions/50788545/a-focus-within-workaround-for-edge

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!