全国旗舰校区

不同学习城市 同样授课品质

北京

深圳

上海

广州

郑州

大连

武汉

成都

西安

杭州

青岛

重庆

长沙

哈尔滨

南京

太原

沈阳

合肥

贵阳

济南

下一个校区
就在你家门口
+
当前位置:首页  >  技术干货  >  详情

垂直居中的几种方法

来源:千锋教育
发布人:qyf
2023-02-21

推荐

在线提问>>

垂直居中的几种方法

  标签自带居中

  二、100%高度的after before加上inline-block

  三、div装成table

  四、margin-top:-50%;

  五、translate:-50%;

  六、position:absolute;margin:auto;

  七、display:flex;

  一:使用 table 标签,并给定高度,其子元素会自动实现垂直居中

<style>
  .parent{
    border: 1px solid red;
    height: 600px;
  }
  .child{
    border: 1px solid green;
  }
</style>
<table class="parent">
  <tr>
    <td class="child">hihihi</td>
  </tr>
</table>

  二:夹心饼干法 - 饼干高度为 100% + inline block

  给子标签的前后添加一个span标签,并将这两个span标签连同 child 设置为 inline-block,垂直对齐方式设置为居中,span标签的高度为 100%,撑开高度。

<body>
  <style>
    .parent{
      height: 200px;
      text-align: center;
    }
    .child{
      display: inline-block;
      vertical-align: middle;
    }
    .parent .before,
    .parent .after{
      display: inline-block;
      height: 100%;
      vertical-align: middle;
    }
  </style>
  <div class="parent">
    <span class=before></span>  
    <div class="child">hihihihi</div>
    <span class=after></span>
  </div>
</body>

  三、将 div 装成 table

  将父标签的 display 设置为 table,再设置子标签垂直居中: vertical-align: middle;

<body>
  <style>
    .table{
      display: table;
      border: 1px solid red;
      height: 600px;
    }

    .td{
      display: table-cell;
      border: 1px solid blue;
      vertical-align: middle;
    }
  </style>
  <div class="table">
      <div class="td">
        <div class="child">hihihi</div>
    </div>
  </div>
</body>

  四、使用定位

  绝对定位 + margin-top: 50%(子绝父相,先用绝对定位将子元素向下和向右移动父元素的百分之五十,再将子元素通过margin-top和margin-left移动自身的百分之五十(因为这一步有多种实现方式,所以第五种方法只是这里使用了平移))

<body>
  <style>
    .parent{
      height: 200px;
      position: relative;
    }
    .child{
      width: 300px;
      position: absolute;
      top: 50%;
      left: 50%;
      margin-left: -150px;
      height: 100px;
      margin-top: -50px;
    }
  </style>
  <div class="parent">
    <div class="child">hihihi</div>
  </div>
</body>

  五:绝对定位 + translate(-50%,-50%)

<body>
  <style>
    .parent{
      height: 200px;
      position: relative;
    }
    .child{
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%,-50%);
    }
  </style>
  <div class="parent">
    <div class="child">hihihi</div>
  </div>
</body>

  六:绝对定位 + margin: auto

<body>
  <style>
    .parent{
      height: 200px;
      position: relative;
    }
    .child{
       position: absolute;
       width: 100px;
      height: 100px;
      margin: auto;
      top: 0;
      bottom: 0;
      left: 0;
      right: 0;
    }
  </style>
  <div class="parent">
    <div class="child">hihihi</div>
  </div>
</body>

  七:使用flex布局

  flex布局中: justify-content: center; 设置为水平方向居中;align-items: center; 设置为垂直方向居中

<body>
  <style>
    .parent{
      height: 200px;
      border: 3px solid red;
      display: flex;
      justify-content: center; /* 水平方向居中 */
      align-items: center; /* 垂直方向居中 */
    }
    .child{
      border: 3px solid green;
      text-align:center;
      width: 100px;
    }
  </style>
  <div class="parent">
    <div class="child">hihihi</div>
  </div>
</body>

相关文章

如何为Apple iOS设计动态岛?

成为UI设计师需要具备哪些技能?

用户体验设计师做什么的?

设计的6大要素是什么?

Java项目中到底该怎么使用线程池?

开班信息 更多>>

课程名称
全部学科
咨询

HTML5大前端

Java分布式开发

Python数据分析

Linux运维+云计算

全栈软件测试

大数据+数据智能

智能物联网+嵌入式

网络安全

全链路UI/UE设计

Unity游戏开发

新媒体短视频直播电商

影视剪辑包装

游戏原画

    在线咨询 免费试学 教程领取