水平垂直居中

你的名字 2022-10-27 12:28 356阅读 0赞

(1)水平居中:

  • 元素为行内元素,设置父元素text-align:center
  • 如果元素宽度固定,可以设置左右margin为auto
  • 如果元素为绝对定位(或相对定位),可以设置父元素position为relative,宽度固定时设置left:0;right:0;margin:auto
  • 如果元素为绝对定位(或相对定位),可以设置父元素position为relative,宽度固定时设置left:50%,margin-left值为宽度一半的负值
  • 使用绝对定位配合CSS3的transform属性,可以设置父元素position为relative,设置left:50%,transform: translate(-50%,0);(宽度不固定也能实现水平居中)
  • 使用flex布局,设置父元素display:flex;同时设置justify-content: center;
  • 使用老版flex布局,设置父元素display: -webkit-box;同时设置-webkit-box-pack: center;

(2)垂直居中:

  • 文本垂直居中,设置父元素line-height为height值
  • 如果元素为绝对定位(或相对定位),可以设置父元素position为relative,高度固定时设置top:0;bottom:0;margin:auto
  • 如果元素为绝对定位(或相对定位),可以设置父元素position为relative,高度固定时设置top:50%,margin-top值为高度一半的负值
  • 使用绝对定位配合CSS3的transform属性,可以设置父元素position为relative,设置top:50%,transform: translate(0,-50%);(高度不固定也能实现水平居中)
  • 使用flex布局,设置父元素display:flex;同时设置align-items: center;
  • 使用老版flex布局,设置父元素display: -webkit-box;同时设置-webkit-box-align: center;
  • 将显示方式设置为表格,设置父元素display: table-cell;同时设置vertical-align: middle;

水平垂直居中想必大家已经司空见惯、见怪不怪了,那假如是父子元素在不知道宽高的情况下实现水平垂直居中呢?

一、子元素是块级元素

方法1、flex布局

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title></title>
  6. <style type="text/css">
  7. *{
  8. margin: 0;
  9. padding: 0;
  10. }
  11. .father {
  12. width: 500px; /*此处未知,方面展示所以假设了宽高*/
  13. height: 300px; /*此处未知,方面展示所以假设了宽高*/
  14. background-color: yellow;
  15. display: flex;
  16. justify-content: center; /*垂直居中*/
  17. align-items: center; /*水平居中*/
  18. }
  19. .child {
  20. width: 200px; /*此处未知,方面展示所以假设了宽高*/
  21. height: 100px; /*此处未知,方面展示所以假设了宽高*/
  22. background-color: blue;
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <div class="father">
  28. <div class="child"></div>
  29. </div>
  30. </body>
  31. </html>

方法2、absolute+CSS3的translate

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title></title>
  6. <style type="text/css">
  7. *{
  8. margin: 0;
  9. padding: 0;
  10. }
  11. .father {
  12. width: 500px;
  13. height: 300px;
  14. background-color: yellow;
  15. position: relative;
  16. }
  17. .child {
  18. position: absolute;
  19. top: 50%;
  20. left: 50%;
  21. transform: translate(-50%, -50%);
  22. width: 200px;
  23. height: 100px;
  24. background-color: blue;
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <div class="father">
  30. <div class="child"></div>
  31. </div>
  32. </body>
  33. </html>

二、子元素是行内元素

水平居中:

  • 首先看它的父元素是不是块级元素,如果是,则直接给父元素设置 text-align: center;
  • 如果不是,则先将其父元素设置为块级元素,再给父元素设置 text-align: center;

垂直居中:

  • 只需要设置行内元素的”行高line-height等于盒子的高”即可,仅适用于纯文字;
  • 父元素设置成table-cell表格形式,使用vertical-align: middle即可使内联元素垂直居中;

发表评论

表情:
评论列表 (有 0 条评论,356人围观)

还没有评论,来说两句吧...

相关阅读

    相关 水平垂直居中

    实现水平垂直居中的方法有很多,在这里我只介绍两种易用且常见的方法,这两种方法对于固定宽高和不定宽高都有效。 1、transform 方案 // html部分

    相关 水平/垂直居中

    一. 水平居中 1.1行内或类行内元素(比如文本和链接) 在块级父容器中让行内元素居中,只需使用 text-align: center;: 这种方法可以

    相关 水平垂直居中

    (1)水平居中: 元素为行内元素,设置父元素text-align:center 如果元素宽度固定,可以设置左右margin为auto 如果元素为绝对定位(或