CSS两列及三列自适应布局方法整理
在传统方法的基础上加入了Flex布局并阐述各方法的优缺点,希望对大家有所帮助。先上目录:
两列布局:左侧定宽,右侧自适应
方法一:利用float和负外边距
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
.main,.sitebar{
font: bolder 20px/300px;
}
.main{
width: 100%;
float: left;
}
.main .content{
margin-left: 200px;
background-color: red;
}
.sitebar{
width: 200px;
float: left;
background-color: green;
margin-left: -100%;
}
</style>
</head>
<body>
<div class="main">
<div class="content">右侧主体自适应区块</div>
</div>
<div class="sitebar">左侧定宽200px区块</div>
</body>
</html>
优点:考虑了页面优化,右侧主内容区先加载,左侧后加载。
缺点:多添加了一层div包裹。
方法二:利用外边距
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
.sitebar{
float: left;
width: 200px;
background-color: green;
}
.content{
background-color: red;
margin-left: 200px;
}
</style>
</head>
<body>
<div class="sitebar">左侧定宽200px区块</div>
<div class="content">右侧主体自适应区块</div>
</body>
</html>
优点:代码简洁,便于理解
缺点:不利于页面优化,右侧主内容区后加载
方法三:利用position
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
.sitebar{
width: 200px;
background-color: green;
}
.content{
position: absolute;
left: 200px;
right: 0;
top: 0;
background-color: red;
}
</style>
</head>
<body>
<div class="content">右侧主体自适应区块</div>
<div class="sitebar">左侧定宽200px区块</div>
</body>
</html>
优点:考虑到了页面优化,右侧内容区先加载
缺点:目测没有
上述三种方法兼容IE7以上,但在IE7下不设置高度时,会产生高度错位bug。可通过设置父元素font-size=0,再分别设置子元素font-size解决。
方法四:利用flex布局
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
.main{
display: flex;
}
.content{
flex:1;
background-color: red;
}
.sitebar{
flex:0 0 200px;
order:-1;
background-color: green;
}
</span><span class="hljs-tag" style="color: #cc6666;"></<span class="hljs-title" style="color: #8abeb7;">style</span>></span>
</head>
<body>
<div class="main">
<div class="content">右侧主体自适应区块</div>
<div class="sitebar">左侧定宽200px区块</div>
</div>
</body>
</html>
优点:CSS3新布局方式,高大上
缺点:仅支持IE11+。
三列布局:左右定款,中间自适应。
方法一:使用负外边距
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
.main,.left,.right{
height: 300px;
font: 20px/300px;
color: #fff;
text-align: center;
}
.main{
width: 100%;
float: left;
}
.main .content{
margin: 0 300px 0 200px;
background-color: black;
}
.left{
width: 200px;
float: left;
margin-left: -100%;
background-color: red;
}
.right{
width: 300px;
float: left;
margin-left: -300px;
background-color: blue;
}
</style>
</head>
<body>
<div class="main">
<div class="content">中间主体区域宽度自适应</div>
</div>
<div class="left">左侧定宽200px</div>
<div class="right">右侧定宽300px</div>
</body>
</html>
优点:兼容IE7+,考虑到页面优化,中间内容区先加载
缺点:多一层div嵌套,不易理解
方法二:使用绝对定位
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
body{
margin:0px;
}
#left {
background-color: #E8F5FE;
border: 1px solid #A9C9E2;
height: 400px;
width: 100px;
position: absolute;
top: 0px;
left: 0px;
}
#center {
background-color: #F2FDDB;
border: 1px solid #A5CF3D;
height: 400px;
margin-right: 102px;
margin-left: 102px;
}
#right {
background-color: #FFE7F4;
border: 1px solid #F9B3D5;
height: 400px;
width: 100px;
position: absolute;
top: 0px;
right: 0px;
}
</style>
</head>
<body>
<div id="center">中列</div>
<div id="left">左列</div>
<div id="right">右列</div>
</body>
</html>
优点:代码结构简单,考虑到了页面优化,中间内容去先加载
缺点:目测没有
方法三:使用flex布局
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.HolyGrail-body {
display: flex;
flex: 1;
}
.HolyGrail-content {
flex: 1;
background-color: green;
}
.HolyGrail-nav, .HolyGrail-ads {
/* 两个边栏的宽度设为12em */
flex: 0 0 200px;
background-color: blue;
}
.HolyGrail-nav {
/* 导航放到最左边 */
order: -1;
background-color: grey;
}
</style>
</head>
<body>
<div class="HolyGrail-body">
<main class="HolyGrail-content">...</main>
<nav class="HolyGrail-nav">...</nav>
<aside class="HolyGrail-ads">...</aside>
</div>
</body>
</html>
优点:高大上
缺点:仅支持IE11+
CSS两列及三列自适应布局方法整理的更多相关文章
- css实现等高布局 两栏自适应布局 三栏自适应布局
等高布局: HTML结构如下: <div class="wrapper"> <div class="box"> <h1>.. ...
- web标准(复习)--3 二列和三列布局
今天学习二列和三列布局,将涉及到以下内容和知识点 二列自适应宽度 二列固定宽度 二列固定宽度居中 xhtml的块级元素(div)和内联元素(span) float属性 三列自适应宽度 三列固定宽度 三 ...
- 总结CSS面试题目的考察点及常见布局问题整理
整理网上流传的若干份面试题目,突发奇想,总结关于CSS面试题目的考察点,发现问题大多围绕几个属性和几种题目,水平有限,仅供参考. 写这个博文内心有种莫名奇妙的自我谴责感,实在不应该把面试层叠样式“应试 ...
- 使用CSS实现三栏自适应布局(两边宽度固定,中间自适应)
来源:http://blog.csdn.net/cinderella_hou/article/details/52156333 所谓三列自适应布局指的是两边定宽,中间block宽度自适应.这道题在今年 ...
- Web标准:三、二列和三列布局
知识点: 1.二列自适应宽度 2.二列固定宽度 3.二列固定宽度居中 4.xhtml的块级元素(div)和内联元素(span) 5.float属性 6.三列自适应宽度 7.三列固定宽度 8.三列固定宽 ...
- css实现三栏自适应布局(两边固定,中间自适应)以及优缺点
方法一:绝对定位(absolute + margin) 原理:给左右两边的元素设置absolute,这样左右两边的元素脱离标准文档流的控制,中间的元素自然会上来,然后给中间的元素设置margin留出左 ...
- CSS 三栏自适应布局
CSS布局 这个很基础,方法也很多,要留意的知识点还是有一些. 比如IE6的触发layout *zoom:1 比如使用浮动后的清除浮动 clear:both 需求的延伸也会有一些: 比如三栏等高 ...
- 【HTML+CSS】右侧固定,左侧自适应布局
<style> *{ padding: 0; margin: 0; } #left{ float: right; width: 100%; height: 300px; } #box{ m ...
- css一边固定,另一边自适应的方法
第一种: 第二种:
随机推荐
- Https要点
http和https的区别 1.https协议需要到ca申请证书 2.http是超文本传输协议,信息是明文传输,https 则是具有安全性的ssl加密传输协议 3.http和https使用的是完全不同 ...
- css3 背景记
css3 背景 css背景主要包括五个属性: background-color background-color:transparent || <color> 用来设置元素的背景颜色,默认 ...
- 浅谈call和apply的联系&区别&应用匹配
call和apply的联系和区别在之前查过资料了解了一番,昨天晚上睡不着觉忽然想到了这个问题,发现对于他们的联系和区别理解的还是很模糊.看来还是欠缺整理,知识没有连贯起来.反思一二,详情如下: 1作用 ...
- MyBatis(3.2.3) - Handling the CLOB/BLOB types
MyBatis provides built-in support for mapping CLOB/BLOB type columns. Assume we have the following t ...
- 教您如何使用SQL中的SELECT LIKE like语句
LIKE语句在SQL有着不可替代的重要作用,下文就将为您介绍SQL语句中SELECT LIKE like的详细用法,希望对您能有所帮助. LIKE语句的语法格式是:select * from 表名 w ...
- Error This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. T
错误提示: Severity Code Description Project File Line Suppression StateError This project references NuG ...
- Jquery 获得服务器控件值的方法小结(转)
由于ASP.NET网页运行后,服务器控件会随机生成客户端id,jquery获取时候不太好操作,google了下,总结有以下3种方法. <!--服务器控件代码:--> <asp:Tex ...
- 选用 get 与 post 的一些建议
1.http的请求方法:get post 2. get:会把请求的内容 放到链接地址里面(数据请求的时候 默认的是get请求) 例:www.baidu.com/user/login?userna ...
- UINavigationController切换视图的简单使用
UINavigationController通过栈的方式来管理视图,通过push将视图压入栈,pop将视图推出栈. 下面通过简单的示例说明 AppDelegate.m - (BOOL)applicat ...
- 动态图片加到UIImageView中
//1.添加一个.gif类型的动态的图片,用到URLForResource方法,gif是图片的格式,FlagZombie是图片的名字 @implementation ViewController- ( ...