zoukankan      html  css  js  c++  java
  • CSS知识点

    实现div盒子水平垂直居中的几种方式

      <body id="container">
          <div id="middle"></div>
      </body>

      <style>

        html,body {
          height:100%; /*  注意html不设置高度 body也就没有继承高度,就会随内容支撑高度

          overflow: hidden; */ //方式一

          margin:0;

          padding: 0;

        }

      </style>

      1.第一种方式元素已知宽度高度,绝对定位absolute+margin一半

      #middle {
        background: red;

         200px;

        height: 200px;

        position: absolute;

        top: 50%;

        left: 50%;

        margin-left: -100px;

        margin-top: -100px;

      }

      2. 第二种方式元素未知,绝对定位absolute+translate(平移一半)

      #middle {
        background: red;

         200px;

        height: 200px;

        position: absolute;

        top: 50%;

        left: 50%;

        transform: translate(-50%,-50%);

      }

      3. 第三种方式元素未知,绝对定位absolute+margin:auto

      #middle {
        background: red;

         200px;

        height: 200px;

        position: absolute;

        left: 0;

        right: 0;

        top: 0;

        bottom: 0;

        margin: auto;

      }

      4. 第二种方式元素未知,display:flex

      #container{

        display: flex;

        justify-content: center;

        align-items: center;

      }

  • 相关阅读:
    下拉选择框,允许手动输入和过滤
    MVC数据绑定
    一个页面多个ng-app注意事项
    modal 多层弹窗 Maximum call stack size exceeded 解决方法
    VS10x CodeMap 注册码(key):
    VS2015卸载再安装
    VS2015无法创建工程
    解决VS2015版本key required问题手动方案
    猪猪公寓—事后诸葛亮
    猪猪公寓——测试总结
  • 原文地址:https://www.cnblogs.com/vofill/p/14801986.html
Copyright © 2011-2022 走看看