ITPub博客

首页 > IT职业 > IT职场 > 前端 - 用css写垂直水平居中

前端 - 用css写垂直水平居中

IT职场 作者:youyou0 时间:2021-09-09 09:37:28 0 删除 编辑

在页面布局中,用css写元素相对父元素 垂直水平居中 非常常见,下面是总结出的三种方法,其中推荐指数 #1> #2 > #3

·父元素:

position: relative;

·子元素:

#1

position: absolute;
top: 50%;
left: 50%;
transform: translateX(-50%) translateY(-50%);

#2

position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
margin: auto;

#3

position: absolute;
top: 50%;
left: 50%;
margin-top:-(高度/2)px;
margin-left:-(宽度/2)px;


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4362/viewspace-2805321/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-12

  • 博文量
    0
  • 访问量
    946