一:消除子元素浮动造成父元素高度折叠的影响
实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> .box1 { width: 300px; border: 5px dashed red; } .box2 { width: inherit; height: 300px; background-color: blue; } .box2 { float: left; } .box1 { overflow: hidden; } </style> </head> <body> <div class="box1"> <div class="box2">子元素块</div> </div> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例
二、绝对定位实现, 浮动定位实现三列布局
1.绝对定位实现
实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <style> .main { height: 800px; position: relative; } .left { height: 800px; width: 200px; background-color: yellow; position: absolute; left: 0; top: 0; } .content { height: 800px; width: 800px; background-color: lightcoral; position: absolute; left: 200px; top: 0; } .right { height: 800px; width: 200px; background-color: lightgreen; position: absolute; left: 1000px; top: 0; } </style> <title>绝对定位演示</title> </head> <body> <div class="main"> <div class="left"></div> <div class="content"></div> <div class="right"></div> </div> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例
2.浮动定位实现
实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>浮动定位实现</title> </head> <style> .content { height: 700px; width: 600px; background-color: lightpink; float: left; } .left { height: 700px; width: 300px; background-color: blue; float: left; } .right { height: 700px; width: 300px; background-color: green; float: left; } </style> <body> <div class="left"></div> <div class="content"></div> <div class="right"></div> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例
总结
消除子元素浮动造成父元素高度折叠的影响有好几种:
将父级元素的浮动与子元素的浮动同样
将父元素的高度与浮动的子元素同样
添加一个清除浮动div clear属性标签消除
父级元素css代码中添加个overflow: hidden;消除
以上方法都可以用,但第4种各种浏览器兼容,适用更规范
绝对定位与浮动定位都可以实现三列布局,视写代码的时候想用哪个就哪个看需求用