• css 常用垂直居中


    1. grid

     <style>
        .parent {
          display: grid;
          place-items: center;
          /* 等同align-items: center;justify-items: center;*/
           300px;
          height: 300px;
          border: 1px solid #ccc;
        }
        .son {
           50px;
          height: 50px;
          background: #f50;
        }
      </style>
      <div class="parent">
        <div class="son"></div>
      </div>
    

    2.flex

    .parent {
    display: flex;
    justify-content: center;
    align-items: center;
     300px;
    height: 300px;
    border: 1px solid #ccc;
    }
    

    3.flex+auto

     .parent {
          display: flex;
           300px;
          height: 300px;
          border: 1px solid #ccc;
        }
    
        .son {
          margin: auto;
        }
    

    4.table-cell

    .son {
           50px;
          height: 50px;
          background: #f50;
        }
    
        .parent {
          display: table-cell;
          text-align: center;
          vertical-align: middle;
           300px;
          height: 300px;
          border: 1px solid #ccc;
        }
    
        .son {
          display: inline-block;
        }
    

    5.absolute+transform

     .son {
           50px;
          height: 50px;
          background: #f50;
        }
    
        .parent {
           300px;
          height: 300px;
          border: 1px solid #ccc;
        }
    
        .parent {
          position: relative;
        }
    
        .son {
          position: absolute;
          left: 50%;
          top: 50%;
          transform: translate(-50%, -50%);
        }
    

    6.absolute+margin:auto

     .son {
           50px;
          height: 50px;
          background: #f50;
        }
    
        .parent {
           300px;
          height: 300px;
          border: 1px solid #ccc;
        }
    
        .parent {
          position: relative;
        }
    
        .son {
          position: absolute;
          left: 0;
          top: 0;
          right: 0;
          bottom: 0;
          margin: auto;
        }
    
  • 相关阅读:
    [HAOI2015]T2
    bzoj1036:[ZJOI2008]树的统计Count
    苹果树
    poj1151 Atlantis
    1593: [Usaco2008 Feb]Hotel 旅馆
    [JSOI2008]最大数maxnumber
    【HNOI2014】米特运输
    【HNOI2013】数列
    Luogu5221 Product
    【CQOI2014】数三角形
  • 原文地址:https://www.cnblogs.com/7c89/p/16000844.html
Copyright © 2020-2023  润新知