2017-05-23 29 views
0

我正在一個項目中,當用戶將鼠標懸停在一個框上時顯示消息,我創建了一個場景來模擬我遇到的問題。z-index和溢出沒有正確顯示div

藍色框應該顯示爲完整的250像素定義,不會被剪裁到200像素,包含div設置爲,有無論如何繞過這一點,我可以坐在包含div的藍色框作爲其需要自包含的UserControl的所有部分。

#first { 
 
    width: 200px; 
 
    height: 200px; 
 
    background-color: green; 
 
    overflow: hidden; 
 
} 
 

 
#second { 
 
    width: 50px; 
 
    height: 50px; 
 
    background-color: red; 
 
    position: relative; 
 
} 
 

 
#fourth 
 
{ 
 
    width: 250px; 
 
    height: 50px; 
 
    background-color: blue; 
 
    position: absolute; 
 
    z-index: 100; 
 
}
<div id="first"> 
 
    <div id="second"> 
 
     <div id="fourth"> 
 
     test 
 
     </div>  
 
    </div> 
 
</div>

+1

刪除'#first {溢出:隱藏}'因爲這是預期的行爲,也沒有工作除了圍繞更改CSS/HTML結構 – Justinas

回答

0

不能完全確定這就是你後,但你需要做的是改變溢出從隱藏到可見的first DIV什麼:

#first { 
 
    width: 200px; 
 
    height: 200px; 
 
    background-color: green; 
 
    overflow: visible; /* change this */ 
 
} 
 

 
#second { 
 
    width: 50px; 
 
    height: 50px; 
 
    background-color: red; 
 
    position: relative; 
 
} 
 

 
#fourth 
 
{ 
 
    width: 250px; 
 
    height: 50px; 
 
    background-color: blue; 
 
    position: absolute; 
 
    z-index: 100; 
 
}
<div id="first"> 
 
    <div id="second"> 
 
     <div id="fourth"> 
 
     test 
 
     </div>  
 
    </div> 
 
</div>

+0

這不是我完全打算的,因爲我希望能夠包含滾動條,但除了第一個移動第四個div之外,似乎沒有任何工作可做。所以我會看看我是否可以嘗試定位元素,使其找到包含div的邊,並在包含div中顯示 – ccStars

2

變化:

#first { 
    width: 200px; 
    height: 200px; 
    background-color: green; 
    overflow: hidden; 

}

#first { 
width: 200px; 
height: 200px; 
background-color: green; 

}

換句話說,刪除溢出:隱藏和那個藍色div將出現你想要的。

+1

【如何回答](HTTPS:/ /stackoverflow.com/help/how-to-answer) – Nimish

+1

這應該是一個評論,而不是一個答案。 –

0

有線說你從#first元素中刪除overflow: hidden;。否則,如果內部的所有元素都超出了界限,它們將被切斷。

#first { 
 
    width: 200px; 
 
    height: 200px; 
 
    background-color: green; 
 
} 
 

 
#second { 
 
    width: 50px; 
 
    height: 50px; 
 
    background-color: red; 
 
    position: relative; 
 
} 
 

 
#fourth 
 
{ 
 
    width: 250px; 
 
    height: 50px; 
 
    background-color: blue; 
 
    position: relative; 
 
    top: 0; 
 
    left: 0; 
 
    z-index: 100; 
 
}
<div id="first"> 
 
    <div id="second"> 
 
     <div id="fourth"> 
 
     test 
 
     </div>  
 
    </div> 
 
</div>