2017-06-06 177 views
1

我已經瞭解到,當我需要時使用屬性overflow:auto;時,它會在框的右側添加一個滾動條(如果有必要,也可以在底部添加滾動條)。你可以看到一個例子,在這裏: https://www.w3schools.com/css/tryit.asp?filename=trycss_overflow_auto爲什麼Overflow屬性不像我預期的那樣工作?

但是,在這種情況下,物業是剛開塊要高:

<!DOCTYPE html> 
 
    <html> 
 
    <head> 
 
    <style> 
 
    div { 
 
     border: 3px solid #73AD21; 
 
    } 
 
    
 
    .clearfix { 
 
     overflow: auto; 
 
    } 
 
    
 
    .img2 { 
 
     float: right; 
 
    } 
 
    </style> 
 
    </head> 
 
    <body> 
 
    
 
    
 
    <p style="clear:right">Add a clearfix class with overflow: auto; to the containing element, to fix this problem:</p> 
 
    
 
    <div class="clearfix"><img class="img2" src="w3css.gif" alt="W3Schools.com" width="100" height="140"> 
 
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet, nulla et dictum interdum...</div> 
 
    
 
    </body> 
 
    </html>

爲什麼沒有在第二個示例中添加一個滾動條,就像在第一個示例中那樣?

+1

你沒有指定一個高度,所以元素被允許作爲內容要求它成長爲高 - 和爲此有沒有任何東西會溢出元素的尺寸,所以自然也不需要滾動條。 – CBroe

+0

,因爲它是一個浮動元素,所以對象的高度不會影響容器。 –

回答

2

您必須指定一個寬度高度使用overflow屬性

相關問題