2016-08-01 27 views
2

我無法將我的div邊緣與邊框貼到我的圖片邊緣。如何將我的div放到我的照片邊緣?

在我將邊框添加到第二個div之前,它完美地排列起來,但現在我添加了它,div邊緣與圖片之間存在間隙。

.headDiv { 
 
    position: relative; 
 
    width: 100%; 
 
} 
 
#heading { 
 
    font-family: fantasy; 
 
    font-size: 36; 
 
    position: absolute; 
 
    top: 25%; 
 
    left: 35%; 
 
} 
 
#navBarDiv { 
 
    height: 30px; 
 
    width: 100%; 
 
    background-color: limegreen; 
 
    margin-top: -4px; 
 
    border: 10px ridge gray; 
 
}
<div class="headDiv"> 
 
    <img id="imgBackgroud" src="http://vignette1.wikia.nocookie.net/unturned-bunker/images/4/4c/PEILEVEL.png/revision/latest?cb=82112" alt="background behing heading" height="200px" width="100%" /> 
 
    <h1 id="heading">Etowah Unturned Server</h1> 
 
</div> 
 
<div id="navBarDiv"> 
 
</div>

回答

2

添加box-sizing: border-box你鑲上股利。這將包括widthheight計算中的任何邊界和填充。

#navBarDiv { 
    height: 30px; 
    width: 100%; 
    background-color: limegreen; 
    margin-top: -4px; 
    border: 10px ridge gray; 
    box-sizing: border-box; /* NEW */ 
} 

現在默認box-sizing: content-box被覆蓋,你的邊界不擴大股利。

https://css-tricks.com/box-sizing/