css三栏布局技巧,六种css三栏布局方法示例

本文我们主要和大家分享六种css三栏布局方法示例, 谈到布局,首先就要想到定位,当别人问我,css的position定位有哪些取值,分别表示什么意思?呃.....抓头.gif,是时候回归本质,看定义了。

定位

position有六个属性值:static、relative、absolute、fixed、sticky和inherit。static(默认):元素框正常生成。块级元素生成一个矩形框,作为文档流的一部分;行内元素则会创建一个或多个行框,置于父级元素中。

relative:元素框相对于之前正常文档流中的位置发生偏移,并且原先的位置仍然被占据。发生偏移的时候,可能会覆盖其他元素。

absolute:元素框不再占有文档位置,并且相对于包含块进行偏移(所谓包含块就是最近一级外层元素position不为static的元素)。

fixed:元素框不再占有文档流位置,并且相对于视窗进行定位。

sticky:css3新增属性值,粘性定位,相当于relative和fixed的混合。最初会被当作是relative,相对原来位置进行偏移;一旦超过一定的阈值,会被当成fixed定位,相对于视口定位。

三列布局

三列布局,其中一列宽度自适应,在PC端最常用之一,搞定了三列布局,其他一样的套路。

方式一:浮动布局

缺点:html结构不正确,当包含区域宽度小于左右框之和,右边框会被挤下来

.tree-columns-layout.float .left {

float: left;

width: 300px;

background-color: #a00;

}

.tree-columns-layout.float .right {

float: right;

width: 300px;

background-color: #0aa;

}

.tree-columns-layout.float .center {

/* left: 300px;

right: 300px; */

margin: 0 300px;

background-color: #aa0;

overflow: auto;

}

我是浮动布局左框

我是浮动布局右框

我是浮动布局中间框

方式二:定位布局

缺点:要求父级要有非static定位,如果没有,左右框容易偏移出去

.tree-columns-layout.position {

position: relative;

}

.tree-columns-layout.position .left {

position: absolute;

left: 0;

top: 0;

width: 300px;

background-color: #a00;

}

.tree-columns-layout.position .right {

position: absolute;

right: 0;

top: 0;

width: 300px;

background-color: #0aa;

}

.tree-columns-layout.position .center {

margin: 0 300px;

background-color: #aa0;

overflow: auto;

}

我是浮动定位左框

我是浮动定位中间框

我是浮动定位右框

方式三:表格布局

缺点:没什么缺点,恐惧table

.tree-columns-layout.table {

display: table;

width: 100%;

}

.tree-columns-layout.table > article {

display: table-cell;

}

.tree-columns-layout.table .left {

width: 300px;

background-color: #a00;

}

.tree-columns-layout.table .center {

background-color: #aa0;

}

.tree-columns-layout.table .right {

width: 300px;

background-color: #0aa;

}

我是表格布局左框

我是表格布局中间框

我是表格布局右框

方式四:flex弹性布局

缺点:兼容性

.tree-columns-layout.flex {

display: flex;

}

.tree-columns-layout.flex .left {

width: 300px;

flex-shrink: 0; /* 不缩小 */

background-color: #a00;

}

.tree-columns-layout.flex .center {

flex-grow: 1; /* 增大 */

background-color: #aa0;

}

.tree-columns-layout.flex .right {

flex-shrink: 0; /* 不缩小 */

width: 300px;

background-color: #0aa;

}

我是flex弹性布局左框

我是flex弹性布局中间框

我是flex弹性布局右框

方式五:grid栅格布局

缺点:兼容性 Firefox 52, Safari 10.1, Chrome 57, Opera 44

.tree-columns-layout.grid {

display: grid;

grid-template-columns: 300px 1fr 300px;

}

.tree-columns-layout.grid .left {

background-color: #a00;

}

.tree-columns-layout.grid .center {

background-color: #aa0;

}

.tree-columns-layout.grid .right {

background-color: #0aa;

}

我是grid栅格布局左框

我是grid栅格布局中间框

我是grid栅格布局右框

方式六:圣杯布局

缺点:需要多加一层标签,html顺序不对,占用了布局框的margin属性

.tree-columns-layout.cup:after {

clear: both;

content: "";

display: table;

}

.tree-columns-layout.cup .center {

width: 100%;

float: left;

}

.tree-columns-layout.cup .center > p {

margin: 0 300px;

overflow: auto;

background-color: #aa0;

}

.tree-columns-layout.cup .left {

width: 300px;

float: left;

margin-left: -100%;

background-color: #a00;

}

.tree-columns-layout.cup .right {

width: 300px;

float: left;

margin-left: -300px;

background-color: #0aa;

}

我是圣杯布局中间框

我是圣杯布局左框

我是圣杯布局右框

实现效果:

1c7a536897b13de4bf7e1f5bed095c3c.png

相关推荐:

CSS的经典三栏布局如何实现

高度已知,左右宽度固定,实现三栏布局的5种方法

三栏布局的用法汇总

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值