gpt4 book ai didi

css - :hover element change::after style another component

转载 作者:行者123 更新时间:2023-11-27 23:02:12 25 4
gpt4 key购买 nike

悬停在 StyledLink 上后,如何将 StyledLi::after 的宽度从 0% 更改为 90%

const StyledLink = styled(Link)`
text-decoration: none;
margin-right: 20px;
font-size: 20px;
color: grey;
transition: 0.2s;

:hover {
color: blue;
}
`
const StyledLi = styled.li`
::after {
content: "";
display: block;
width: 0%;
height: 2px;
background-color: blue;
}
`

最佳答案

试试这个。您可以使用绝对定位。在 StyledLink 中设置 StyledLi 的相对位置和 :after 的绝对位置。

第二种变体,为 StyledLi 使用伪元素,为 StyledLi 使用悬停。

您可以在下面看到两个示例。

const Container = styled.div``;

const List = styled.ul``;

const StyledLi = styled.li`
position: relative;
`;

const StyledLink = styled.a`
text-decoration: none;
margin-right: 20px;
font-size: 20px;
color: grey;
transition: 0.2s;

:after {
content: "";
position: absolute;
bottom: 0;
left: 0;
display: block;
width: 0%;
height: 2px;
background-color: blue;
}

:hover {
color: blue;
}

:hover:after {
width: 90%;
}
`;

const App = () => (
<Container>
<List>
<StyledLi>
<StyledLink>List Item</StyledLink>
</StyledLi>
<StyledLi>
<StyledLink>List Item</StyledLink>
</StyledLi>
<StyledLi>
<StyledLink>List Item</StyledLink>
</StyledLi>
</List>
</Container>
);

第二种变体

const Container = styled.div``;

const List = styled.ul``;

const StyledLi = styled.li`
color: grey;

:after {
content: "";
bottom: 0;
left: 0;
display: block;
width: 0%;
height: 2px;
background-color: blue;
}

:hover:after {
width: 90%;
}

:hover {
color: blue;
}
`;

const StyledLink = styled.a`
text-decoration: none;
margin-right: 20px;
font-size: 20px;
transition: 0.2s;
color: inherit;
`;

const App = () => (
<Container>
<List>
<StyledLi>
<StyledLink>List Item</StyledLink>
</StyledLi>
<StyledLi>
<StyledLink>List Item</StyledLink>
</StyledLi>
<StyledLi>
<StyledLink>List Item</StyledLink>
</StyledLi>
</List>
</Container>
);

关于css - :hover element change::after style another component,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58916343/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com