zoukankan      html  css  js  c++  java
  • 关于居中的问题

    说起居中就来气,说居中可以看出工作经验,我总结几个,大家可以参考下

    1、行内元素的居中

    这点不用多说,水平居中用text-align:center,垂直居中用line-hight:高值

    2、已知宽高的块级元素居中

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title></title>
            <style type="text/css">
            *{
                margin: 0;
                padding: 0;
            }
                #one{
                     400px;
                    height: 400px;
                    border: 1px solid red;
                    position: relative;/*父盒子要相对定位*/
                
                }
                #two{
                     100px;
                    height: 100px;
                    border: 1px solid blue;
                    position: absolute;/*子盒子要绝对定位*/
                    left: 50%;
                    top: 50%;
                    margin-top: -50px;/*向上偏移自身高度的一半*/
                    margin-left: -50px;    /*向左自身宽度的一半*/
                }
                    
            </style>
        </head>
        <body>
            <div id="one">
                <div id="two"></div>    
            </div>
        </body>
    </html>

    3、未知宽高的块级元素居中之table-cell

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title>居中table</title>
            <style type="text/css">
            *{
                margin: 0;
                padding: 0;
            }
                #one{
                     400px;
                    height: 400px;
                    border: 1px solid red;
                    display: table-cell;
                    vertical-align: middle;
                    text-align: center;
                    
                }
                #two{
                     100px;
                    height: 100px;
                    border: 1px solid blue;
                    display: inline-block;/*须转化为内联元素*/
                }
                
                
            </style>
        </head>
        <body>
            <div id="one">
                <div id="two"></div>    
            </div>
        </body>
    </html>

    4、未知宽高的块级元素居中之transform

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title>css3transform</title>
            <style type="text/css">
            *{
                margin: 0;
                padding: 0;
            }
                #one{
                     400px;
                    height: 400px;
                    border: 1px solid red;
                    position: relative;
                    
                
                }
                #two{
                     100px;
                    height: 100px;
                    border: 1px solid blue;
                    left: 50%;
                    top: 50%;
                    -webkit-transform: translate(-50%, -50%);
                    position: absolute;
                }
                
                
            </style>
        </head>
        <body>
            <div id="one">
                <div id="two"></div>    
            </div>
        </body>
    </html>

    5、未知宽高的块级元素居中之弹性盒子布局

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title>弹性盒子布局flex</title>
            <style type="text/css">
            *{
                margin: 0;
                padding: 0;
            }
                #one{
                     400px;
                    height: 400px;
                    border: 1px solid red;
                    display: flex;
                    justify-content: center;/*水平居中*/
                    align-items: center;/*垂直居中*/
                }
                #two{
                     100px;
                    height: 100px;
                    border: 1px solid blue;
                }
                
                
            </style>
        </head>
        <body>
            <div id="one">
                <div id="two"></div>    
            </div>
        </body>
    </html>


     

  • 相关阅读:
    windows7 下 apache2.4 和 php5.5 及 mysql5.6 的安装与配置
    JavaScript高级编程 (1)
    关于解决乱码问题的一点探索之二(涉及Unicode(utf-16)和GBK)
    关于解决乱码问题的一点探索之一(涉及utf-8和GBK)
    Windows上安装、配置MySQL的常见问题
    解析DXF图形文件格式
    vue三十一:vue配置反向代理
    vue三十:eslint修复错误和打包用于生产
    vue二十九:多个文件组件集成
    vue二十八:Vue-Cli之环境搭建之node安装脚手架和使用脚手架创建vue项目
  • 原文地址:https://www.cnblogs.com/goweb/p/5577025.html
Copyright © 2011-2022 走看看