实例
<!-- 1. 实例演示如何消除子元素浮动造成父元素高度折叠的影响 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>Document</title> <style> h3 span{ font-size: 14px; } .parent{ background-color: #ccc; overflow: hidden; } .child{ height: 400px; width: 400px; background-color: black; float: left; } .container{ width: 1000px; min-height: 100px; margin: 0 auto; } .header{ width: 100%; min-height: 100px; background-color: #ccc; } /* 绝对定位实现三栏式布局 */ .main{ width: 100%; min-height: 500px; /* background-color: #f00; */ margin: 10px auto; position: relative; } .main .left{ width: 300px; height: 500px; background-color: blue; position: absolute; left: 0; top: 0; } .main .cen{ width: 400px; height: 500px; background-color: red; position: absolute; left: 300px; top: 0; } .main .right{ width: 300px; height: 500px; background-color: yellowgreen; position: absolute; left: 700px; top: 0; } /* 浮动实现三栏式布局 */ .two_main{ width: 100%; min-height: 500px; margin: 10px auto; } .two_main .left, .two_main .right{ width: 300px; height: 500px; } .two_main .left{ background-color: #ccc; float: left; } .two_main .right{ background-color: #000; float: right; } .two_main .cen{ width: 600px; height: 500px; margin-left: 300px; background-color: yellowgreen; } .footer{ width: 100%; min-height: 100px; background-color: #0ff; } </style> </head> <body> <h3>第一题 <span>实例演示如何消除子元素浮动造成父元素高度折叠的影响。</span></h3> <p>推荐在父级添加 overflow:hidden 属性实现清除浮动</p> <div class="parent"> <div class="child"></div> </div> <h3>第二题 <span>实例演示三列布局的实现原理( 绝对定位实现, 浮动定位实现)。</span></h3> <p style="text-align: center;">绝对定位实现三栏式布局</p> <div class="container"> <div class="header"></div> <div class="main"> <div class="left"></div> <div class="cen"></div> <div class="right"></div> </div> <div class="footer"></div> </div> <p style="text-align: center;">浮动实现三栏式布局</p> <div class="container"> <div class="header"></div> <div class="two_main"> <div class="left"></div> <div class="right"></div> <div class="cen">主题内容</div> </div> <div class="footer"></div> </div> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例