css
主页 > 网页 > css >

css常用元素水平垂直居中方案

2019-08-10 | 秩名 | 点击:
本篇文章介绍css常用元素水平垂直居中方案

flex实现水平垂直居中-[-/a>

适用场景:父子宽高都可未知(比较推荐这种方式,简单,而且目前兼容性也不错。)

 
<html>
  <head>
    <style>
    .parent {
      height: 100vh;
      width: 100vw;
      text-align: center;
      background: #c0c0c0;
    }
     
    .parent:before {
      content: "\200B";
      display: inline-block;
      height: 100%;
      vertical-align: middle;
    }
     
    .son {
      display: inline-block;
      vertical-align: middle;
      width: 200px;
      height: 200px;
      padding: 10px 15px;
      background: #f5f5f5;
    }
    </style>
  </head>
  <body>
    <div class="parent">
      <div class="son"></div>
    </div>
  </body>
</html>

原文链接:https://www.jb51.net/css/687449.html
相关文章
最新更新