2017-06-06 220 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属性

相关问题