实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>1.绝对定位来实现三列布局</title> <style type="text/css"> .header, .footer { widows: 100%;/*宽度自适应*/ height: 60px; background-color: lightblue; } .content { width: 1000px; min-height: 100%; background-color: lightskyblue; margin:auto; text-align: center; line-height: 60px;/*垂直居中*/ } .main { /*main是定位父级,必须设置定位属性*/ position: relative; /*设置宽高*/ width: 1000px; min-height: 600px; margin:auto;/*水平居中*/ background-color: yellow; } /*绝对定位*/ .left { top: 0; left: 0; width: 200px; position: absolute; min-height: 100%; background-color: pink; } .right { position: absolute; top: 0; right: 0; width: 200px; min-height: 100%; background-color: lightgreen; } .center { /*margin-left: 200px; margin-right: 200px;*/ margin:0 200px; min-height: 650px; background-color: wheat; } </style> </head> <body> <!-- DOM结构 --> <!-- 头部 --> <div class="header"> <div class="content">网站头部</div> </div> <!-- 主体 --> <div class="main"> <div class="left">左侧</div> <div class="center">中间</div> <div class="right">右侧</div> </div> <!-- 底部 --> <div class="footer"> <div class="content">网站底部</div> </div> <pre> 基本思路: 1、县创建一个父级区块:relative 2、左右侧边栏采用绝对定位 3、中间采用margin进行外边距挤压来形成内容区 4、如果定位父级宽度,中间区域就是定宽,否则就是自适应 </pre> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例