CSS-三栏布局

1、浮动布局:优点:兼容性好;缺点:元素脱离文档流,产生浮动。

<style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        .box>div {
            height: 100px;
        }

        .left {
            float: left;
            width: 100px;
            background: red;
        }

        .content {
            background: yellow;
        }

        .right {
            float: right;
            width: 100px;
            background: blue;
        }
    </style>
    /*这种布局方式是利用浮动脱离文档流实现的,这里需要特别注意一下,left、right都在content前面布局,如果按照正常的顺序left-content-right,中间元素形成定位,那么就会将right顶下去。无法实现三栏布局。 */
    <body>
	    <div class="box">
	        <div class="left">1</div>
	        <div class="right">3</div>
	        <div class="content">2</div>
	    </div>
	</body>

2、定位布局:优点:简单,适合快速开发页面;缺点:元素脱离文档流,导致后面的元素也会脱离文档流,可使用性比较差

<style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        .box {
            position: relative;
        }

        .box>div {
            height: 100px;
            position: absolute;
        }

        .left {
            left: 0px;
            width: 100px;
            background: red;
        }

        .content {
            left: 100px;
            right: 100px;
            background: yellow;
        }

        .right {
            right: 0px;
            width: 100px;
            background: blue;
        }
    </style>
    <div class="box">
        <div class="left">1</div>
        <div class="content">2</div>
        <div class="right">3</div>
    </div>

3、flex布局:优点:开发简单易上手;缺点:这在移动端开发非常常见,但PC端IE8不兼容。

<style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        .box {
            display: flex;
        }

        .box>div {
            height: 100px;
        }

        .left {
            width: 100px;
            background: red;
        }

        .content {
            flex: 1;
            background: yellow;
        }

        .right {
            width: 100px;
            background: blue;
        }
    </style>
    <div class="box">
        <div class="left">1</div>
        <div class="content">2</div>
        <div class="right">3</div>
    </div>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值