【问题标题】:How can I hover my ::after element in styled component如何将 ::after 元素悬停在样式组件中
【发布时间】:2019-10-10 20:24:28
【问题描述】:

我尝试使用样式组件悬停我的 ::after 元素,但它不起作用。

下面我粘贴了我的代码,我认为它应该可以正常工作,但它没有。

const Div = styled.div`
display:flex;
flex-direction:column;
justify-content:center;
align-items:center;
postition:relative;
&::after{
  content: '';
  background:rgba(255, 193, 5, 1);
  position:absolute;
  top:60%;
  left:-15%;
  width:20vw;
  height:20vw;
  border-radius:50%;
  &:hover{
    transform: scale(1.2);
  }
}
`

【问题讨论】:

  • 你不能悬停一个伪元素。您只能在父级悬停时应用样式。 div:hover::after { }

标签: css pseudo-element gatsby styled-components


【解决方案1】:

您不能在 ::after 伪元素上使用 :hover。您可以在this blog by Martin Wolf 中阅读更多相关信息。

你提供的代码不是最好的,因为你的项目没有太多的上下文,但它在 Sass 中应该看起来像这样(我假设你正在使用它?)。

.example-container {
    display:flex;
    flex-direction:column;
    justify-content:center;
    align-items:center;
    postition:relative;

    &::after{
        content: '';
        background:rgba(255, 193, 5, 1);
        position:absolute;
        top:60%;
        left:-15%;
        width:20vw;
        height:20vw;
        border-radius:50%;
    }

    &:hover {
        &::after {
            transform: scale(1.2);
        }
    }
}

【讨论】:

  • 谢谢!我完全阅读了我在文章中需要的内容。顺便说一句,我没有使用 Sass
猜你喜欢
  • 2019-05-28
  • 1970-01-01
  • 2016-02-29
  • 2012-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-05
  • 1970-01-01
相关资源
最近更新 更多