zoukankan      html  css  js  c++  java
  • Day14:CSS垂直居中

    verticle-align:middle

    vertical-align:middle实现css垂直居中是常用的方法,但是需要注意,vertical生效的前提是diaplay:inline-block

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>css水平居中</title>
    <style>
    #out {
     background: blue;
      600px;
     height: 300px;
    }
    #in {
     background: yellow;
      50%;
     height: 50%;
     display: inline-block;
     vertical-align: middle;
    }
    </style>
    </head>
    <body>
    <div id="out">
     <div id="in"></div>
     </div>
    </body>
    </html>
    

    display: flex实现

    display:flex实现css垂直居中的方法是给父元素display: flex;而子元素align-self:center;

    <style>
    #out{
    background: blue;
     600px;
    height: 300px;
    display: flex;
    }
    #in {
    background: yellow;
     50%;
    height: 50%;
    align-self: center;
    }
    父元素display:flex
    子元素align-self:center
    

    伪元素:before实现CSS垂直居中

    <style>
    #out{
    background:blue;
     600px;
    height: 300px;
    display: block;
    }
    #out:before{
    content: '';
    display:inline-block;
    vertical-align:middle;
    height:100%;
    }
    #in{
    background: yellow;
     50%;
    height: 50%;
    display: inline-block;
    vertical-align: middle;
    }
    </style>
    

    display:table-cell实现css垂直居中

    给父元素display:table,子元素display: table-cell的方式实现css垂直居中。

    <style>
    #out {
    background: blue;
     600px;
    height: 300px;
    dispaly: table;
    }
    #in{
    background: yellow;
     50%;
    height: 50%;
    display:table-cell;
    vertical-align:middle;
    }
    </style>
    

    隐藏节点实现CSS垂直居中

    <style>
    #out{
    background: blue;
     600px;
    height: 300px;
    }
    #hide{
     50%;
     height: 25%;
    }
    隐藏节点的height为剩余高度的一半
    #in {
    background: yellow;
     50%;
    height: 50%;
    }
    </style>
    <body id="out">
    <div id="hide"></div>
    <div id="in"></div>
    </body>
    

    通过transform实现CSS垂直居中

    <style>
    #out {
    background: blue;
     600px;
    height: 300px;
    }
    #in{
    background: yellow;
     50%;
    height: 50%;
    position: relative;
    top: 50%;
    transform: translateY(--50%);
    }
    </style>
    

    line-height实现CSS垂直居中

    <style>
    #out {
    background: blue;
     600px;
    height: 300px;
    }
    #in {
      50%;
     height: 50%;
     line-height: 300px;
    }
    </style>
    
    <body>
    <div id="out">
     <p id="in">css</p>
    </div>
    </body>
    

    请点赞!因为你的鼓励是我写作的最大动力!

    官方微信公众号

    吹逼交流群:711613774

    吹逼交流群

  • 相关阅读:
    No enclosing instance of type XXX is accessible.
    No enclosing instance of type XXX is accessible.
    Websphere 学习(一)
    List去重与equals/hashcode
    List去重与equals/hashcode
    org.apache.log4j.Logger详解
    org.apache.log4j.Logger详解
    onclick="return checkForm()" 、onclick="checkForm();return false;"解析 与 return false;
    onclick="return checkForm()" 、onclick="checkForm();return false;"解析 与 return false;
    大数据基础第一天内容
  • 原文地址:https://www.cnblogs.com/dashucoding/p/11932373.html
Copyright © 2011-2022 走看看