场景:有一个容器,宽高必定,有内边距,当容器的内容(子元素)超出容器时出现滚动条。这一场景在 Chrome 和 Firefox(IE)表现不一致,border-box 布局。代码以下:css
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>title</title> <style type="text/css"> * { box-sizing: border-box; } .box { overflow: auto; border: 4px solid #f00; padding: 30px; width: 200px; height: 200px; background-color: #ffb475; } .content { width: 100%; height: 150px; background-color: #8b8bff; } </style> </head> <body> <div class="box"> <h3>这是一个标题</h3> <div class="content"></div> </div> </body> </html>
Chrome 74html
Firefox 67css3
IE 11布局
能够看到,Chrome 会将滚动容器的下内边距(padding-bottom)一块儿做为滚动内容,而 Firefox 和 IE 不会。
咱们指望获得的是 Chrome 的行为。为了让 Firefox,IE 与 Chrome 变现一致,有以下解决方案:spa
解决方案1
利用 css3 选择器,为滚动容器内最后一个元素添加 margin-bottom (前提:最后一个元素为块级元素,最后一个元素不能 display: none;)code
.box { padding-bottom: 0; } .box > :last-child { margin-bottom: 30px; }
解决方案2
为滚动容器添加一个伪类htm
.box { padding-bottom: 0; } .box::after { content: ""; display: block; height: 30px; width: 100%; }
解决方案3
在滚动容器内,添加一个html元素做为容器,写上 padding-bottom ,把内容所有写在这个元素内ip
.box { padding-bottom: 0; } .con-wrap { padding-bottom: 30px; } <div class="box"> <div class="con-wrap"> <h3>这是一个标题</h3> <div class="content"></div> </div> </div>
欢迎各位大腿提出更好的解决方案~it