CSS之水平垂直居中

如何实现一个div的水平垂直居中

   <div class="content-wrapper">
      <div class="content">content</div>
    </div>

在这里插入图片描述

  1. flex布局
 .content-wrapper {
      width: 400px;
      height: 400px;
      background-color: lightskyblue;
      display: flex;
      justify-content: center;
      align-items: center;
 }
  1. transform
 .content-wrapper {
      width: 400px;
      height: 400px;
      background-color: lightskyblue;
      position: relative;
    }
    .content {
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translateX(-50%) translateY(-50%);
    }
  1. 定位
 .content-wrapper {
      width: 400px;
      height: 400px;
      background-color: lightskyblue;
      position: relative;
    }
    .content {
      width: 60px;
      height: 20px;
      position: absolute;
      left: 170px;
      top: 190px;
    }
  1. 计算距离
 .content-wrapper {
      width: 400px;
      height: 400px;
      background-color: lightskyblue;
      padding-top: 190px;
      padding-left: 170px;
      box-sizing: border-box;
    }
    .content-wrapper .content {
      width: 60px;
      height: 20px;
    }
  1. display:table-cell
  .content-wrapper {
      width: 400px;
      height: 400px;
      background-color: lightskyblue;
      display: table-cell;
      vertical-align: middle;
    }
    .content-wrapper .content {
      width: 100%;
      height: 20px;
      text-align: center;
    }
  1. line-height
  .content-wrapper {
      width: 400px;
      height: 400px;
      background-color: lightskyblue;
      line-height: 400px;
    }
    .content-wrapper .content {
      width: 100%;
      height: 20px;
      text-align: center;
    }
  1. margin: auto
  .content-wrapper {
      width: 400px;
      height: 400px;
      background-color: lightskyblue;
      position: relative;
    }
    .content {
      width: 60px;
      height: 20px;
      margin: auto;
      position: absolute;
      left: 0;
      top: 0;
      right: 0;
      bottom: 0;
    }

相关推荐

  1. CSS水平垂直居中

    2024-02-14 16:28:02       54 阅读
  2. CSS 垂直水平居中总结(全)

    2024-02-14 16:28:02       73 阅读
  3. CSS中的水平垂直居中元素的多种方式

    2024-02-14 16:28:02       67 阅读

最近更新

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

    2024-02-14 16:28:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-14 16:28:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-02-14 16:28:02       87 阅读
  4. Python语言-面向对象

    2024-02-14 16:28:02       96 阅读

热门阅读

  1. 深入理解C#中的事件驱动编程

    2024-02-14 16:28:02       47 阅读
  2. 利用英伟达(NVIDIA)RAPIDS实现GPU加速数据科学

    2024-02-14 16:28:02       62 阅读
  3. gstreamer 常用的图片格式转换命令

    2024-02-14 16:28:02       50 阅读
  4. 15.1 OpenGL可编程片段处理:片段着色器变量

    2024-02-14 16:28:02       54 阅读
  5. 2024/2/13 图的基础知识 3(拓扑排序)

    2024-02-14 16:28:02       56 阅读
  6. AcWing 1233. 全球变暖(bfs块问题)

    2024-02-14 16:28:02       49 阅读
  7. STM32自学☞PWM驱动LED呼吸灯

    2024-02-14 16:28:02       46 阅读