在悬停时,我使用top
值-0.3em
进行相对定位。我希望div
向上移动,第二div
向上移动。但我发现divs
之间存在白色差距。为什么会这样,我该如何解决?
.one {
width: 3em;
line-height: 3em;
background: yellow;
}
.two {
width: 3em;
line-height: 3em;
background: red;
}
.one:hover {
line-height: 3.3em;
position: relative;
top: -0.3em;
}

<div class="one">one</div>
<div class="two">two</div>
&#13;
答案 0 :(得分:1)
试试这个
<div class="zero">
<div class="one">one</div>
<div class="two">two</div>
</div>
CSS
.one {
width: 3em;
line-height: 3em;
background: yellow;
}
.two {
width: 3em;
line-height: 3em;
background: red;
}
.zero{
width: 3em;
line-height: 3em;
}
.zero:hover {
line-height: 3.3em;
position: relative;
top: -0.3em;
}
答案 1 :(得分:0)
使用.one
上的负边距代替:
.one {
width: 3em;
line-height: 3em;
background: yellow;
}
.two {
width: 3em;
line-height: 3em;
background: red;
}
.one:hover {
margin-top: -0.3em;
}
<div class="one">one</div>
<div class="two">two</div>
由于相对定位不会改变使用top: -0.3em
的文档流中元素的位置,因此不会导致.two
向上移动0.3 em。