美文网首页
使用flex设置垂直和水平居中

使用flex设置垂直和水平居中

作者: shirleyyu001 | 来源:发表于2017-01-14 00:46 被阅读0次

flex语法:http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html?utm_source=tuicool

基本的垂直水平居中
//css
.center-wrap{
    display:flex;
    align-items: center;
    justify-content: center;
}
//html
<div class="center-wrap">
    <div>this is the box which need be setted</div>
</div>
扩展:

若多个元素需要进行垂直居中,用以下方法可以减少DOM元素的使用:

多个元素相对于父元素垂直居中.png
//css
.box {
        width: 300px;
        height: 300px;
        margin: 0 auto;
        border: 1px solid;
        -webkit-justify-content: center;
        justify-content: center;  /* 此处是关键*/
        -webkit-align-items: center;
        align-items: center;  /* 此处是关键*/
        -webkit-flex-direction: column;
        flex-direction: column;  /* 此处是关键*/
    }
    .title {
        font-size: 14px;
        font-weight: normal;
    }
    .img {
        width: 200px;
        margin: 20px 0;
    }
    .btn {
        width: 100px;
        height: 28px;
        color: #fc6537;
        border-radius: 5px;
        border: 1px solid #fc6537;
        background-color: #fff;
    }

//html
<div class="box">
    <hl class="title">多个元素相对于父元素垂直居中</hl>
    ![](images/flex-direction.jpg)
    <button class="btn">click me</button>
</div>

相关文章

  • [2020-08-10]css3中的常用几种布局

    div的水平垂直居中 flex布局,使用display: flex实现水平垂直居中 grid布局 使用grid布局...

  • 块元素水平垂直居中

    块元素水平垂直居中 使用flex布局将子元素水平垂直居中 使用position定位 或

  • 垂直居中的多种写法

    1、垂直居中 2、水平居中 3、垂直水平居中 方法1:使用绝对定位 方法二:使用display:flex 扩展1:...

  • 关于水平垂直居中的问题

    文字水平居中 文字水平垂直居中 设置padding高度自动撑开 flex 子元素在父元素中水平垂直居中 方法一 ...

  • 使用flex设置垂直和水平居中

    flex语法:http://www.ruanyifeng.com/blog/2015/07/flex-gramma...

  • html,css基础知识

    1.怎么让一个不定宽高的 DIV,垂直水平居中? 使用Flex 只需要在父盒子设置:display: flex; ...

  • css 居中

    居中有水平居中和垂直居中。 水平居中+垂直居中 flex法 position法 就是计算呗~ 参考 CSS各种居中...

  • 面试题 -- 水平垂直居中

    水平居中 元素的水平垂直居中 1.定位 2.用translate设置宽、高 3.flex 4.table-cell

  • flex布局

    flex布局小技巧,想要让元素水平垂直居中,可以设置为margin:auto auto;

  • CSS布局小技巧

    1.让元素水平垂直居中 文字水平垂直居中 2.让元素垂直居中 2.1 flex方式 2.2 position方式 ...

网友评论

      本文标题:使用flex设置垂直和水平居中

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