[英]select previous element and apply effect using css
I have 3 <div>
s in one horizontal line. 我在一条水平线上有3个<div>
。 each width = 33.33% and I want to change width of one div
on hover to 50% and other two to 25%. 每个宽度= 33.33%,我想将悬停时将一个div
宽度更改为50%,将其他两个更改为25%。
<div id="A"></div>
<div id="B"></div>
<div id="C"></div>
I can apply this for A as 我可以这样申请A
#A:hover{width:50%}
#A:hover+#B
{width:25%}
#A:hover~#C
{width:25%}
and it work fine. 它工作正常。
when we hover B 当我们将鼠标悬停在
#B:hover{width:50%}
#B:hover+#A
{width:25%}
#B:hover~#C
{width:25%}
B expand to 50% and C shrink to 25%. B扩大到50%,C缩小到25%。 but #A remain 33.33%. 但#A保持33.33%。
How can I fix this. 我怎样才能解决这个问题。
In Css
there is no previous selectors 在Css
中没有先前的选择器
33.33%
默认情况下,宽度为33.33%
width
is changed to 25%
在父级悬停时, width
更改为25%
div
inside the width is changed to 50%
在div
悬停时,宽度更改为50%
* { box-sizing: border-box; } .par { overflow: hidden; /* to clear floats */ } .par div { width: 33.33%; /* default width */ transition: .2s linear; border: 1px solid red; height: 100px; float: left; } .par:hover div { width: 25%; /* width when you hover on parent */ } .par div:hover { width: 50% /* width when you hover on the element */ }
<div class="par"> <div id="A">1</div> <div id="B">2</div> <div id="C">3</div> </div>
Flexbox can do that without a parent selctor: Flexbox可以在没有父选择器的情况下做到这一点:
* { box-sizing: border-box; } .parent { width: 80%; margin: 1em auto; display: flex; } .child { height: 150px; border: 1px solid grey; flex: 1; transition: flex 1s ease; } .child:hover { flex: 0 0 50%; }
<div class="parent"> <div class="child"></div> <div class="child"></div> <div class="child"></div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.