CSS 居中对齐 (水平居中 )

水平居中

1.文本居中对齐

内联元素(给容器添加样式) 

限制条件:仅用于内联元素 display:inline 和 display: inline-block;

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .box {
        margin: 30px 30px 0px 300px;
        border: 1px solid gray;
        text-align: center;
      }

      .item {
        display: inline-block;
        width: 400px;
        background: yellow;
      }
    </style>
  </head>
  <body>
    <div class="box">
      <span>水平居中 -- display: inline</span>
    </div>
    <div class="box">
      <div class="item">水平居中 -- display: inline-block</div>
    </div>
  </body>
</html>

 自动外边距(块级元素)

限制条件:仅用于块级元素 display:block;

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .box {
        margin: 30px;
        border: 1px solid gray;
      }
      .item {
        margin: auto;
        width: 300px;
        background: yellow;
      }
    </style>
  </head>
  <body>
    <div class="box">
      <div class="item">水平居中 -- 块级元素 display:block</div>
    </div>
  </body>
</html>

 flex布局   

给容器添加样式 :display: flex; justify-content: center; (

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .box {
        margin: 30px 30px 0px 300px;
        border: 1px solid gray;
        display: flex;
        justify-content: center;
      }
      .item {
        width: 400px;
        background: yellow;
      }
    </style>
  </head>
  <body>
    <div class="box">
      <span>水平居中 -- flex布局 display: inline</span>
    </div>
    <div class="box">
      <div class="item">水平居中 -- flex布局 display: block</div>
    </div>
  </body>
</html>

子绝父相 + transform (CSS3)


限制条件:浏览器需支持CSS3,比较老的浏览器不适用
给容器(父元素)添加样式

position: relative
给内部元素添加样式
position: absolute;
left: 50%;
transform: translate(-50%, 0);

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .box {
        margin: 30px 30px 0px 300px;
        border: 1px solid gray;
        height: 50px;
        position: relative;
      }

      .item {
        background-color: yellow;
        position: absolute;
        left: 50%;
        transform: translate(-50%, 0);
      }
    </style>
  </head>
  <body>
    <div class="box">
      <span class="item">水平居中 -- 子绝父相 + transform</span>
    </div>
  </body>
</html>

 子绝父相 + 自动外边距 (指定宽度)

限制条件:内部元素需限定宽度

给容器(父元素)添加样式

position: relative


给内部元素添加样式

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

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .box {
        margin: 30px 30px 0px 300px;
        border: 1px solid gray;
        height: 50px;
        position: relative;
      }

      .item {
        background-color: yellow;
        position: absolute;
        left: 0;
        right: 0;
        margin: auto;
        width: 300px;
      }
    </style>
  </head>
  <body>
    <div class="box">
      <span class="item">水平居中 -- 子绝父相 + 自动外边距</span>
    </div>
  </body>
</html>

子绝父相 + 负外边距 (知道宽度 + 宽度计算)不退推荐

限制条件:需知道内部元素的宽度(无法预知宽度的内联元素和未知宽度的块级元素都不适用)

给容器(父元素)添加样式

position: relative

给内部元素添加样式

position: absolute;
left:50%;
margin-left:-内部元素宽度的一半
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .box {
        margin: 30px;
        border: 1px solid gray;
        height: 100px;
        position: relative;
      }
      .item {
        width: 400px;
        background: yellow;
        position: absolute;
        left: 50%;
        margin-left: -200px;
      }
    </style>
  </head>
  <body>
    <div class="box">
      <div class="item">水平居中 -- 绝对定位元素 position:absolute</div>
    </div>
  </body>
</html>

相关推荐

  1. CSS水平垂直居中

    2024-03-10 21:26:05       54 阅读
  2. CSSCSS水平居中方案

    2024-03-10 21:26:05       35 阅读
  3. 总结:css水平居中

    2024-03-10 21:26:05       36 阅读
  4. android 水平居中对齐并举例

    2024-03-10 21:26:05       35 阅读
  5. CSS 垂直水平居中总结(全)

    2024-03-10 21:26:05       72 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-10 21:26:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-10 21:26:05       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-10 21:26:05       82 阅读
  4. Python语言-面向对象

    2024-03-10 21:26:05       91 阅读

热门阅读

  1. MOGDB/openGauss数据库gs dump备份脚本及备份清理

    2024-03-10 21:26:05       36 阅读
  2. HarmonyOS4.0入门学习需要学习哪些知识点呢?

    2024-03-10 21:26:05       44 阅读
  3. openssl3.2 - exp - export RSA pubKey from RSA privKey on memory

    2024-03-10 21:26:05       38 阅读
  4. 8. Go实现Gin服务优雅关机与重启

    2024-03-10 21:26:05       42 阅读
  5. Android 二维码相关(一)

    2024-03-10 21:26:05       43 阅读
  6. 计算机基础专升本笔记十二-Excel常用快捷键大全

    2024-03-10 21:26:05       46 阅读
  7. SpringBoot多环境配置和logback日志记录器

    2024-03-10 21:26:05       43 阅读
  8. 关于设置html不让浏览器进行缓存的问题

    2024-03-10 21:26:05       42 阅读
  9. SQL如何添加数据?|SQL添加数据示例

    2024-03-10 21:26:05       47 阅读
  10. 使用Numpy手工模拟梯度下降算法

    2024-03-10 21:26:05       35 阅读