美文网首页
CSS 简易居中 - 让你的偶像站在C位

CSS 简易居中 - 让你的偶像站在C位

作者: 进击的小短腿子 | 来源:发表于2019-12-19 13:21 被阅读0次

    居然有人总结了15种居中方式... 太多了,记不住。我们就说说三种简单常用的。

    来,我们让蔡徐坤站在C位(水平垂直居中):

    <body>
        <div class="stage" style="height: 200px;background:aliceblue">
            <div class="center">
                蔡徐坤
            </div>
        </div>
    </body>
    

    1.flex

    .stage {
         display: flex;
          justify-content: center;
          align-items: center;
     }
    

    等效

    .stage {
         display: flex;
     }
    .center {
         margin: auto;
     }
    

    2.absolute

    .stage {
        position: relative;
    }
    .center {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
    }
    

    3.line-height

    .stage {
        height: 200px;
    }
    .center {
        text-align: center;
        line-height: 200px;
    }
    

    相关文章

      网友评论

          本文标题:CSS 简易居中 - 让你的偶像站在C位

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