美文网首页
CSS居中问题

CSS居中问题

作者: Z了个L | 来源:发表于2016-08-28 14:11 被阅读9次
    
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>居中</title>
         <!--
           水平居中:
             行内标签 和 行内-块级标签  在父标签中设置  text-align: center;
             块级标签:  在自身设置 margin: 0 auto;
    
    
           垂直居中:
              行内标签 和 行内-块级标签  在父标签中设置  line-height = height
              块级标签:  定位
    
        -->
        <style>
            .main{
                width: 500px;
                height: 300px;
                background-color: red;
    
                /*垂直居中*/
                line-height: 300px;
    
                /*水平居中*/
                text-align: center;
    
                position: relative;
            }
    
            span{
                background-color: blue;
            }
    
            .test {
                background-color: yellow;
                width: 200px;
    
                height: 150px;
                line-height: 150px;
    
                /*水平居中*/
                /*margin-left: auto;*/
                /*margin-right: auto;*/
    
                margin:0 auto;
    
                position: absolute;
                left:50%;
                top:50%;
                transform: translate(-50%, -50%);
            }
        </style>
    </head>
    <body>
        <div class="main">
            <!--行内标签-->
            <!--<span>行内标签</span>-->
            <!--行内-块级标签-->
            <!--<button>我是按钮</button>-->
            <!--块级标签-->
            <div class="test">块级标签</div>
        </div>
    </body>
    </html>
    
    

    相关文章

      网友评论

          本文标题:CSS居中问题

          本文链接:https://www.haomeiwen.com/subject/wlntettx.html