解读CSS布局之-水平垂直居中

地址:http://f2e.souche.com/blog/jie-du-cssbu-ju-zhi-shui-ping-chui-zhi-ju-zhong/
demo:http://codepen.io/tianzi77/pen/gpQBMN

新增加demo:http://codepen.io/tianzi77/pen/JdVyXV

利用display:table 以及transform2种方式实现水平垂直居中

随便写了几个小案列。

html:

    <h1>利用text-align和行高进行水平垂直居中</h1>
    <div class="outer">
        <div class="zhuangjia">
            <input type="text" placeholder="zhuangjia">
        </div>
    </div>

    <h1>利用50%+margin进行居中</h1>
    <div class="inner">
        <div class="zj">
            <input type="text" placeholder="zhuangjia">
        </div>
    </div>

    <h1>利用定位进行居中</h1>
    <div class="inner1">
        <div class="zj1">
            <input type="text" placeholder="zhuangjia">
        </div>
    </div>

样式:

        h1 {
            text-align: center
        }

        .outer {
            text-align: center;
            width: 100%;
            height: 400px;
            line-height: 400px;
            border: 1px solid red;
            font-size: 0;
        }

        .zhuangjia {
            line-height: normal;
            vertical-align: middle;
            display: inline-block;
        }

        .inner {
            width: 700px;
            height: 700px;
            border: solid 1px red;
            position: relative;
        }

        .zj {
            position: absolute;
            left: 50%;
            top: 50%;
            margin-left: -75.5px;
            margin-top: -10.5px;
            width: 151px;
            height: 21px;
        }

        .inner1 {
            width: 700px;
            height: 700px;
            border: 1px solid red;
            position: relative;
        }

        .zj1 {
            position: absolute;
            left: 0;
            right: 0;
            top: 0;
            bottom: 0;
            margin: auto;
            width: 151px;
            height: 21px;
        }

example:
http://codepen.io/hzxs1990225/full/RNRVZM

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值