1-css的多种垂直居中的方法

作者: _panda | 来源:发表于2017-08-03 16:24 被阅读79次

多种css垂直居中的方法

==css3不定宽高水平垂直居中==

-webkit-justify-content:center;
justify-content:center;//子元素水平居中
-webkit-align-items:center;
align-items:center;//子元素垂直居中
display:-webkit-flex;
display:flex;

1:固定高宽的垂直居中

如上图,固定高宽的很简单,写法如下:

position: absolute;
left: 50%;
top: 50%;
width:200px;
height:100px;
margin-left:-100px;
margin-top:-50px;

2: 不固定高宽div垂直居中的方法

方法一:
用一个“ghost”伪元素(看不见的伪元素)和 inline-block / vertical-align 可以搞定居中,非常巧妙。但是这个方法要求待居中的元素是 inline-block,不是一个真正通用的方案。
html如下:

<div class="block" style="height: 300px;">

    <div class="centered">
        <h1>haorooms案例题目</h1>
        <p>haorooms案例内容,haorooms案例内容haorooms案例内容haorooms案例内容haorooms案例内容haorooms案例内容haorooms案例内容haorooms案例内容haorooms案例内容</p>
    </div>

</div>
css如下:
/* This parent can be any width and height */
.block {
  text-align: center;
}

/* The ghost, nudged to maintain perfect centering */
.block:before {
  content: '';
  display: inline-block;
  height: 100%;
  vertical-align: middle;
  margin-right: -0.25em; /* Adjusts for spacing */
}

/* The element to be centered, can
   also be of any width and height */ 
.centered {
  display: inline-block;
  vertical-align: middle;
  width: 50%;
}

方法二:table布局

<table style="width: 100%;">
  <tr>
     <td style="text-align: center; vertical-align: middle;">
          Unknown stuff to be centered.
     </td>
  </tr>
</table>
//由于table写法比较费时,你也可以用div代替table,写法如下:
//html:
<div class="something-semantic">
   <div class="something-else-semantic">
       Unknown stuff to be centered.
   </div>
</div>

css:

.something-semantic {
   display: table;
   width: 100%;
}
.something-else-semantic {
   display: table-cell;
   text-align: center;
   vertical-align: middle;
}

方法三: 终极解决方法

以上2中方法可能都有其局限性,我介绍的第三中方法是比较成熟的不是固定高宽div的垂直居中的方法!但是方法是css3的写法,想兼容IE8的童鞋们,建议用上面的方法!
方法和我们固定高宽的差不多,但是不用margin我们用的是 translate()

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>haorooms不固定高度div写法</title>

    <style>
.center {
  position: fixed;
  top: 50%;
  left: 50%;
  background-color: #000;
  width:50%;
  height: 50%;
-webkit-transform: translateX(-50%) translateY(-50%);
}


    </style>
</head>
<body>
    <div class="center"></div>
</body>
</html>

我上面的css只是针对webkit内核的浏览器,其他内核浏览器写法如下:

-webkit-transform: translateX(-50%) translateY(-50%);
-moz-transform: translateX(-50%) translateY(-50%);
-ms-transform: translateX(-50%) translateY(-50%);
transform: translateX(-50%) translateY(-50%);

关于每个浏览器的写法,可以看看我之前的一篇文章:http://www.haorooms.com/post/css_common
有些弹出层的样式,也可以用这个方法居中

position: fixed;
top: 50%;
left: 50%;
width: 50%;
max-width: 630px;
min-width: 320px;
height: auto;
z-index: 2000;
visibility: hidden;
-webkit-backface-visibility: hidden;
-moz-backface-visibility: hidden;
backface-visibility: hidden;
-webkit-transform: translateX(-50%) translateY(-50%);
-moz-transform: translateX(-50%) translateY(-50%);
-ms-transform: translateX(-50%) translateY(-50%);
transform: translateX(-50%) translateY(-50%);
运用margin:auto进行垂直居中
//实现html如下:(做一个简单的垂直弹框)

<div class="father">
    <div class="son"></div>
</div>
//css代码如下,很简单,兼容性也蛮好,支持IE8+

.father{
    position:fixed;
    width:100%;
    height:100%;
    top;0;left:0;
    background-color:rgba(0,0,0,.7);
    }
.son{
    position: 
    absolute;top:0;left:0;bottom:0;right:0;
    width:50%;height:50%;
    margin:auto;
    background-color:red;
    }

margin的值设置为auto,可以让我们对剩余空间进行分配!我们知道,块级元素设置为margin:0 auto;可以左右居中显示!那有没有办法让margin设置为margin:auto之后,上下左右都居中呢?上下左右都居中,就可以实现我们的垂直居中了!

==答案是有的,只要我们让上下有足够的空间,就可以让margin的auto来分配上下空间。
我们可以利用定位的方式,让margin上下左右都有足够的空间!那么就可以用margin:auto来实现垂直居中了==

相关文章

  • 1-css的多种垂直居中的方法

    多种css垂直居中的方法 ==css3不定宽高水平垂直居中== 1:固定高宽的垂直居中 如上图,固定高宽的很简单,...

  • 用CSS实现元素居中的N种方法

    网页布局中,元素水平居中比垂直居中简单不少,同时实现水平垂直居中也不难,难的是想出多种实现水平垂直居中的方法并从中...

  • CSS解决盒模型居中的问题

    CSS实现盒子模型水平居中、垂直居中、水平垂直居中的多种方法 CSS实现盒子模型水平居中的方法 全局样式 第一种:...

  • 常用的居中方法

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

  • CSS垂直水平居中总结

    css实现垂直水平居中的方法多种,各种方法各有优劣,查阅网上资料,自己总结于此,方便日后应用。 1. 绝对定位居中...

  • CSS水平垂直居中总结

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

  • 垂直居中的多种写法

    1、垂直居中 2、水平居中 3、垂直水平居中 方法1:使用绝对定位 方法二:使用display:flex 扩展1:...

  • CSS居中之垂直居中(多种方法实现)

    table-cell+vertical-align 当有多个子div时,运用该方法效果如下 当子元素为inline...

  • 2018-05-23

    div高度不固定,垂直居中的方法-汇总 1、高度宽度不固定,内容区域垂直居中

  • 水平居中与垂直居中

    水平居中和垂直居中可以说是在开发中经常遇到的问题,网上搜索水平居中和垂直居中的方法有很多,有的方法并不是很好,所以...

网友评论

    本文标题:1-css的多种垂直居中的方法

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