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一边固定,另一边自适应的方法
第一种: 第二种:
随机推荐
- PHP中的Trait
Trait 自 PHP 5.4.0 起,PHP 实现了一种代码复用的方法,称为 trait. Trait 是为类似 PHP 的单继承语言而准备的一种代码复用机制.Trait 为了减少单继承语言的限制, ...
- Commons CLI - Usage
Usage Scenarios The following sections describe some example scenarios on how to use CLI in applicat ...
- 一段C程序分析
#include <stdio.h> #include <stdlib.h> void main() { int i; ; char ch; printf("请输入一 ...
- ### OpenCV安装(Linux)
### OpenCV安装(Linux) @(gr_self)[ffmpeg | openCV] #@author: gr #@date: 2015-09-02 #@email: forgerui@gm ...
- C++转到C#历程零基础知识(持续增加)
1.命名空间.类和源文件的关系:几个源文件用同一个命名空间时候,那么这几个源文件中的各个类之间的调用时可行的.他不会根据你的源文件分离而分开,因为最终编译后生成的是dll,这里来看你的几个源文件是没有 ...
- 接口(interface)
接口(interface) 接口(interface)定义了一个可由类和结构实现的协定.接口可以包含方法.属性.事件和索引器.接口不提供它所定义的成员的实现-它仅指定实现该接口的类或结构必须提供的成员 ...
- java新手笔记32 jdk5新特性
1.for package com.yfs.javase; import java.awt.Color; import java.util.Calendar; import java.util.Has ...
- windows同时安装两个jdk
两个项目需求不同,需要不同的jdk(1.7,1.8),所以每次切换项目的时候就会重新卸载原有的,在安装新的,比较麻烦. 就查资料看可以同时安装多个jdk,但是每次只能配置一个环境变量.安装的时候还是遇 ...
- [转]PHP5.5安装PHPRedis扩展
phpredis是个人觉得最好的一个php-redis客户端,因为其提供的function与redis的命令基本一致,降低的了学习成本,同时功能也很全面. 一.linux安装方法 phpredis下载 ...
- LESS学习总结
之前在工作过程中,用到了Less,一直没有将学习心得整理归纳,今天终于空出时间整理了一下. Less学习常用参考文档: Less 中文网 http://lesscss.cn/ 快速入门 | Les ...