我想在展開懸停在另一個元素上後保留div的寬度。這是我的代碼:使用懸停展開後保留div寬度(CSS)
CSS:
div.container{
width: 400px;
height: 250px;
border: 1px solid;
clear: both;
}
.square{
width: 100px;
height: 100px;
background: black;
float: left;
position: relative;
}
.grow {
width: 0px;
height: 100px;
float: left;
background: black;
position: relative;
transition:width 0.5s;
-webkit-transition:width 0.5s;
background: green;
}
.square:hover + .grow {
width: 200px;
}
而且還有我的html:
<div class="container">
<div class="square"></div>
<div class="grow"></div>
</div>
我想唯一的CSS的解決方案。我知道這對JS很簡單,但我真的需要使用CSS。
非常感謝!
我只是將它設置爲'position:absolute',這樣就不會考慮容器的佈局。 – MMM 2014-09-22 11:55:02
當您沒有將鼠標懸停在'.square'上時,'.grow' div會恢復爲原始寬度。由於您試圖控制事件和行爲,因此您需要使用JavaScript/jQuery。 CSS是用於造型的。 – 2014-09-22 12:10:00