博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
css多栏自适应布局
阅读量:7026 次
发布时间:2019-06-28

本文共 7850 字,大约阅读时间需要 26 分钟。

css多栏自适应布局还是需要总结一下的,都是基本功。

一般使用position属性布局,或者用float属性布局,也可以使用display属性。

看资料说position适合首页布局,因为首页内容往往可以完全控制。float适合模板布局,模板中填充的内容无法控制。

一、左侧尺寸固定右侧自适应

1、浮动实现

在一文已介绍过。

.left{
width: 150px; float: left; } /*流体布局*/.right {
margin-left: 150px;}

原理:左侧定宽浮动,右侧使用margin-left,且不要定宽,容器尺寸变化右侧可自适应

左侧内容固定---------左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左
右侧内容自适应----------右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右
View Code

2、绝对定位实现

.container{
width: 100%;position: relative;padding-left: 150px;}.left {
width: 150px;position: absolute;left: 0;}/*流体布局*/.right {
width: 100%;}

原理:重点是container设置padding-left给left腾出空间,left相对于containr绝对定位,right占满剩余空间。

2 columns layout of starof
左侧内容
固定 ---------左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左
右侧内容
自适应 ----------右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右
View Code

3、BFC实现

.left {
width: 150px;float: left;}.right {
display: table-cell;}

原理:左栏定宽浮动,右栏生成BFC,根据BFC特性,与浮动元素相邻的,创建了BFC的元素,都不能与浮动元素相互覆盖。

2 columns layout of starof
左侧内容
固定 ---------左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左
右侧内容
自适应 ----------右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右
View Code

效果同上。

4、table实现

.container {
width: 100%;display: table;}.left {
width: 150px;display: table-cell;}.right {
display: table-cell;}

原理:不说了。

2 columns layout of starof
左侧内容
固定 ---------左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左
右侧内容
自适应 ----------右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右
View Code

 

二、 右侧尺寸固定,左侧自适应的流体布局

1、不改变DOM位置的写法【用的比较多】

在一文已介绍过。

.wrap {
width: 100%; float: left; background-color: green;}.left {
margin-right: 150px;}.right {
width: 150px; float: left; margin-left: -150px; background-color: pink;}

原理:给left包裹一层wrap,wrap用来布局,left调整内容。

wrap和right都左浮动,这样right会超过视口,通过设置margin-left负值拉回。

此时right回到窗口,但会覆盖wrap内容。left就派上用场了,left设置margin-right将内容拉回。此时布局和内容都达到目的,完成!

左侧内容
自适应 ---------左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左
右侧内容
固定 ----------右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右
View Code

2、改变DOM位置的写法

在一文已介绍过。

.right{
float: right;width: 150px;}.left{
margin-right: 150px;}

原理:因为右边先渲染,右边右浮动,左边设margin-right即可。

2 columns layout of starof
右侧内容
固定 ----------右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右
左侧内容
自适应 ---------左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左
View Code

三、左右都自适应

在一文已介绍过。

.left {
float: left;}.right{
display: table-cell;}

原理:左栏左浮动,右栏生成BFC,根据BFC特性:与浮动元素相邻的、创建了BFC的元素,都不能与浮动元素相互覆盖。

2 columns layout of starof
右侧内容
自适应 ----------右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右
View Code

缺点:由于IE6并不支持display:table-cell,用css hack弥补,右侧设定overflow:auto;zoom:1或者overflow:hidden;zoom:1。

.right{
display:table-cell;_display:block;zoom:1;}

应用案例:红色框部分,两栏自适应,左右都不定宽,右侧栏数量不定。

四、三栏布局,左右定宽,中间内容自适应【update20170422】

1、左右float+中间margin实现

.left {
width: 150px;float: left;}.right {
width: 100px;float: right;}.content {
margin-right: 100px;margin-left: 150px;}.footer {
clear: both;}

原理:用float实现。

左边定宽左浮动,右边定宽右浮动,中间margin调整左右间距,底部清除浮动。

Note:left和right的html代码写在content之前,先渲染。

3 columns layout of starof
标题
left
固定 -----------左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左
right
固定 ----------右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右
内容区域 content
自适应 --------------自适应自适应自适应自适应自适应自适应自适应自适应自适应自适应自适应自适应自适应自适应自适应自适应自适应自适应自适应自适应自适应自适应自适应自适应自适应自适应
View Code

缺点:

DOM顺序和视觉顺序不同,关键的主体内容在文档后面,主次不合理。如果右栏包含大量脚本资源,可能影响甚至阻塞整个页面的载入。不适合用做整站页面框架的搭建。

2、左右绝对定位+margin

原理:左右绝对定位,中间margin:0 100px 0 150px;

3 columns layout of starof
left
固定 -----------左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左
内容区域 content
自适应 --------------自适应自适应自适应自适应自适应自适应自适应自适应自适应自适应自适应自适应自适应自适应自适应自适应自适应自适应自适应自适应自适应自适应自适应自适应自适应自适应
right
固定 ----------右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右
View Code

3、左中右全部浮动+左右margin-left负值

 重点是content 右2层标签,外层content布局,内层body内容展示。content,right,content都左浮动。content100%宽度,left设置margin-left:-100%,由于前面的content宽度100%于浏览器,所以这里的-100%margin值正好使左栏div定位到了页面的左侧,right设置margin-left:-100px;content里面加一层body为内容主体,设置margin:0 100px 0 150px;

3 columns layout of starof
内容区域 content
自适应 --------------自适应自适应自适应自适应自适应自适应自适应自适应自适应自适应自适应自适应自适应自适应自适应自适应自适应自适应自适应自适应自适应自适应自适应自适应自适应自适应
left
固定 -----------左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左
right
固定 ----------右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右
View Code

content结构在left和right前面。

4、float+负margin实现

原理:分而治之,多封装一层,两两处理。

原理简单,处理起来稍复杂,我分步说明。

步骤一:先处理好right布局,wrap和right都左浮动,即应用上面【右侧尺寸固定,左侧自适应的流体布局】的第一种方法。

留空
右侧内容
固定 ----------右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右
View Code

目前的效果是这样:

将左边leftwrap留空部分补上下面结构

主体部分
左侧栏

步骤二:再处理left和content布局,contentwrap右浮动,left左浮动,完成。

3 columns layout of starof
content
自适应
左侧内容
固定 ---------左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左左
右侧内容
固定 ----------右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右右
View Code

缺点:嵌套多层标签,html文档不够简洁。

总结:如果不是必要,浮动布局不要轻易定宽高,尽量自适应。

 

资源链接:

基于CSS3的自适应布局技术

https://github.com/RubyLouvre/myless/issues/2

 

本文作者,因知识本身在变化,作者也在不断学习成长,文章内容也不定时更新,为避免误导读者,方便追根溯源,请诸位转载注明出处:有问题欢迎与我讨论,共同进步。

你可能感兴趣的文章
ASP.NET MVC 3和Razor中的@helper 语法
查看>>
CentOS6、7 LVM逻辑卷分区自动扩容Shell脚本编程思路与实例
查看>>
jQuery1.7.2正式发布了
查看>>
递归遍历目录中的所有文件
查看>>
详解Spring MVC 4之DispatcherServlet
查看>>
改写的日历小程序(Java)
查看>>
Java多线程初学者指南(7):向线程传递数据的三种方法
查看>>
将一列的转换成一行
查看>>
Virtual Machine Manager 2008 2008 R2系列之安装部署
查看>>
软件工厂(Software factory)介绍
查看>>
zabbix常用key和自定义key的讲解
查看>>
让你彻底理解STP的各种角色选举
查看>>
ADO.NET 对象模型
查看>>
linux常用命令使用技巧
查看>>
企业架构 - 开篇:TOGAF介绍
查看>>
Windows数据类型探幽——千回百转你是谁?(4)
查看>>
WCF服务编程设计规范(1):最新版WCF Coding Standard 介绍和下载
查看>>
Apache Segmentaion Fault故障处理案例分析
查看>>
C# 温故知新 基础篇(5) 类<思维导图>
查看>>
ZeroMQ接口函数之 :zmq_send_const – 从一个socket上发送一个固定内存数据
查看>>