zoukankan      html  css  js  c++  java
  • 让div水平垂直居中的几种方法

    结构层

     <div class='container'>
          <div>123</div>
     </div>

    样式层

    .container{
                width: 500px;
                height: 500px;
                margin:0 auto;
                border:1px solid red;
                /* 一 二 三 五样式 */
                /* position: relative; */
                /* 第四种的样式 */
                /* display: flex;
                justify-content: center;
                align-items: center; */
                /* 第六种样式 */
                /* display: table; */
                /* 第七种样式 */
                display: flex;
            }
            /* 第一种 */
            /* .container div{
                 100px;
                height:100px;
                background:rebeccapurple;
                position: absolute;
                left:0;
                top:0;
                right:0;
                bottom:0;
                margin:auto;
            } */
            /* 第二种 */
            /* .container div{
                 200px;
                height: 200px;
                background:red;
                position: absolute;
                left:50%;
                top:50%;
                margin-left:-100px;
                margin-top:-100px;
            } */
            /* 第三种 */
            /* .container div{
                 200px;
                height: 200px;
                background:red;
                position: absolute;
                top:50%;
                left:50%;
                transform: translate(-50%,-50%);
            } */
            /* 第四种 弹性布局*/
            /* .container div{
                 200px;
                height: 200px;
                background-color: salmon;
            } */
            /* 第五种 */
            /* .container div{
                position: absolute;
                background-color: salmon;
                 200px;
                height: 200px;
                left:calc((500px - 200px)/2);
                top:calc((500px - 200px)/2);
                left:-webkit-calc((500px - 200px)/2);
                top:-webkit-calc((500px - 200px)/2);
            } */
            /* 第六种 */
            /* .container div{
                 200px;
                height: 200px;
                display: table-cell;
                vertical-align: middle;
                text-align: center;
                background:red;
            } */
            /* 第七种 */
            .container div{
                width: 200px;
                height: 200px;
                background-color: slateblue;
                margin:auto;
                }

    结果

  • 相关阅读:
    [转载]windows进程中的内存结构
    RTTI和4个强制转换运算符
    由于应用程序配置不正确,程序未能启动
    光照
    服务器数据同步
    SAP
    PHP输入流php://input
    五款常用mysql slow log分析工具的比较
    开发搜索引擎–PHP中文分词
    大型网站调试工具之一(php性能优化分析工具XDebug)
  • 原文地址:https://www.cnblogs.com/shine1234/p/13387787.html
Copyright © 2011-2022 走看看