内容高度不固定的元素——垂直居中

html,body{
    margin: 0;
    height: 100%;
}

.box{
    background: #ccc;
    position: fixed;
    width: 100%;
    height: 100%;
    text-align: center;
    top:0px;
    left:0px;
}

.content{
    display: inline-block;
    vertical-align: middle;
    width: 300px;
    height: 300px;
    background: skyblue;
}

.box::before{
    display: inline-block;
    height: 100%;
    content: '';
    vertical-align: middle;
    width:1px;
    background:blue;
}

对应的html

<div class="box">
    <div class="content"></div>
</div>

原理:

父元素box覆盖整个页面,box的伪类before是其子元素,该子元素高度为100%,将父元素撑开,box的子元素before和content都是inline-block,且vertical-align: middle;,两个子元素居中对齐;

效果:

方法二:直接用函数:

@mixin dialogCon1($w, $color, $radius) {
    width: $w;
    height: auto;
    background-color: $color;
    border-radius: $radius;
    position: fixed;
    top: 50%;
    left: 50%;
    /*-webkit-transform: translateZ(0) scale(1.0, 1.0);*/
    -webkit-transform: translate(-50%, -50%) scale(1.0, 1.0);
    -ms-transform: translate(-50%, -50%) scale(1.0, 1.0);
    transform: translate(-50%, -50%) scale(1.0, 1.0);
    -webkit-font-smoothing: subpixel-antialiased;
}

缺点是IE9不支持transform,所以弹窗推荐用方法 一

转载于:https://www.cnblogs.com/xiaozhumaopao/p/7098756.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值