css实现垂直水平居中、两栏和三栏布局

1.垂直水平居中

<style>
        /* 1. 基于定位 */
        /* .content {
            position: relative;
            background-color: #ccc; 
            width: 500px;
            height: 500px
        } */
        /* .box { 缺点:需要知道宽高以及精确计算
            position: absolute;
            background-color: #f00;   
            width: 50px;
            height: 50px;
            left: 50%;
            top: 50%;
            margin-left: -25px;
            margin-top: -25px;
        } */
        /* .box { 缺点:需要知道宽高
            position: absolute;
            background-color: #f00;   
            width: 50px;
            height: 50px;
            left: 0;
            top: 0;
            bottom: 0;
            right: 0;
            margin: auto;
        } */
        /* .box  缺点:兼容性
            position: absolute;
            padding: 20px;
            background-color: #f00;   
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
        } */
        /* 2.基于flex */
        /* .content {  
            display: flex;
            justify-content: center;
            align-items: center;
            width: 500px;
            height: 500px;
            background-color: #ccc;
        }
        .box {
            padding: 20px;
            background-color: #f00;  
        } */
        /* 3.基于js */
        /* 4.基于table-cell */
    </style>
    <body>
        <div class="content">
            <div class="box">css</div>
        </div>
    </body>

2.两栏、三栏布局

<style>
        /* 使用margin实现两边固定,中间自适应 */
        .content{
            padding: 0 0 0 200px;
        }
        .right, .left {
            width: 200px;
        }
        .center, .right, .left {
            float: left;
            box-sizing: border-box;
            min-height: 300px;
        }
        .center {
            width: 100%;
            background: #ccc;
        }
        .left {
            position: relative;
            border: 1px solid #f00;
            left: -100%;
            margin-left: -200px;
        }
        .right {
            border: 1px solid #ff0;
            margin-right: -200px;
        }
        /* 使用flex实现两边固定,中间自适应 */
        .content {
            display: flex;
            justify-content: space-between;
            height: 100%;
        }
        .left, .right {
            flex: 0 0 200px;
            height: 300px;
            border: 1px solid #f00;
        }
        .center {
            flex: 1;
            min-height: 300px;
            border: 1px solid #ff0;
        }
        /* 使用定位实现两边固定,中间自适应 */
        .content {
            position: relative;
            overflow: hidden;
        }
        .left,.right {
            position: absolute;
            top: 0;
            width: 200px;
            height: 300px;
            border: 1px solid #f00;
        }
        .left {
            left: 0;
        }
        .right {
            right: 0;
        }
        .center {
            margin: 0 200px;
            min-height: 300px;
            border: 1px solid #ff0;
        }
        /* 左边固定,右边自适应 */
        .content {
            padding: 0 0 0 200px;
        }
        .left, .right {
            float: left;
            box-sizing: border-box;
            min-height: 300px;
        }
        .right{
            width: 100%;
            background: #ccc;
        }
        .left {
            width: 200px;
            border: 1px solid #f00;
            position: relative;
            left: -100%;
            margin-left: -200px;
        }
        /* 右边固定,左边自适应 */
        .content{
            padding: 0 200px 0 0px;
        }
        .right {
            width: 200px;
        }
        .center, .right {
            float: left;
            box-sizing: border-box;
            min-height: 300px;
        }
        .center {
            width: 100%;
            background: #ccc;
        }
        .right {
            border: 1px solid #ff0;
            margin-right: -200px;
        }
        
    </style>
    <body>
        <!-- 使用margin、定位实现两边固定,中间自适应 -->
        <div class="content">
            <div class="center"></div>
            <div class="left"></div>
            <div class="right"></div>
        </div>
        <!-- 使用flex实现两边固定,中间自适应 -->
        <div class="content">
            <div class="left"></div>
            <div class="center"></div>
            <div class="right"></div>
        </div>
        <!-- 左边固定,右边自适应 -->
        <div class="content">
            <div class="right"></div>
            <div class="left"></div>
        </div>
        <!-- 右边固定,左边自适应 -->
        <div class="content">
            <div class="center"></div>
            <div class="right"></div>
        </div>
    </body>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值