2015-04-01 51 views
0

悬停时,我使用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>

+0

代替提供jsfiddle的链接,最好使用堆栈溢出的“代码片段”功能。 – niyasc 2015-04-01 10:03:57

回答

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。

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; 
}