常见布局

常见布局

左列定宽,右列自适应

效果

在这里插入图片描述

示例基础代码
<div class="content">
    <div class="left"></div>
    <div class="right"></div>
</div>
.content {
    height: 80px;
}
.left {
    width: 200px;
    height: 100%;
    background-color: teal;
}
.right {
    background-color: bisque;
    height: 100%;
}
方式一 float+margin-left
.content1 .left {
    float: left;
}
.content1 .right {
    margin-left: 200px;
}
方式二 right嵌套一个内容区
.content2 .left {
    float: left;
}
.content2 .right {
    float: right;
    width: 100%;
    margin-left: -200px;
}
.content2 .right .right_inbox {
    margin-left: 200px;
}

html right提前

<div class="content content2">
    <div class="right"></div>
    <div class="left"></div>
</div>
方式三 float+overflow (触发BFC)

BFC,即块格式化上下文-MDN

.content3 .left {
    float: left;
}
.content3 .right {
    overflow: auto;
}
方式四 calc计算宽度
.content4 .left {
    float: left;
}
.content4 .right {
    float: left;
    width: calc(100% - 200px);
}
方式五 表格单元格
.content5 {
    display: table;
    width: 100%;
}
.content5 .left, .content5 .right {
    display: table-cell;
}
方式六 绝对定位
.content6 {
    position: relative;
}
.content6 .left {
    position: absolute;
    top: 0;
    left: 0;
}
.content6 .right {
    position: absolute;
    top: 0;
    left: 200px;
    right: 0;
}
方式七 flex布局
.content7 {
    display: flex;
}
.content7 .right {
    flex: 1;
}
方式八 栅格布局
.content8 {
    display: grid;
    grid-template-columns: 200px 1fr;
}

一列不定宽,一列自适应

效果

在这里插入图片描述

示例基础代码
<div class="content">
    <div class="left"></div>
    <div class="right"></div>
</div>
.content {
    height: 80px;
}
.left {
    height: 100%;
    background-color: teal;
}
.right {
    background-color: bisque;
    height: 100%;
}
方式一 float+overflow (触发BFC)
.left {
    float: left;
}
.right {
    overflow: auto;
}
方式二 flex布局
.content {
    display: flex;
}
.content .right {
    flex: 1;
}
方式三 栅格布局
.content {
    display: grid;
    grid-template-columns: auto 1fr;
}

圣杯布局-两侧定宽,中间自适应

最终效果

在这里插入图片描述

实现代码

html

<div class="box">
    <div class="header">圣杯布局</div>
    <div class="parent">
        <div class="center">中间自适应</div>
        <div class="left">左列定宽</div>
        <div class="right">右列定宽</div>
    </div>
    <div class="footer"></div>
</div>
  1. 基础css
.box {
    width: 100%;
}
.header, .footer {
    height: 100px;
    background-color: grey;
}
.parent {
    height: 300px;
}
.center {
    width: 100%;
    height: 100%;
    background-color: gold;
    float: left;
}
.left, .right {
    width: 200px;
    height: 100%;
    background-color: teal;
    float: left;
}

此时效果

在这里插入图片描述

  1. 给left和right加上margin-left,使左右两列提上去
.left1 {
    margin-left: -100%;
}
.right1 {
    margin-left: -200px;
}

此时效果

在这里插入图片描述

  1. 此时中间内容是被遮挡的,给parent加上padding,空出位置给两边
.parent {
    padding: 0 200px;
}

此时效果

在这里插入图片描述

  1. 左右两列也被移到了中间,用定位将两边移开
.left {
    position: relative;
    left: -200px;
}
.right {
    position: relative;
    left: 200px;
}

此时效果

在这里插入图片描述

双飞翼布局

双飞翼布局实现效果和圣杯是一样的,只是实现方式略有不同,有点类似左列定宽右列自适应中的方式二,增加了一个内容区。

实现代码

html

<div class="box">
    <div class="header">双飞翼布局</div>
    <div class="parent">
        <div class="center">
            <div class="center_inbox">中间自适应</div>
        </div>
        <div class="left2">左列定宽</div>
        <div class="right2">右列定宽</div>
    </div>
    <div class="footer"></div>
</div>
  1. 基础css + 给left和right加上margin-left,使左右两列提上去
.box {
    width: 100%;
}
.header, .footer {
    height: 100px;
    background-color: grey;
}
.parent {
    height: 300px;
}
.center {
    width: 100%;
    height: 100%;
    background-color: gold;
    float: left;
}
.left, .right {
    width: 200px;
    height: 100%;
    background-color: teal;
    float: left;
}
.left {
    margin-left: -100%;
}
.right {
    margin-left: -200px;
}
  1. 给内容区增加左右外边距
.center_inbox {
    margin: 0 200px;
}
最终效果

在这里插入图片描述

参考

CSS 常见布局方式

干货!各种常见布局实现+知名网站实例分析

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值