美文网首页前端开发那些事儿
水平+垂直居中块状元素的各种方法整理

水平+垂直居中块状元素的各种方法整理

作者: microkof | 来源:发表于2021-01-20 15:33 被阅读0次

前言

这个是老话题了,就不多说了。有句话说在前面:

  1. 案例都是直接在body里居中,给body设置了特别样式,但实践中肯定不是在body中居中,案例只是简单举例。

  2. 如果要全屏居中, 需要做一个全屏fixed容器,然后在容器里居中。案例就不对此举例了。

支持子元素百分比宽高 兼容性
绝对定位 + margin:auto 支持 IE6
绝对定位 + 负margin 不支持 IE6
绝对定位 + calc 不支持 IE9
绝对定位 + translate 支持 IE9
table-cell 支持 IE8
Flex 支持 IE10
Grid 支持 IE10

绝对定位系列

绝对定位 + margin:auto

<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <style>
    /* 清除默认样式 */
    * {
      padding: 0;
      margin: 0;
    }

    /* 令html和body全屏显示, 并有一个灰色背景 */
    html,
    body {
      height: 100%;
      background: gray;
    }

    /* 先在父元素上设置相对定位 */
    body {
      position: relative;
    }

    .center {
      /* 绝对定位 */
      position: absolute;

      /* 上下左右全部为0 */
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;

      /* 给定宽高 */
      width: 70%;
      height: 25%;

      /* 令外边距自动填充 */
      margin: auto;

      /* 白色背景 */
      background: white;
    }
  </style>
</head>

<body>
  <div class="center"></div>
</body>

</html>

绝对定位 + 负margin

兼容性:IE6
特点:定位元素不支持百分比宽度和高度

<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <style>
    /* 清除默认样式 */
    * {
      padding: 0;
      margin: 0;
    }

    /* 令html和body全屏显示, 并有一个灰色背景 */
    html,
    body {
      height: 100%;
      background: gray;
    }

    /* 先在父元素上设置相对定位 */
    body {
      position: relative;
    }

    .center {
      /* 绝对定位 */
      position: absolute;

      /* 上方和左方为50%,这是写死的 */
      top: 50%;
      left: 50%;

      /* 给定宽高 */
      width: 300px;
      height: 200px;

      /* 上外边距为负的给定高度的一半 */
      margin-top: -100px;

      /* 左外边距为负的给定宽度的一半 */
      margin-left: -150px;

      /* 白色背景 */
      background: white;
    }
  </style>
</head>

<body>
  <div class="center"></div>
</body>

</html>

绝对定位 + calc

<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <style>
    /* 清除默认样式 */
    * {
      padding: 0;
      margin: 0;
    }

    /* 令html和body全屏显示, 并有一个灰色背景 */
    html,
    body {
      height: 100%;
      background: gray;
    }

    /* 先在父元素上设置相对定位 */
    body {
      position: relative;
    }

    .center {
      /* 绝对定位 */
      position: absolute;

      /* 减去的值是height和width的一半 */
      top: calc(50% - 100px);
      left: calc(50% - 150px);

      /* 给定宽高 */
      width: 300px;
      height: 200px;

      /* 白色背景 */
      background: white;
    }
  </style>
</head>

<body>
  <div class="center"></div>
</body>

</html>

绝对定位 + translate

<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <style>
    /* 清除默认样式 */
    * {
      padding: 0;
      margin: 0;
    }

    /* 令html和body全屏显示, 并有一个灰色背景 */
    html,
    body {
      height: 100%;
      background: gray;
    }

    /* 先在父元素上设置相对定位 */
    body {
      position: relative;
    }

    .center {
      /* 绝对定位 */
      position: absolute;

      /* 上方和左方为50% */
      top: 50%;
      left: 50%;

      /* 这个50%是相对于自身宽高而言的 */
      transform: translate(-50%, -50%);

      /* 宽高可以设百分比 */
      width: 30%;
      height: 40%;

      /* 白色背景 */
      background: white;
    }
  </style>
</head>

<body>
  <div class="center"></div>
</body>

</html>

table-cell

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <style>
    /* 清除默认样式 */
    * {
      padding: 0;
      margin: 0;
    }

    body {
      /* 令body全屏显示 */
      width: 100vw;
      height: 100vh;

      /* 显示为表格的格子 */
      display: table-cell;

      /* 水平居中 */
      text-align: center;

      /* 垂直居中 */
      vertical-align: middle;

      /* 灰色背景 */
      background: gray;
    }

    .center {
      /* 显示为行内块元素 */
      display: inline-block;

      /* 宽高支持百分比 */
      width: 30%;
      height: 40%;

      /* 白色背景 */
      background: white;
    }
  </style>
</head>

<body>
  <div class="center"></div>
</body>

</html>

Flex

<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <style>
    /* 清除默认样式 */
    * {
      padding: 0;
      margin: 0;
    }

    /* 令html和body全屏显示, 并有一个灰色背景 */
    html,
    body {
      height: 100%;
      background: gray;
    }

    /* 找到中央盒子的直接父元素 */
    body {
      /* 令其变成弹性布局 */
      display: flex;
    }

    .center {
      /* 自动外边距 */
      margin: auto;

      /* 白色背景 */
      background: white;

      /* 宽高支持百分比 */
      width: 30%;
      height: 40%;
    }
  </style>
</head>

<body>
  <div class="center"></div>
</body>

</html>

Grid

<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <style>
    /* 清除默认样式 */
    * {
      padding: 0;
      margin: 0;
    }

    /* 令html和body全屏显示, 并有一个灰色背景 */
    html,
    body {
      height: 100%;
      background: gray;
    }

    /* 中央盒子的直接父元素 */
    body {
      /* 令其变成网格布局 */
      display: grid;

      /* 令子元素居中 */
      place-items: center;
    }

    .center {
      /* 宽高支持百分比 */
      width: 30%;
      height: 40%;

      /* 白色背景 */
      background: white;
    }
  </style>
</head>

<body>
  <div>dsf sdf sd sd</div>
  <div class="center"></div>
</body>

</html>

相关文章

  • CSS水平居中和垂直居中的方法

    本篇文章主要介绍本人最近在CSS学习中整理总结出的水平&垂直居中的几种方法 水平居中 子元素为行内元素、单个块状及...

  • css各种居中方法总结

    CSS中的居中可分为水平居中和垂直居中。水平居中分为行内元素居中和块状元素居中两种情况,而块状元素又分为定宽块状元...

  • 水平居中和垂直居中

    水平居中设置--行内元素 水平居中设置--定宽块级元素 水平居中设置--不定宽块状元素 垂直居中--父元素高度确定...

  • CSS居中小技巧

    Ⅰ、水平居中设置-行内元素 Ⅱ、水平居中设置-定宽块状元素 Ⅲ、水平居中总结-不定宽块状元素方法 (一) (二) ...

  • 水平+垂直居中块状元素的各种方法整理

    前言 这个是老话题了,就不多说了。有句话说在前面: 案例都是直接在body里居中,给body设置了特别样式,但实践...

  • CSS布局

    [ 水平居中、垂直居中 ] 水平居中 子元素为行内元素还是块状元素,宽度一定还是宽度未定,采取的布局方案不同。下面...

  • css 图片居中

    css图片居中(水平居中和垂直居中) css图片水平居中 块状元素直接用text-align:center, di...

  • CSS - 垂直水平居中方法

    前言 总括:整理 css 垂直水平居中方法,区分内联元素与块级元素 CSS垂直居中和水平居中 用css让一个容器水...

  • CSS水平垂直居中布局

    一、水平居中 (1)行内元素的水平居中 (2)块状元素的水平居中(定宽) (3)块状元素的水平居中(不定定宽) 可...

  • 如何水平居中一个元素

    主要介绍水平居中,垂直居中,水平垂直居中的各种办法: 行内元素水平居中 如果块级元素内部包着也是一个块级元素,我们...

网友评论

    本文标题:水平+垂直居中块状元素的各种方法整理

    本文链接:https://www.haomeiwen.com/subject/tdlogktx.html