行内元素以及块级元素的 水平垂直居中方法

2022-11-23 0 241



行内元素

line-height:内容高度;
tex-align:center;

块级元素

1.定位+margin方法

定位上下左右值为0,然后margin:auto实现

	 .fa{//父元素设置定位
            position: relative;
            width: 500px;
            height: 500px;
            background-color:#eee
            }
         .son{//子元素设置属性上下左右值为0,margin:auto
            position:absolute;
            top:0;
            left:0;
            right:0;
            bottom:0;
            margin: auto;
            width:20px;
            height:50px;
            background-color:#999;
            }

2.定位+margin/tansform方法

子元素在定位下移动父盒子一半宽高,再通过margin操作向反方向移动半个子盒子的宽高

 	.fa{
            position: relative;
            width: 500px;
            height: 500px;
            background-color:#eee
            }
            .son{
            position:absolute;
            top:50%;
            left:50%;
            margin-top: -10px;
            margin-left: -25px;
            width:20px;
            height:50px;
            background-color:#999;
            }

通过transform:tanslateX()/translateY()反方向移动半个子盒子的宽高实现

	.fa{
            position: relative;
            width: 500px;
            height: 500px;
            background-color:#eee
            }
        .son{
            position:absolute;
            top:50%;
            left:50%;             
            width:20px;
            height:50px;
            background-color:#999;
            transform: translateX(-10px);
            transform:translateY(-25px);           
            }

3.flex方法
父盒子设置flex:flex;设置主轴居中justify-content:center;侧轴居中align-items:center;

   	.fa{
            display: flex;
            justify-content: center;
            align-items: center;
            width: 500px;
            height: 500px;
            background-color:#eee
            }
        .son{            
            width:20px;
            height:50px;
            background-color:#999;
            transform: translateX(-10px);
            transform:translateY(-25px);           
            }

 

1. 本站所有资源来源于用户上传和网络,因此不包含技术服务请大家谅解!如有侵权请邮件联系客服!cheeksyu@vip.qq.com
2. 本站不保证所提供下载的资源的准确性、安全性和完整性,资源仅供下载学习之用!如有链接无法下载、失效或广告,请联系客服处理!
3. 您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容资源!如用于商业或者非法用途,与本站无关,一切后果请用户自负!
4. 如果您也有好的资源或教程,您可以投稿发布,成功分享后有积分奖励和额外收入!
5.严禁将资源用于任何违法犯罪行为,不得违反国家法律,否则责任自负,一切法律责任与本站无关

源码下载

发表评论
暂无评论