CSS常见布局

算法云影
• 阅读 2931

两列自适应布局

两列自适应布局是指一列由内容撑开,另一列撑满剩余宽度的布局方式

1.float+overflow:hidden

<style>
    .contain {
        overflow: hidden;
        zoom: 1;
    }
    .left {
        float: left;
        background: blue;
    }
    .right {
        overflow: hidden;
        zoom: 1;
        background: yellow;
    }
</style>
<div class="contain">
    <div class="left">左栏</div>
    <div class="right">右栏</div>
</div>

2.flex布局

<style>
    .contain {
        display: flex;
    }
    .right {
        flex: 1;
    }
</style>
<div class="contain">
    <div class="left">左栏</div>
    <div class="right">右栏</div>
</div>

3.grid布局

<style>
    .contain {
        display: grid;
        grid-template-columns: auto 1fr;
    }
</style>
<div class="contain">
    <div class="left">左栏</div>
    <div class="right">右栏</div>
</div>

三栏布局——两边固定宽度中间自适应

1.float布局

float布局使用时注意清除浮动。
<style>
    /*父元素清除浮动*/
    .float:after {
        content: "";
        height: 0;
        display: block;
        clear: both;
        visibility: hidden;
    }
    .float {
        *zoom: 1;
    }
    .float .left {
        float: left;
        width: 300px;
        height: 300px;
        background: red;
    }
    .float .right {
        float: right;
        width: 300px;
        height: 300px;
        background: blue;
    }
    .float .center {
        background: yellow;
        height: 400px;
        margin-left: 300px;
        margin-right: 300px;
    }
</style>
<section class="float">
    <div class="left">左边</div>
    <div class="right">右边</div>
    <div class="center">中间</div>
</section>

2.Position 布局

Position布局只是根据定位属性去直接设置元素位置。不推荐使用
<style>
    .position {
        position: relative;
    }
    .position .left {
        position: absolute;
        left: 0;
        width: 300px;
        height: 300px;
        background: red;
    }
    .position .center {
        position: absolute;
        left: 300px;
        right: 300px;
        height: 400px;
        background: yellow;
    }
    .position .right {
        position: absolute;
        right: 0;
        width: 300px;
        height: 300px;
        background: blue;
    }
</style>
<section class="position">
    <div class="left">左边</div>
    <div class="center">中间</div>
    <div class="right">右边</div>
</section>

3.Flex 布局

flex布局比较强大,只能支持到IE10及以上。
<style>
    .flex {
        display: flex;
    }
    .flex .left {
        width: 300px;
        background: red;
    }
    .flex .center {
        flex: 1;
        background: yellow;
    }
    .flex .right {
        width: 300px;
        background: blue;
    }
</style>
<section class="flex">
    <div class="left">左边</div>
    <div class="center">中间</div>
    <div class="right">右边</div>
</section>

4.table 布局

table布局使用起来方便,没有兼容性也不存在问题,但使用不方便
 <style>
    .table {
        width: 100%;
        display: table;
    }
    .table .left {
        display: table-cell;
        width: 300px;
        background: red;
    }
    .table .center {
        display: table-cell;
        background: yellow;
    }
    .table .right {
        display: table-cell;
        width: 300px;
        background: blue;
    }
</style>
<section class="table">
    <div class="left">左边</div>
    <div class="center">中间</div>
    <div class="right">右边</div>
</section>

5.grid布局

grid布局很强大,但是兼容性很差。
<style>
    .grid {
        display: grid;
        grid-template-rows: 100px;
        grid-template-columns: 300px auto 300px;
    }
    .grid .left {
        background: red;
    }
    .grid .center {
        background: yellow;
    }
    .grid .right {
        background: blue;
    }
</style>
<section class="grid">
    <div class="left">左边</div>
    <div class="right">右边</div>
    <div class="center">中间</div>
</section>

圣杯布局

三个部分都设定为左浮动,然后设置center的宽度为100%,此时,leftright部分会跳到下一行;
通过设置margin-left为负值让leftright部分回到与center部分同一行;

<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <style>
    .main {
      padding-left: 300px;
      padding-right: 300px;
    }
    .center {
      float: left;
      width: 100%;
      height: 400px;
      background: #4ba946;
    }
    .left {
      float: left;
      width: 300px;
      height: 300px;
      background: #0376c2;
      margin-left: -100%;
      position: relative;
      left: -300px;
    }
    .right {
      float: left;
      width: 300px;
      height: 300px;
      background: #9889c1;
      margin-left: -300px;
      position: relative;
      right: -300px;
    }
  </style>
</head>
<body>
<section class="main">
  <div class="center">中间</div>
  <div class="left">左边</div>
  <div class="right">右边</div>
</section>
</body>

缺点:
center部分的最小宽度不能小于left部分的宽度,否则会left部分掉到下一行
如果其中一列内容高度拉长(如下图),其他两列的背景并不会自动填充。(借助伪等高布局可解决)

双飞翼布局

实现步骤(前两步与圣杯布局一样)
三个部分都设定为左浮动,然后设置center的宽度为100%,此时,leftright部分会跳到下一行;
通过设置margin-left为负值让leftright部分回到与center部分同一行;
center部分增加一个内层div,并设margin: 0 200px;

缺点
多加一层 dom 树节点,增加渲染树生成的计算量。

多列等高布局——圣杯布局

实现步骤:
1.三部分都设定为浮动,设置center的宽度为100%。
2.设置 leftrightmargin-left为负值让leftright部分回到与center部分同一行。
3.设置相对定位,让leftright部分移动到两边。
4.伪等高布局

.container {
    overflow: hidden;//把溢出背景切掉
}
.center,.left,.right {
    padding-bottom: 10000px;
    margin-bottom: -10000px;
}

示例:

<style>
    .container {
        padding-left: 300px;
        padding-right: 300px;
        overflow: hidden;
    }
    /*关键*/
    .container .center, .left, .right {
        padding-bottom: 10000px;
        margin-bottom: -10000px;
    }
    .container .center {
        float: left;
        width: 100%;
        height: 400px;
        background: yellow;
    }
    .container .left {
        float: left;
        width: 300px;
        height: 300px;
        margin-left: -100%;
        background: red;
        position: relative;
        left: -300px;
    }
    .container .right {
        float: left;
        width: 300px;
        height: 300px;
        margin-left: -300px; /*right 的宽度*/
        background: blue;
        position: relative;
        right: -300px;
    }
</style>
<section class="container">
    <div class="center">中间</div>
    <div class="left">左边</div>
    <div class="right">右边</div>
</section>

粘连布局

描述:
有一块内容<main>,当<main>的高度足够长的时候,紧跟在<main>后面的元素<footer>会跟在<main>元素的后面。当<main>元素比较短的时候(比如小于屏幕的高度),我们期望这个<footer>元素能够“粘连”在屏幕的底部。
实现步骤:

<style>
    html, body {
        height: 100%;
    }
    .wrap {
        min-height: 100%;/*设置min-height,变为视口高度*/
        background: blue;
        overflow: hidden;
    }
    .main {
        padding-bottom: 50px;
    }
    .footer {
        height: 50px;
        margin-top: -50px;
        background: red;
    }
</style>
<div class="wrap">
    <article class="main">
        <p>1</p>
        <p>2</p>
        <p>3</p>
    </article>
</div>
<footer class="footer">footer</footer>
点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
美凌格栋栋酱 美凌格栋栋酱
6个月前
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(
Easter79 Easter79
3年前
typeScript数据类型
//布尔类型letisDone:booleanfalse;//数字类型所有数字都是浮点数numberletdecLiteral:number6;lethexLiteral:number0xf00d;letbinaryLiteral:number0b101
Wesley13 Wesley13
3年前
VBox 启动虚拟机失败
在Vbox(5.0.8版本)启动Ubuntu的虚拟机时,遇到错误信息:NtCreateFile(\\Device\\VBoxDrvStub)failed:0xc000000034STATUS\_OBJECT\_NAME\_NOT\_FOUND(0retries) (rc101)Makesurethekern
Wesley13 Wesley13
3年前
FLV文件格式
1.        FLV文件对齐方式FLV文件以大端对齐方式存放多字节整型。如存放数字无符号16位的数字300(0x012C),那么在FLV文件中存放的顺序是:|0x01|0x2C|。如果是无符号32位数字300(0x0000012C),那么在FLV文件中的存放顺序是:|0x00|0x00|0x00|0x01|0x2C。2.  
Wesley13 Wesley13
3年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
3年前
PHP创建多级树型结构
<!lang:php<?php$areaarray(array('id'1,'pid'0,'name''中国'),array('id'5,'pid'0,'name''美国'),array('id'2,'pid'1,'name''吉林'),array('id'4,'pid'2,'n
Wesley13 Wesley13
3年前
Java日期时间API系列36
  十二时辰,古代劳动人民把一昼夜划分成十二个时段,每一个时段叫一个时辰。二十四小时和十二时辰对照表:时辰时间24时制子时深夜11:00凌晨01:0023:0001:00丑时上午01:00上午03:0001:0003:00寅时上午03:00上午0
Wesley13 Wesley13
3年前
MBR笔记
<bochs:100000000000e\WGUI\Simclientsize(0,0)!stretchedsize(640,480)!<bochs:2b0x7c00<bochs:3c00000003740i\BIOS\$Revision:1.166$$Date:2006/08/1117
Python进阶者 Python进阶者
1年前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这