常用flex樣式類最全相容寫法詳解

混沌傳奇發表於2018-02-27

樣式程式碼

/* ============================================================
   flex:定義佈局為盒模型
   flex-v:盒模型垂直佈局
   flex-1:子元素佔據剩餘的空間
   flex-align-center:子元素垂直居中
   flex-pack-center:子元素水平居中
   flex-pack-justify:子元素兩端對齊
   相容性:ios 4+、android 2.3+、winphone8+、IE10+、Safari、Opera、Chrome、Firefox
   ============================================================ */
.flex{
    display: -webkit-box;
    display: -webkit-flex;
    display: -ms-flexbox;
    display: -moz-box;
    display: -moz-flex;
    display: flex;
}
.flex-v{
    -webkit-box-orient: vertical;
    -webkit-flex-direction: column;
    -moz-box-orient: vertical;
    -moz-flex-direction: column;
    -ms-flex-direction: column;
    flex-direction: column;
}
.flex-1{
    -webkit-box-flex: 1;
    -webkit-flex: 1;
    -moz-box-flex: 1;
    -moz-flex: 1;
    -ms-flex: 1;
    flex: 1;
}
.flex-align-center{
    -webkit-box-align: center;
    -webkit-align-items: center;
    -moz-box-align: center;
    -moz-align-items: center;
    -ms-flex-align: center;
    align-items: center;
}
.flex-pack-center{
    -webkit-box-pack: center;
    -webkit-justify-content: center;
    -moz-box-pack: center;
    -moz-justify-content: center;
    -ms-flex-pack: center;
    justify-content: center;
}
.flex-pack-justify{
    -webkit-box-pack: justify;
    -webkit-justify-content: space-between;
    -moz-box-pack: justify;
    -moz-justify-content: space-between;
    -ms-flex-pack: justify;
    justify-content: space-between;
}
複製程式碼

示例一:子元素水平兩端對齊

<style>
    .flex div{
        background-color: blue;
    }
</style>
<div class="flex flex-pack-justify">
    <div>模組一</div>
    <div>模組二</div>
    <div>模組三</div>
    <div>模組四</div>
</div>
複製程式碼

示例二:子元素水平居中對齊

<style>
    .flex div{
        background-color: blue;
    }
</style>
<div class="flex flex-pack-center">
    <div>模組一</div>
    <div>模組二</div>
    <div>模組三</div>
    <div>模組四</div>
</div>
複製程式碼

示例三:子元素垂直居中對齊

<style>
    .flex div{
        background-color: blue;
    }
</style>
<div class="flex flex-align-center">
    <div>模組一</div>
    <div>模組二</div>
    <div>模組三</div>
    <div>模組四</div>
</div>
複製程式碼

示例四:id為ohther的子元素佔據剩餘的空間

<style>
    .flex div{
        background-color: blue;
    }
</style>
<div class="flex">
    <div>模組一</div>
    <div class='flex-1' id='ohther' style='background: green;'>模組二</div>
</div>
複製程式碼

示例五:子元素垂直佈局

<style>
    .flex div{
        background-color: blue;
        border: 1px solid #eeeeee;
    }
</style>
<div class="flex flex-v">
    <div>模組一</div>
    <div>模組二</div>
    <div>模組三</div>
    <div>模組四</div>
</div>
複製程式碼

相關文章