CSS中的各种居中

该文总结了CSS中实现文本水平居中、垂直居中以及水平垂直居中的多种方法,包括使用text-align、display:flex、display:table-cell和position/transform属性的组合技巧。

在CSS布局时经常会需要实现水平居中,垂直居中,水平垂直居中这样的要求但是却又不是非常的在意,所以总结一下。

文本水平居中

1.使用text-align属性:将容器的text-align属性设置为"center",可以使文本在容器中水平居中。

<style>
        .container {
            width: 500px;
            height: 500px;
            border: solid 1px black;
  
            text-align: center; /*将文本水平居中*/
        }
        </style>
<body>
    <div class="container">你好世界</div>
</body>

2.使用display: flex和justify-content属性将容器内的内容水平居中。

<style>
      .container {
          width: 500px;
          height: 500px;
          border: solid 1px black;
  
          display: flex; /* 将容器设置为弹性盒子 */
          justify-content: center; /* 将弹性盒子内的内容水平居中 */
      }
</style>

<body>
    <div class="container">你好世界</div>
</body>

3.使用display: table和margin属性将容器内的内容水平居中。

<style>
      .container {
        width: 500px;
        height: 500px;
        border: solid 1px black;

        display: table-cell; /* 将容器设置为表格单元格 */
        text-align: center; /* 将文本水平居中 */
     }
</style>

<body>
    <div class="container">你好世界</div>
</body>

4.使用transform属性将文本水平垂直居中。

<style>
      .container {
          width: 500px;
          height: 500px;
          border: solid 1px black;

          position: relative; /* 将容器设置为相对定位 */
      }

      .container p {
          position: absolute; /* 将文本设置为绝对定位 */
          left: 50%; /* 将文本的左侧距离容器宽度的一半对齐 */
          transform: translateX(-50%); /* 将文本水平垂直居中 */
      }
    </style>

<body>
    <div class="container">
        <p>你好世界</p>
    </div>
</body>

最终效果:
效果图

文本垂直居中

1.使用line-height属性将单行文本垂直居中。

<style>
    .container {
      width: 500px;
      height: 500px;
      border: solid 1px black;

      line-height: 500px;
    }
</style>

<body>
    <div class="container">
        你好
    </div>
</body>

2.使用display: flex和align-items属性将容器内的内容垂直居中。

<style>
    .container {
      width: 500px;
      height: 500px;
      border: solid 1px black;

      display: flex; /* 将容器设置为弹性盒子 */
      align-items: center; /* 将弹性盒子内的内容垂直居中 */
    }
</style>

<body>
    <div class="container">
        你好
    </div>
</body>

3.使用position和transform属性将文本垂直居中。

<style>
    .container {
        width: 500px;
        height: 500px;
        border: solid 1px black;

        position: relative; /* 将容器设置为相对定位 */
    }

    .container p {
        position: absolute; /* 将文本设置为绝对定位 */
        top: 50%; /* 将文本的顶部与容器高度的一半对齐 */
        transform: translateY(-50%); /* 将文本垂直居中 */
    }
</style>

<body>
    <div class="container">
        <p>你好</p>
    </div>
</body>

4.使用display: table-cell和vertical-align属性将文本垂直居中。

<style>
      .container {
        width: 500px;
        height: 500px;
        border: solid 1px black;

        display: table-cell; /* 将容器设置为表格单元格 */
        vertical-align: middle; /* 将文本垂直居中 */
      }
</style>

<body>
    <div class="container">
        你好
    </div>
</body>

最终效果:

效果

文本水平垂直居中

1.使用display: flex和justify-content、align-items属性将容器内的内容水平垂直居中。

<style>
      .container {
        width: 500px;
        height: 500px;
        border: solid 1px black;

        display: flex; /* 将容器设置为弹性盒子 */
        justify-content: center; /* 将弹性盒子内的内容水平居中 */
        align-items: center; /* 将弹性盒子内的内容垂直居中 */
    }
</style>

<body>
    <div class="container">
        你好
    </div>
</body>

2.使用position和transform属性将文本水平垂直居中。

<style>
      .container {
        width: 500px;
        height: 500px;
        border: solid 1px black;

        position: relative; /* 将容器设置为相对定位 */
      }

      .container p {

        position: absolute; /* 将文本设置为绝对定位 */
        top: 50%; /* 将文本的顶部与容器高度的一半对齐 */
        left: 50%; /* 将文本的左侧与容器宽度的一半对齐 */
        transform: translate(-50%, -50%); /* 将文本水平垂直居中 */
        text-align: center; /* 将文本水平居中 */
        
      }
</style>

<body>
    <div class="container">
        <p>你好</p>
    </div>
</body>

3.使用display: table和vertical-align属性将文本水平垂直居中。

<style>
      .container {
        width: 500px;
        height: 500px;
        border: solid 1px black;

        display: table; /* 将容器设置为表格 */
      }

      .container p {

        display: table-cell; /* 将文本设置为表格单元格 */
        text-align: center; /* 将文本水平居中 */
        vertical-align: middle; /* 将文本垂直居中 */

      }
</style>

<body>
    <div class="container">
        <p>你好</p>
    </div>
</body>

最终效果:

效果

博客-CSS中的各种居中

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值