Step-By-Step icon indicating copy to clipboard operation
Step-By-Step copied to clipboard

使用CSS让一个元素水平垂直居中

Open YvetteLau opened this issue 5 years ago • 11 comments

YvetteLau avatar Jul 04 '19 03:07 YvetteLau

设置元素样式为: display:flex; justify-content: center; align-items: center;

mark-0609 avatar Jul 04 '19 06:07 mark-0609

行内元素居中 text-align: center + line-height; 弹性布局flex; 绝对定位 absolute + margin/transform/calc;

shenshuangdao avatar Jul 04 '19 10:07 shenshuangdao

1、块级元素,设置宽高,需要谁居中,给其设置 margin: 0 auto; 2、行内元素:首先看它的父元素是不是块级元素,如果是,则直接给父元素设置 text-align: center; 如果不是,则先将其父元素设置为块级元素,再给父元素设置 text-align: center; 3、使用绝对定位:首先设置父元素为相对定位,再设置子元素为绝对定位,设置子元素的left:50%,即让子元素的左上角水平居中; 4、使用flexbox布局,只需要给待处理的块状元素的父元素添加属性 display: flex; justify-content: center;

tpxiang avatar Jul 05 '19 01:07 tpxiang

display:flex; justify-content: center; align-items: center;

umeimmense avatar Jul 05 '19 02:07 umeimmense

  1. 绝对定位 + margin:auto
  2. 绝对定位 + 负margin (需已知宽高)
  3. 绝对定位 + transform
  4. flex布局

Tcdian avatar Jul 05 '19 03:07 Tcdian

  1. 父元素使用padding
  2. flex布局 display: flex; justify-content: center; align-items:center;
  3. 使用绝对定位和transform positon: absolute; left: 50%; top: 50%; transform: translate(-50%,-50%);
  4. 使用绝对定位和负的margin position: absolute; left: 50%; top: 50%; margin-left: -宽的一半; margin-top: - 高的一半;
  5. margin auto margin: 0 auto; margin-top: 高的一半;
  6. table-cell .parent{ width: 300px; height: 200px; border: 1px solid red; display: table-cell; vertical-align: middle; text-align: center; } .son{ width: 50px; height:50px; background:yellow; border: 1px solid blue; display: inline-block; }
  7. text-align: center和line-height

luohong123 avatar Jul 05 '19 09:07 luohong123

1.单行文本,只需要将文本行高(line-height)和所在区域高度(height)设为一致即可

p{ height:40px; line-height:40px }

2.父元素高度不确定,只能靠内部文本内容撑开。可以设置margin,padding值相等

假设父元素是div div{ margin: 30px auto; padding:20px ; }

3.父级高度固定的时,设置父级div的display属性:display: table;;然后再添加一个div包含文本内容,设置其display:table-cell;和vertical-align:middle

4、flex的垂直居中,这种做法可以不定义内部元素的高度

5.使用absolute+transform

lydfree avatar Jul 06 '19 08:07 lydfree

  1. 使用Flex
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>未知宽高元素水平垂直居中</title>
</head>
<style>
.parent4{
    display:flex;
    justify-content: center;
    align-items: center;
    width: 300px;
    height:300px;
    background: #FD0C70;
}
.parent4 .child{
    color:#fff;
}
</style>
<body>
   <div class="parent4">
        <div class="child">hello world-4</div>
    </div>
</body>
</html>
  1. 使用绝对定位
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>未知宽高元素水平垂直居中</title>
</head>
<style>
.parent3{
    position:absolute;
    height:300px;
    width: 300px;
    background: #FD0C70;
}
.parent3 .child{
    position: absolute;
    top: 50%;
    left: 50%;
    color:black;
    transform: translate(-50%, -50%);
}
</style>
<body>
<div class="parent3">
        <div class="child">hello world-3</div>
    </div>
</body>
</html>
  1. inline-block
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>未知宽高元素水平垂直居中</title>
</head>
<style>
.parent2{
    height:300px;
    width: 300px;
    text-align: center;
    background: #FD0C70;
}
.parent2 span{
    display: inline-block;
    height:50%
  
}
.parent2 .child{
    display: inline-block;
    color: #fff;
}

</style>
<body>
    <div class="parent2">
        <span></span>
        <div class="child">hello world-2</div>
    </div>
</body>
</html>
  1. 使用 table 和 table-cell
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>未知宽高元素水平垂直居中</title>
</head>
<style>

.parent1{
    display: table;
    height:300px;
    width: 300px;
    background-color: #FD0C70;
}
.parent1 .child{
    display: table-cell;
    vertical-align: middle;
    text-align: center;
    color: #fff;
    font-size: 16px;
}

</style>
<body>
    <div class="parent1">
        <div class="child">hello world-1</div>
    </div>
</body>
</html>

转载:https://www.cnblogs.com/jogen/p/5213566.html

jodiezhang avatar Jul 06 '19 15:07 jodiezhang

1、 使用position/transform/top/left( 已知宽高) 2 、 使用display:flex/flex/align-items: center;jusitify-content: center 3、 margin: 0 auto;(已知宽度) 4、 使用grid

     .parent{
          display: grid;
      }
      .left {
          justify-self: center; 
          align-self: center;
          /* 以上可以简写为  place-self: center;*/
          height: 100px;
          width: 100px;
          /* float: left; */
          background-color: blueviolet;
      }

image

MissNanLan avatar Jul 07 '19 15:07 MissNanLan

父元素 .container

子元素 .box

利用 flex 布局

/* 无需知道被居中元素的宽高 */
.container {
    display: flex;
    align-items: center;
    justify-content: center;
}

子元素是单行文本

设置父元素的 text-alignline-height = height

.container {
    height: 100px;
    line-height: 100px;
    text-align: center;
}

利用 absolute + transform

/* 无需知道被居中元素的宽高 */
/* 设置父元素非 `static` 定位 */
.container {
    position: relative;
}
/* 子元素绝对定位,使用 translate的好处是无需知道子元素的宽高 */
/* 如果知道宽高,也可以使用 margin 设置 */
.box {
    position: absolute;
    left: -50%;
    top: -50%;
    transform: translate(-50%, -50%);
}

利用 grid 布局

/* 无需知道被居中元素的宽高 */
.container {
    display: grid;
}
.box {
    justify-self: center; 
    align-self: center;
}

利用绝对定位和 margin:auto

/* 无需知道被居中元素的宽高 */
.box {
    position: absolute;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    margin: auto;
}
.container {
    position: relative;
}

YvetteLau avatar Jul 07 '19 16:07 YvetteLau

解决方案有如下几种

绝对定位:

     position:abaolute;
     left:50%:
     top:50%:
     margin-left: -width/2;
     margin-top: -width/2;

间距居中

position-absolute: 上下左右都为0;
margin:auto;

flex

display:flex
jusicitify-content:center;
align-items:center;

display:table-cell

  display:table-cell;
  vertical-align:middle
  text-align:center;

单行文本居中:

text-align:center;
line-height:height;

chongyangwang avatar Jul 14 '19 05:07 chongyangwang