CSS布局

本文详细介绍了使用CSS实现两栏和三栏布局的多种方法,包括浮动、绝对定位、flex布局和网格布局。对于两栏布局,重点讲解了如何使一栏宽度固定,另一栏自适应。对于三栏布局,探讨了左右两栏固定,中间栏自适应的实现方式。同时,提到了在高度未知情况下的布局适应性分析。
摘要由CSDN通过智能技术生成

一、两列布局

一般两栏布局指的是左边一栏宽度固定,右边一栏宽度自适应

1、利用浮动。

        (1)左边设置固定宽度,设置向左浮动。右边设置margin-left: width ; 宽度设置为auto(不设置也行,默认是auto,撑满整个父元素)

<style>
        * {
            padding: 0;
            margin: 0;
        }
        
        .content {
            height: 200px;
        }
        
        .left {
            background-color: aqua;
            height: 100%;
            width: 200px;
            float: left;
        }
        
        .right {
            background-color: yellowgreen;
            height: 100%;
            margin-left: 200px;
            width: auto; /* 当这个没有设置时,也是可以的 */
        }
</style>

<body>
    <div class="content">
        <div class="left">左边一栏宽度固定</div>
        <div class="right">右边一栏宽度自适应</div>
    </div>
</body>

​​​

        (2)左边设置固定大小,并设置向左浮动右边设置overflow:hidden;触发BFC(BFC的区域不会与浮动元素发生重叠)

<style>
        * {
            padding: 0;
            margin: 0;
        }
        
        .content {
            height: 200px;
        }
        
        .left {
            background-color: aqua;
            height: 100%;
            width: 200px;
            float: left;
        }
        
        .right {
            background-color: yellowgreen;
            height: 100%;
            overflow: hidden;
        }
</style>

 <body>
     <div class="content">
         <div class="left">左边一栏宽度固定</div>
         <div class="right">右边一栏宽度自适应</div>
     </div>
  </body>

2、利用绝对定位 

        (1)父元素设置 position: relative;左边设置position:absolute;固定宽度右边设置margin-left:width;

<style>
        * {
            padding: 0;
            margin: 0;
        }
        
        .content {
            height: 200px;
            position: relative;
        }
        
        .left {
            background-color: aqua;
            height: 100%;
            width: 200px;
            position: absolute;
        }
        
        .right {
            background-color: yellowgreen;
            height: 100%;
            margin-left: 200px;
        }
</style>

<body>
    <div class="content">
        <div class="left">左边一栏宽度固定</div>
        <div class="right">右边一栏宽度自适应</div>
    </div>
</body>

        (2)父元素设置position: relative;左边设置固定宽度;右边设置position: absolute;left: width;其他方位定义为0

<style>
        * {
            padding: 0;
            margin: 0;
        }
        
        .content {
            height: 200px;
            position: relative;
        }
        
        .left {
            background-color: aqua;
            height: 100%;
            width: 200px;
        }
        
        .right {
            background-color: yellowgreen;
            height: 100%;
            position: absolute;
            left: 200px;
            top: 0;
            right: 0;
            bottom: 0;
        }
</style>
<body>
    <div class="content">
        <div class="left">左边一栏宽度固定</div>
        <div class="right">右边一栏宽度自适应</div>
    </div>
</body>

3、利用flex布局:父元素设置 display: flex; 左边设置固定宽度; 右边设置flex:1

<style>
        * {
            padding: 0;
            margin: 0;
        }
        
        .content {
            height: 200px;
            display: flex;
        }
        
        .left {
            background-color: aqua;
            height: 100%;
            width: 200px;
        }
        
        .right {
            background-color: yellowgreen;
            height: 100%;
            flex: 1;
        }
</style>

    <body>
        <div class="content">
            <div class="left">左边一栏宽度固定</div>
            <div class="right">右边一栏宽度自适应</div>
        </div>
</body>

 二、三栏布局

一般指页面中一共有三栏,左右两栏宽度固定,中间自适应的布局

1、利用浮动。左右固定宽度,并设置向左向右的浮动中间设置左右两个方向的margin值。(中间一栏必须放到最后)

<style>
        * {
            padding: 0;
            margin: 0;
        }
        
        .content {
            height: 200px;
        }
        
        .left {
            background-color: aqua;
            height: 100%;
            width: 200px;
            float: left;
        }
        
        .right {
            background-color: yellowgreen;
            height: 100%;
            width: 200px;
            float: right;
        }
        
        .center {
            background-color: blue;
            height: 100%;
            margin-left: 200px;
            margin-right: 200px;
        }
</style>

<body>
    <div class="content">
        <div class="left">宽度固定</div>
        <div class="right">宽度固定</div>
        <div class="center">中间自适应</div>
    </div>
</body>

2、利用绝对定位。左右两栏设置为绝对定位中间设置对应方向大小的margin的值

<style>
        * {
            padding: 0;
            margin: 0;
        }
        
        .content {
            height: 200px;
            position: relative;
        }
        
        .left {
            background-color: aqua;
            height: 100%;
            width: 200px;
            position: absolute;
        }
        
        .right {
            background-color: yellowgreen;
            height: 100%;
            width: 200px;
            position: absolute;
            right: 0;
            top: 0;
        }
        
        .center {
            background-color: blue;
            height: 100%;
            margin-left: 200px;
            margin-right: 200px;
        }
</style>

<body>
    <div class="content">
        <div class="left">宽度固定</div>
        <div class="center">中间自适应</div>
        <div class="right">宽度固定</div>
    </div>
</body>

3、利用flex布局。左右两栏设置固定大小中间一栏设置为flex:1

<style>
        * {
            padding: 0;
            margin: 0;
        }
        
        .content {
            height: 200px;
            display: flex;
        }
        
        .left {
            background-color: aqua;
            height: 100%;
            width: 200px;
        }
        
        .right {
            background-color: yellowgreen;
            height: 100%;
            width: 200px;
        }
        
        .center {
            background-color: blue;
            height: 100%;
            flex: 1;
        }
</style>

<body>
    <div class="content">
        <div class="left">宽度固定</div>
        <div class="center">中间自适应</div>
        <div class="right">宽度固定</div>
    </div>
</body>

4、网格布局。在父元素上设置就行。

<style>
        * {
            padding: 0;
            margin: 0;
        }
        
        .content {
            display: grid;
            width: 100%;
            grid-template-columns: 200px auto 200px;
            grid-template-rows: 200px;
        }
        
        .left {
            background-color: aqua;
        }
        
        .right {
            background-color: yellowgreen;
        }
        
        .center {
            background-color: blue;
        }
</style>

<body>
    <div class="content">
        <div class="left">宽度固定</div>
        <div class="center">中间自适应</div>
        <div class="right">宽度固定</div>
    </div>
</body>

5、表格布局。父元素设置display: table, width: 100%; 三列都设置display: table-cell;左右设置固定宽度 

    <style>
        * {
            padding: 0;
            margin: 0;
        }
        
        .content {
            display: table;
            width: 100%;
            height: 200px;
        }
        
        .content div {
            display: table-cell;
        }
        
        .left {
            background-color: aqua;
            width: 200px;
        }
        
        .right {
            background-color: yellowgreen;
            width: 200px;
        }
        
        .center {
            background-color: blue;
        }
</style>

<body>
    <div class="content">
        <div class="left">宽度固定</div>
        <div class="center">中间自适应</div>
        <div class="right">宽度固定</div>
    </div>
</body>

如果去掉高度已知,考虑纵向,哪种方案还适用,哪些不适用 

flex布局和表格布局是通用的,其它方案不适用

 三、“品”字布局

1、第一个div设置margin:0 auto;第二个设置向左浮动,margin-left:50%;第三个设置向左浮动,miang-left: 2*width;

2、将第二个和第三个向左浮动改为display: inline-block;其他不变

    <style>
        * {
            padding: 0;
            margin: 0;
        }
        
        .content div {
            width: 100px;
            height: 100px;
            text-align: center;
            line-height: 100px;
        }
        
        .d1 {
            background-color: aqua;
            margin: 0 auto;
        }
        
        .d2 {
            background-color: yellowgreen;
            float: left; /* 可替换成display: inline-block; */
            margin-left: 50%;
        }
        
        .d3 {
            background-color: blue;
            float: left; /* 可替换成display: inline-block; */
            margin-left: -200px;
        }
</style>

<body>
    <div class="content">
        <div class="d1">1</div>
        <div class="d2">2</div>
        <div class="d3">3</div>
    </div>
</body>

四、九宫格布局 

 待更新

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值