简体   繁体   English

CSS:将焦点样式应用于未聚焦的元素?

[英]CSS: apply :focus style to unfocused element?

I have a fieldset that needs to imitate the appearance of components from a library, but I can't directly copy-paste its styling into the css for that div, since if the library theme gets changed then that copy-pasted css won't match with the other components.我有一个需要模仿库中组件外观的字段集,但我不能直接将其样式复制粘贴到该 div 的 css 中,因为如果库主题发生更改,那么复制粘贴的 css 将不会与其他组件匹配。 I was more or less doing this by applying the css classes used by that library (eg library-class ) to non-library elements like so (I'm using react):我或多或少地通过将该库(例如library-class )使用的 css 类应用于非库元素(我正在使用反应)来做到这一点:

return (
<fieldset className="library-class">
    <input/>
    <input/>
    <input/>
</fieldset>);

My issue is that the library applies this box shadow to input elements in focus, but I need my whole fieldset to show that box shadow - rather than the inputs inside - when any element inside is focused.我的问题是库将此框阴影应用于焦点输入元素,但我需要我的整个字段集来显示框阴影 - 而不是里面的输入 - 当里面的任何元素被聚焦时。 The browser debug css looks like this:浏览器调试 css 如下所示:

library-class:focus{
    box-shadow: #c1c1c1 0 0 1px;
}

Is there a way to forcefully apply the:focus pseudo class to my whole fieldset?有没有办法将:focus 伪 class 强制应用到我的整个字段集中?

You can use the :focus-within pseudo-class:您可以使用:focus-within伪类:

 .library-class:focus-within { box-shadow: #f90 0 0 0.5em; }
 <fieldset class="library-class"> <input/> <input/> <input/> </fieldset>

References:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM