transform:translate(-50%,-50%)水平垂直居中效果

transform: translateY(-50%) 实现元素垂直居中效果
transform:translate(-50%,-50%)实现水平垂直居中
通过position absolute和top 50%,结合transform产生效果。

eg.头部CSS排版
在这里插入图片描述

<style  lang="stylus"  rel="stylesheet/stylus">
.top_container
    background-color #02a774
    position fixed
    z-index 100
    width 100%
    height 45px
    left 0
    top 0
    border-radius 3px
    .top_left
        position absolute
        left 15px
        top 50%
        transform translateY(-50%)
    .top_center
        position absolute
        top 50%
        width 50%
        left 50%
        transform translate(-50%, -50%)
        text-align center
    .top_right
        position absolute
        top 50%
        right 15px
        font-size 14px
        transform translateY(-50%)
        text-align center
</style>

translate()函数是css3的新特性.在不知道自身宽高的情况下,可以利用它来进行水平垂直居中.。

transform: translateY(-50%)垂直居中:
让div 沿Y轴平移自身高度的一半。

同理,transform: translateX(-50%)为水平居中。

transform:translate(-50%,-50%)水平垂直居中:
当使用:top: 50%;left: 50%;, 是以左上角为原点,往上(x轴),左(y轴)移动自身长宽的 50%,故不处于中心位置。

} 一个最长的用法就是:让弹出的元素始终位于页面的中间位置。首先position: fixed; top: 50%; left: 50%;让这个图片的左上角处在屏幕的正中间。然后让他上下位移transform:translateX(-50%) translateY(-50%);就是相对于这个图片的宽高的50%,也就是一半。移回来。 .center-vertical {    position: fixed;    top: 50%;    left:50%;      -webkit-transform: translate(-50%-50%);    -ms-transform: translate(-50%,-50%);    -moz-transform: translate(-50%,-50%);    -o-transform: translate(-50%,-50%);    transform: translate(-50%-50%);}肖肖肖丽珠关注120分享专栏目录CSS实现垂直居中的4种思路详解09-24CSS实现垂直居中是网页布局中的常见需求,本文将详细讲解四种不同的方法,帮助开发者更好地理解和运用这些技巧。 1. **行高line-height实现单行文本垂直居中** 单行文本的垂直居中可以通过设置`line-height`来实现...CSS进阶之形变与动画 ()transform垂直居中总结、transition动画、animation动画、vertical-alignGood Good Study,Day Day Up! 1万+水平居中的方案行内级元素:设置父元素的 text-align:center块级元素:设置当前块级元素(要有宽度) margin:0 auto绝对定位:元素有宽度的情况下,设置left0、right0、margin:0 auto;flex布局:通过设置justify-content: center;垂直居中的方案绝对定位: 元素要有高度的情况下,设置top0、bottom0、margin:auto 0;flex布局 通过设置align-CSS 盒子垂直居中的方法_css transform居中3-15margin-top: 50%; // 向下移动父盒子的一半 transform: translateY(-50%); // 向上移动自身盒子的一半 } /* 通过 定位来移动*/ .father { width: 500px; height: 500px; background-color: skyblue; border: 1px solid #000; margin: 0 auto; position: relative; } .son { width: 200px; height...CSS进阶之形变与动画 ():transform垂直居中总结、transition动画、a...3-13translate水平垂直居中 水平居中:使用left/translate,其中需要知道left是基于包含块的宽度,translate是基于自身的大小。 垂直居中: 使用top/translate,其中需要知道top是基于包含块的高度,translate是基于自身的大小。 .shuipin{ position: relative; left: 50%; transform: translateX(-50%); } .chuizhi{ position...transform 垂直居中weixin_33948416的博客 9212019独角兽企业重金招聘Python工程师标准>>> ...css水平垂直居中方案最新发布m0_73351190的博客 187本篇文章介绍比较常见的三种方法。...的元素水平垂直居中的总结_绝对定位位水平垂直居中 transform...3-15DOCTYPEhtml>元素的垂直居中.elem1{position:relative;width:500px;height:500px;border:1px solid red; } .elem2{position:absolute;width:200px;height:200px;border:1px solid#9521de;left:50%;top:50%;transform:translate(-50%,-50%); } 效果如下: 不过transformcss3里面的内容,所以存在兼容性问题;IE9...实现div元素在整个屏幕的的垂直居中translateY(-50%)的利用_tran...3-14本文介绍了如何使用CSStransform: translateY(-50%)实现div元素在整个屏幕的垂直居中。通过设置position: absolute,top: 50%,right: 20px,再结合transform: translateY(-50%),即使元素高度不确定,也
03-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值