美文网首页
垂直居中

垂直居中

作者: 逆风飘游的鱼 | 来源:发表于2019-08-26 10:07 被阅读0次

https://jscode.me/t/topic/1936

如果 .parent 的 height 不写,你只需要 padding: 10px 0; 就能将 .child 垂直居中;

如果 .parent 的 height 写死了,就很难把 .child 居中,以下是垂直居中的方法。

忠告:能不写 height 就千万别写 height。

1.table自带功能

<table class="parent">

    <tr>      <td class="child">    一串文字一串文字一串文字一串文字    </td>    </tr>

  </table>

.parent{  height: 600px;}

2.div 装成 table

<div class="table">

<div class="td"> <div class="child">一串文字一串文字一串文字 </div></div>

  </div>

div.table{  display: table;  height: 600px;}

div.td{  display: table-cell;  vertical-align: middle;}

3.margin-top -50%

<div class="parent">  <div class="child">一串文字一串文字</div>  </div>

.parent{ height: 600px;  position: relative;}

.child{   position: absolute;  top: 50%; margin-top: 负的高度一半;}

4.translate -50%

<div class="parent"> <div class="child">  一串文字一串文字 </div></div>

.parent{ height: 600px;  position: relative;}

.child{  position: absolute;  top: 50%; transform: translateY(-50%);}

5.absolute margin auto

<div class="parent"> <div class="child">  一串文字一串文字 </div></div>

.parent{  height: 600px; position: relative;}

.child{  position: absolute;  height: 200px;  margin: auto;  top: 0;  bottom: 0;  left: 0;  right: 0;}

6.flex

<div class="parent"> <div class="child">  一串文字一串文字 </div></div>

.parent{  height: 600px;  display: flex;  justify-content: center;  align-items: center;}

相关文章

  • CSS居中布局方案

    水平居中 垂直居中 水平垂直居中

  • 元素居中的方式

    1 水平居中 2 垂直居中 3 水平垂直居中

  • 常用的居中方法

    本文将介绍的居中方法有 ,水平居中,垂直居中和水平垂直居中。 一水平居中 二水平垂直居中

  • 居中布局

    水平居中 垂直居中 垂直水平居中 已知元素的宽高的居中布局 定位居中布局 盒模型居中布局 图片的垂直水平居中(利用...

  • CSS水平垂直居中总结

    CSS水平居中、垂直居中、水平垂直居中方法总结 文字的水平居中: 单行文字的垂直居中: 让有宽度的div水平居中:...

  • 垂直居中

    文字的垂直居中 元素的垂直居中

  • CSS图片居中(水平居中和垂直居中)

    css图片水平居中 css图片垂直居中 css图片水平垂直居中

  • 居中对齐

    行内元素居中[#hang]垂直居中[#hc]水平居中[#hs] 块级元素居中[#kuai]垂直居中[#kc]水平居...

  • CSS居中大全(带截图)

    文字水平居中 图片水平垂直居中 图片与文字水平垂直居中 代码同上 DIV相对于页面垂直居中并且响应式 视口绝对垂直...

  • css 居中

    居中有水平居中和垂直居中。 水平居中+垂直居中 flex法 position法 就是计算呗~ 参考 CSS各种居中...

网友评论

      本文标题:垂直居中

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