zoukankan      html  css  js  c++  java
  • CSS中垂直水平居中

    方法一:使用flex布局,父级元素设置justify-content和align-items

     <div class="cont">
        <div class="item"></div>
      </div>
    
       .cont {
                 100px;
                height: 100px;
                background: red;
                display: flex;
                justify-content: center;
                align-items: center;
            }
    
            .item {
                 50px;
                height: 50px;
                background: yellow
            }

    方法二:使用position和transform属性:这是css3里的样式;缺点:兼容性不好,只支持IE9+的浏览器

     <div class="cont">
        <div class="item"></div>
      </div>
    
      .cont {
                 100px;
                height: 100px;
                background: red;
                position: relative;
            }
    
            .item {
                 50px;
                height: 50px;
                background: yellow;
                position: absolute;
                left: 50%;
                top: 50%;
                transform: translate(-50%, -50%);
            }

    方法三:使用position和margin属性:兼容性好。缺点:必须知道元素的宽高

     <div class="cont">
        <div class="item"></div>
      </div>
    
      .cont {
                 100px;
                height: 100px;
                background: red;
                position: relative;
            }
    
            .item {
                  50px;
                height: 50px;
                background: yellow;
                position: absolute;
                left: 50%;
                top: 50%;
                margin-left: -25px;
                margin-top: -25px;
            }

    方法四:使用position和margin属性:兼容性好

     <div class="cont">
        <div class="item"></div>
      </div>
    
      .cont {
                 100px;
                height: 100px;
                background: red;
                position: relative;
            }
    
            .item {
                 50px;
                height: 50px;
                background: yellow;
                position: absolute;
                left: 0;
                right: 0;
                top: 0;
                bottom: 0;
                margin: auto;
            }
     
     
     
     
  • 相关阅读:
    android最恶心的是什么?
    有谁熟悉WordPress不?
    迷茫了....
    这事儿SB了....
    软件可靠性测
    养生之道
    我学员的一个问题及其我对之的解答,关于lr返回值问题
    如何自动启动小键盘
    测试工程师工作流程概论
    异构数据库之间的导入导出[转载]
  • 原文地址:https://www.cnblogs.com/songxia/p/11078465.html
Copyright © 2011-2022 走看看