码迷,mamicode.com
首页 > Web开发 > 详细

css 4种常见实现元素居中的办法

时间:2018-12-16 23:19:01      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:弹性   flex   ali   auto   spl   bsp   调整   play   常见   

css 4种常见实现元素居中的办法:

 

1、通过 margin 属性调整 :
{
position: absolute;
top: 50%;
left: 50%;
margin-left: -盒子的一半;
margin-top: -盒子的一半;
}

 

2、通过 transform 属性调整:
{
position: absolute;
top: 50%;
left: 50%;
transform: translateX(-盒子的一半) translateY(-盒子的一半);
}

 

3、通过绝对定位:注意子绝父相(子元素绝对定位,父元素相对定位)
{
position: absolute;
top: 0;
left: 0;
right: 0;
bootom: 0;
margin: auto;
}

 

4、通过弹性盒子 :
/* 父盒子 */
{
display: flex;
justify-content: center;
align-items: center;
}

css 4种常见实现元素居中的办法

标签:弹性   flex   ali   auto   spl   bsp   调整   play   常见   

原文地址:https://www.cnblogs.com/yummylucky/p/10128094.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!