前端相关

CSS 3 Flex布局

2017-08-04  本文已影响7人  DeeJay_Y

Flex布局

flex布局可以实现空间自动分配自动对齐,适用于简单的线性布局。

flex基本概念

flex基本概念
  1. flex container的属性

`2. flex item属性

使用flex布局

<style>
    *{
        margin: 0;
        padding: 0;
        box-sizing: border-box;
        list-style: none;
    }
    .container{
        height: 100vh;
        display: flex;
        flex-direction: column;
        /*使用列布局*/
    }
    .header{
        height: 100px;
        background: #98ff79;
    }
    .main{
        flex-grow: 1;
        /*将剩下的空间分配给main*/

        overflow: auto;
        /*中间内容超出时增加滚动条,不用给下面ul>li增加fix定位*/
    }
    .footer{
        height: 100px;
        background: #e69dff;
    }
    .footer ul{
        width: 100%;
        height: 100%;
        display: flex;
    }
    .footer ul li{
        width: 25%;
        height: 100%;
        border: 1px solid black;
    }
</style>

<div class="container">
    <div class="header">header</div>
    <div class="main">main</div>
    <div class="footer">
        <ul>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
    </div>
</div>
<style>
    *{
        margin: 0;
        padding: 0;
        box-sizing: border-box;
        list-style: none;
    }
    ul{
        display: flex;
        flex-wrap: wrap;
        width: 350px;
        margin: 0 auto;
        border: 1px solid red;
        justify-content: space-between;
    }
    li{
        width: 100px;
        height: 100px;
        background: #83ffe6;
        border: 1px solid black;
    }
</style>

<ul>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
</ul>
<style>
    *{
        margin: 0;
        padding: 0;
    }
    .header{
        height: 50px;
        background: #a4ff6a;
    }
    .footer{
        height: 50px;
        background: #ff7cf4;
    }
    .content{
        display: flex;
    }
    .left{
        width: 200px;
        background: #c4beff;
    }
    .main{
        height: 400px;
        background: #ffb894;
        flex-grow: 1;
    }
    .right{
        width: 200px;
        background: #ff988a;
    }
</style>

<div class="header">header</div>
<div class="content">
    <div class="left">left</div>
    <div class="main">main</div>
    <div class="right">right</div>
</div>
<div class="footer">footer</div>
<style>
    .parent{
        min-height: 400px;
        background: #efafff;
        display: flex;
        justify-content: center;
        /*主轴居中*/
        align-items: center;
        /*侧轴居中*/
    }
    .child{
        border: 1px solid red;
    }
</style>

<div class="parent">
    <div class="child">
        jdashfjasdfah覅好僵啊时代峰峻爱世界净空法师大恒科技凤凰健康设计费
    </div>
</div>
上一篇 下一篇

猜你喜欢

热点阅读