zoukankan      html  css  js  c++  java
  • 水平垂直居中的布局(定宽高和不定宽高)

    一、定宽高

    1、绝对定位和负margin值:

      <section class="absolute">
            <div></div>
       </section>
       <style>
        section{
                display:block;     
        }
        section.absolute {
                width: 100px;
                height: 100px;
                border: 1px solid #ccc;
                position: relative;
            }
            
            .absolute div {
                position: absolute;
                width: 50px;
                height: 50px;
                left: 50%;
                top: 50%;
                margin: -25px 0 0 -25px;
                background-color: yellow;
            }
       </style>

    2、绝对定位加 transform:

    <section class="absoluteTransform">
            <div></div>
    </section>
    <style>
         section{
                display:block;
         }
         section.absoluteTransform {
                width: 100px;
                height: 100px;
                border: 1px solid #ccc;
                position: relative;
          }
            
          .absoluteTransform div {
                position: absolute;
                width: 50px;
                height: 50px;
                left: 50%;
                top: 50%;
                background-color: yellow;
                -webkit-transform: translate(-50%, -50%);
           }
    </style>

    3、绝对定位 + left/right/bottom/top + margin:

    <section class="absoluteM">
            <div></div>
    </section>
    <style>
            section{
                display: block;
           }
             section.absoluteM {
                width: 100px;
                height: 100px;
                border: 1px solid #ccc;
                position: relative;
            }
            
            .absoluteM div {
                width: 50px;
                height: 50px;
                background-color: yellow;
                position: absolute;
                left: 0;
                top: 0;
                right: 0;
                bottom: 0;
                margin: auto;
            }          
    </style>    

    4、flex 布局:

        <section class="flex">
            <div></div>
        </section>
        <style>
            section{
               display: block;
            }
            section.flex {
                width: 100px;
                height: 100px;
                border: 1px solid #ccc;
                display: flex;
                justify-content: center;
                align-items: center;
            }
            
            .flex div {
                width: 50px;
                height: 50px;
                background: yellow;
            }
        </style>

    5、grid布局:

        <section class="grid">
            <div></div>
        </section>
        <style>
            section{
               display: block;
            }
             section.grid {
                width: 100px;
                height: 100px;
                border: 1px solid #ccc;
                display: grid;
            }
            
            .grid div {
                width: 50px;
                height: 50px;
                background-color: yellow;
                margin: auto;
            }
        </style>

    6、table 布局:

        <section class="table">
            <div></div>
        </section>
        <style>
            section{
               display: block;
            }
            section.table {
                width: 100px;
                height: 100px;
                border: 1px solid #ccc;
                display: table-cell;
                text-align: center;
                vertical-align: middle;
            }
            
            .table div {
                width: 50px;
                height: 50px;
                background-color: yellow;
                display: inline-block;
            }
        </style>

    二、不定宽高

    1、绝对定位加 transform:

    <div class="box">
          <div class="children-box">111111</div>
     </div>
    
    <style>
     .box {
        width: 200px;
        height: 200px;
        border: 1px solid red;
        position: relative;
    }
    .children-box {
       position: absolute;
       background: yellow;
       left: 50%;
       top: 50%;
       transform: translate(-50%, -50%);
    }       
    </style>

    2、table布局:

    <div class="box">
         <div class="children-box">111111</div>
    </div>
    
    <style>
    .box {
        width: 200px;
        height: 200px;
        border: 1px solid red;
        display: table-cell;
        text-align: center;
        vertical-align: middle;
    }
    .children-box {
       background: yellow;
       display: inline-block;
    }
    </style>

    3、flex 布局:

    <div class="box">
        <div class="children-box">11111111</div>
    </div>
    
    <style>
    .box {
        width: 200px;
        height: 200px;
        border: 1px solid red;
        display: flex;
        justify-content: center;
        align-items: center;
    }
    .children-box {
        background: yellow;
    }
    </style>

    4、flex 变异布局:

    <div class="box">
          <div class="children-box">11111111</div>
    </div>
    
    <style>
    .box {
        width: 200px;
        height: 200px;
        border: 1px solid red;
        display: flex;
    }
    .children-box {
        background: yellow;
        margin: auto;
    }
    </style>

    5、grid + flex 布局:

    <div class="box">
           <div class="children-box">11111111</div>
    </div>
    
    <style>
    .box {
        width: 200px;
        height: 200px;
        border: 1px solid red;
        display: grid;
    }
    .children-box {
        background: yellow;
        align-self: center;
        justify-self: center;
    }
    </style>

    6、grid + margin 布局:

    <div class="box">
           <div class="children-box">11111111</div>
    </div>
    
    <style>
    .box {
        width: 200px;
        height: 200px;
        border: 1px solid red;
        display: grid;
    }
    .children-box {
        background: yellow;
        margin: auto;
    }
    </style>
  • 相关阅读:
    android 拖放功能
    android 桌面文件夹ui美化
    instanceof 杂谈
    Android自定义长按事件
    launcher in android
    类似网易163TAB选项卡(标签)代码
    【新手入门教程】简洁纯CSS下拉导航菜单代码
    橘黄色的大气CSS菜单代码
    Flash效果的网站后台左侧Js多级展开菜单代码
    仿FLASH动感十足鼠标滑过放大的菜单代码
  • 原文地址:https://www.cnblogs.com/tg666/p/12295566.html
Copyright © 2011-2022 走看看