css - 仅当子项未聚焦时才在悬停时更改父项的样式 - 样式化组件

标签 css styled-components

我有一个 div 组件:

const Parent = styled.div`
   &:not(${Input}:focus):hover {
     border-color: blue;
   }
`;

和输入的 child

const Input = styled.input``;

如您所见,如果 Input 获得焦点,我不想更改 Parent 上的 border-color。但是atm :hover 根本不起作用。寻求帮助,谢谢!

最佳答案

您可以利用 :focus-within 来做到这一点。

div {
  padding: 1em;
  margin: 2em;
  display: inline-block;
  border-width: 3px;
  border-style: solid;
}

div:hover {
  border-color: red;
}

div,
div:focus-within,
div:focus-within:hover {
  border: 3px solid blue;
}
<div>
  <input type="text">
</div>

关于css - 仅当子项未聚焦时才在悬停时更改父项的样式 - 样式化组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55628865/

相关文章:

reactjs - 如何将类型添加到主题旁边的组件自己的 Prop ?

javascript - 使用 Javascript 的实时日历(不是任何其他语言)

css - Styled-components:插值函数或 component.extend?

jquery - ui-sortable-handle 类添加到 edit.php WordPress

html - 在 Rails 中丢失图像 src

css - 有没有办法让文本粘在 div "border"上?

javascript - 样式组件中的响应式数组

webpack - 使用样式组件进行 Preact 会导致浏览器控制台出现错误

css - Bootstrap - 为什么我的按钮没有响应?

css - 单击 div 到底层元素