子元素在父元素水平垂直居中方法整理

1,579 阅读2分钟

前言

在网页的布局中,最常见的一种效果就是让子元素在父元素中水平垂直居中效果,虽说功能简单,但你有几种解决方式呢?今天整理了几种解决方式,一起来看下吧~


一、方案一

1. 思路

利用父元素相对定位,子元素决定定位,计算子元素与父元素之间的距离。控制子元素的topleft值将其定位到父元素的正中央位置。 缺点:如果子元素或者父元素宽高发生改变,那么就无法实现水平垂直居中了。

2. 代码演示

代码如下(示例):

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>计算距离定位实现</title>
    <style>
      .parent {
        width: 500px;
        height: 500px;
        border: 1px solid #000;
        position: relative;
      }
      .child {
        width: 100px;
        height: 100px;
        border: 1px solid #999;
        position: absolute;
        top: 50%;
        left: 50%;
        margin-top: -50px;
        margin-left: -50px;
      }
    </style>
  </head>
  <body>
    <div class="parent">
      <div class="child">我是子元素</div>
    </div>
  </body>
</html>

二、方案二

1. 思路

利用父相子绝的定位原理,给子元素设置margin为auto,从而实现在父元素水平垂直居中。

2. 代码演示

代码如下(示例):

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>控制外边距实现</title>
    <style>
      .parent {
        width: 500px;
        height: 500px;
        border: 1px solid #000;
        position: relative;
      }
      .child {
        width: 100px;
        height: 100px;
        border: 1px solid #999;
        position: absolute;
        margin: auto;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
      }
    </style>
  </head>
  <body>
    <div class="parent">
      <div class="child">我是子元素</div>
    </div>
  </body>
</html>

三、方案三

1. 思路

利用flex布局实现子元素水平垂直居中,使用display: flex;将父元素转变为flex布局,设置justify-contentalign-items让子元素水平垂直居中。 缺点:IE浏览器存在兼容性问题。

2. 代码演示

代码如下(示例):

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>flex布局实现</title>
    <style>
      .parent {
        width: 500px;
        height: 500px;
        border: 1px solid #000;
        display: flex;
        justify-content: center;
        align-items: center;
      }
      .child {
        width: 100px;
        height: 100px;
        border: 1px solid #999;
      }
    </style>
  </head>
  <body>
    <div class="parent">
      <div class="child">我是子元素</div>
    </div>
  </body>
</html>

四、方案四

1. 思路

先利用父相子绝,让子元素在父元素中移动父元素的50%距离,再利用transform让子元素移动自身宽度的50%,就能实现在父元素中水平垂直居中了。 :比较推荐的做法

2. 代码演示

代码如下(示例):

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>transform实现</title>
    <style>
      .parent {
        width: 500px;
        height: 500px;
        border: 1px solid #000;
        position: relative;
      }
      .child {
        width: 100px;
        height: 100px;
        border: 1px solid #999;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
      }
    </style>
  </head>
  <body>
    <div class="parent">
      <div class="child">我是子元素</div>
    </div>
  </body>
</html>

三、 效果演示

如下图所示,子元素已经实现在父元素中水平垂直居中效果。 在这里插入图片描述


总结

Do nothing by halves.