Flex布局学习

1.Flex学习

1.微信布局传统

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>微信布局传统title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        main{
            height: 100vh;
            background: #ddd;
        }
        footer{
            position: fixed;
            width: 100vw;
            bottom: 0;
            height: 140px;
            background:linear-gradient(to bottom,
            #f3f3f3,#ddd,#f3f3f3);
            border-top: solid 1px #ddd;
        }
        footer div{
            line-height: 140px;
            float: left;
            width: 33%;
            height: 100%;
            /*background: black;*/
            text-align: center;
            color: #555;
            font-size: 40px;
        }
        footer div:nth-child(n+2){
            border-left: solid 1px #ccc;

        }

    style>
<body>
<main>
    <footer>
        <div>消息div>
        <div>发现div>
        <div>div>
    footer>

main>
body>
html>

2.微信布置flex

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>微信布置flextitle>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        body{
            height: 100vh;
            display: flex;
            flex-direction: column;
        }
        main{
            background: #ddd;
            flex:1;
        }
        footer{
            background:linear-gradient(to bottom,
            #f3f3f3,#ddd,#f3f3f3);
            height: 140px;
            display: flex;
            justify-content: space-evenly;

        }
        footer div{
            flex:1;
            text-align: center;
            font-size: 40px;
            line-height: 140px;
            color: #555;

        }
        footer div:nth-child(n+2){
            border-left: solid 1px #ccc;
        }
    style>
head>
<body>
<main>111main>
    <footer>
        <div>消息div>
        <div>通讯录div>
        <div>发现div>
        <div>div>
    footer>



body>
html>

3.声明弹性盒子的几种方式

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Titletitle>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        footer{
            display: flex;
            /*
            应用属性之后变成弹性盒子
            */
            /*display: inline-flex;*/
            background: black;
        }
    style>
head>
<body>
<footer>
    <div>simondiv>
    <div>simondiv>
    <div>simondiv>
footer>
body>
html>

4.改变主轴线方向

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Titletitle>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        article{
            border: solid 5px blueviolet;
            width: 400px;
            /*
            当加入弹性布局时元素默认是水平从左排列
            */
            display: flex;
            /*
            改变方向
            flex-direction: row-reverse
            从右
            到垂直默认从上往下
            flex-direction: column;
            垂直默认从下往上:
            flex-direction: column-reverse;
            */
            /*flex-direction: row-reverse;*/
            flex-direction: column;
        }
        article div{
            width: 100px;
            height: 100px;
            background: red;
            margin: 10px;
        }
        body{
            padding-left: 100px;
            padding-top: 100px;
        }
    style>
head>
<body>
    <article>
        <div>div>
        <div>div>
        <div>div>
    article>
body>
html>

5.控制弹性元素溢出换行处理

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Titletitle>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        article{
            border: solid 5px blueviolet;
            width: 400px;
            /*
            当加入弹性布局时元素默认是水平从左排列
            */
            display: flex;
            /*
            改变方向
            flex-direction: row-reverse
            从右
            到垂直默认从上往下
            flex-direction: column;
            垂直默认从下往上:
            flex-direction: column-reverse;
            */
            /*flex-direction: row-reverse;*/
            flex-direction: column;
        }
        article div{
            width: 100px;
            height: 100px;
            background: red;
            margin: 10px;
        }
        body{
            padding-left: 100px;
            padding-top: 100px;
        }
    style>
head>
<body>
    <article>
        <div>div>
        <div>div>
        <div>div>
    article>
body>
html>

6.主轴与交叉轴及其排列方式

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Titletitle>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        article{
            border: solid 5px silver ;
            width: 550px;
            height: 280px;
            display: flex;
            flex-flow: wrap row;
            /*
            row:
            目前是水平主轴,这个意思是从主轴开始进行排列
            justify-content: flex-start;
            下面是主轴从结束开始排
            column:
            目前是垂直主轴
            justify-content: center(还能居中);
            space-between
            这个就是左右两边会靠到边上 中间的会平均进行分布
            space-around
            这个就是每个元素之间的间隔都是一个元素的长度
            space-evenly
            这个就是每一个元素平均分布
            */
            justify-content: center;
            /*
            下面这个是交叉轴
            row:
            垂直的是交叉轴
            align-items: flex-end;(交叉轴终点也就是从下往上)
            align-items: stretch; 拉伸(根据就近有height失效)
            拉伸一定要注意高度和宽度设置(设置了会无效)
            */
            align-items: center;
        }
        article div{

            width: 100px;
            height: 100px;
            background: blueviolet;
            padding: 10px;
            box-sizing: border-box;
            /*background-clip: content-box;*/
            color: white;
        }
        body{
            padding-left: 100px;
            padding-top: 100px;
        }
    style>
head>
<body>
<article>
    <div>1div>
    <div>2div>
    <div>3div>
article>
body>
html>

7.多行主轴和交叉轴及其排列方式

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Titletitle>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        article{
            border: solid 5px silver ;
            width: 550px;
            height: 280px;
            display: flex;
            flex-flow: wrap row;
            /*
            row:
            目前是水平主轴,这个意思是从主轴开始进行排列
            justify-content: flex-start;
            下面是主轴从结束开始排
            column:
            目前是垂直主轴
            justify-content: center(还能居中);
            space-between
            这个就是左右两边会靠到边上 中间的会平均进行分布
            space-around
            这个就是每个元素之间的间隔都是一个元素的长度
            space-evenly
            这个就是每一个元素平均分布
            */
            justify-content: center;
            /*
            下面这个是交叉轴
            row:
            垂直的是交叉轴
            align-items: flex-end;(交叉轴终点也就是从下往上)
            align-items: stretch; 拉伸(根据就近有height失效)
            拉伸一定要注意高度和宽度设置(设置了会无效)
            */
            align-items: center;
        }
        article div{

            width: 100px;
            height: 100px;
            background: blueviolet;
            padding: 10px;
            box-sizing: border-box;
            /*background-clip: content-box;*/
            color: white;
        }
        body{
            padding-left: 100px;
            padding-top: 100px;
        }
    style>
head>
<body>
<article>
    <div>1div>
    <div>2div>
    <div>3div>
article>
body>
html>

8.元素可用容器分配

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Titletitle>
head>
<style type="text/css">
    *{
        margin: 0;
        padding: 0;
    }
    body{
        padding-left: 50px;
        padding-top: 15px;
    }
    article{
        border: #555555 solid 5px;
        width: 550px;
        height: 100px;
        display: flex;
        flex-flow: wrap row;

    }
    article div:nth-child(1){
        flex-grow: 0;
    }

    article div:nth-child(2){
        flex-grow: 1;
    }

    article div:nth-child(3){
        flex-grow: 3;
    }


    article div{
        /*
        flex-grow:1
        每个元素平均分配剩下部分的一等分
        举个例子这里是550去掉300
        还有250
        第一个不分(100)
        第二个分(100+250/4*1)
        第三个分(100+250/4*3)
        */
        flex-grow: 1;
        width: 100px;
        height: 100px;
        background-color: blueviolet;
        background-clip: content-box;
        padding: 10px;
        border: solid 1px  blueviolet;
        box-sizing: border-box;
        font-size: 35px;
        color: white;
    }


style>
<body>
<article>
    <div>1div>
    <div>2div>
    <div>3div>
article>
body>
html>

9.小米结构demo

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Titletitle>

    <style>
        *{
            margin: 0;
            padding: 0;
        }
        header{
            height: 60px;
            background: blueviolet;
        }
        main{
            flex-grow: 1;
            background: #ccc;

        }
        footer{
            height: 60px;
            background: #383881;
        }
        body{
            height: 100vh;
            display: flex;
            flex-flow: column;
            justify-content: space-between;
        }
    style>
head>

<body>
<header>

header>
<main>

main>

<footer>

footer>
body>
html>

10.元素动态缩小技巧

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Titletitle>
head>
<style type="text/css">
    *{
        margin: 0;
        padding: 0;
    }
    body{
        padding-left: 50px;
        padding-top: 15px;
    }
    article{
        border: #555555 solid 5px;
        width: 400px;
        height: 100px;
        display: flex;
        flex-flow: row;

    }
    article div:nth-child(1){
        /*
        空间不够时我们写的缩小比例
        flex-shrink

        */
        flex-shrink: 0;
    }

    article div:nth-child(2){
        flex-shrink: 1;
    }

    article div:nth-child(3){
        /*
        缩小比例为
        200/(200*1+200*3)*3 = 0.75
        所以缩小 200*0.75 = 150px
        */
        flex-shrink: 3;
    }


    article div{
        /*
        flex-grow:1
        每个元素平均分配剩下部分的一等分
        举个例子这里是550去掉300
        还有250
        第一个不分(100)
        第二个分(100+250/4*1)
        第三个分(100+250/4*3)
        */
        width: 200px;
        height: 100px;
        background-color: blueviolet;
        background-clip: content-box;
        padding: 10px;
        border: solid 1px  blueviolet;
        box-sizing: border-box;
        font-size: 35px;
        color: white;
    }


style>
<body>
<article>
    <div>1div>
    <div>2div>
    <div>3div>
article>
body>
html>

11.主轴基本尺寸

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Titletitle>
head>
<style type="text/css">
    *{
        margin: 0;
        padding: 0;
    }
    body{
        padding-left: 50px;
        padding-top: 15px;
    }
    article{
        border: #555555 solid 5px;
        width: 550px;
        height: 100px;
        display: flex;
        flex-flow: wrap row;

    }


    article div{
        /*
        主轴的基本尺寸(元素必须在主轴占到的区域)
        flex-basis
        若有基准尺寸则不按主轴方向的px来算
        max(min) weight(height)> basic > weight(height)
         */
        flex-basis: 100px;
        /*width: 100px;*/
        height: 100px;
        background-color: blueviolet;
        background-clip: content-box;
        padding: 10px;
        border: solid 1px  blueviolet;
        box-sizing: border-box;
        font-size: 35px;
        color: white;
    }


style>
<body>
<article>
    <div>1div>
    <div>2div>
    <div>3div>
article>
body>
html>

12.弹性元素的组合定义

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Titletitle>
head>
<style type="text/css">
    *{
        margin: 0;
        padding: 0;
    }
    body{
        padding-left: 50px;
        padding-top: 15px;
    }
    article{
        border: #555555 solid 5px;
        width: 400px;
        height: 100px;
        display: flex;
        flex-flow: row;

    }


    article div{
        /*
        举个例子若是这样定义
        flex-grow: 1;
        flex-shrink: 2;
        flex-basis: 100px;
        等于下面的
        */
        flex:0 0 200px;
        /*width: 100px;*/
        height: 100px;
        background-color: blueviolet;
        background-clip: content-box;
        padding: 10px;
        border: solid 1px  blueviolet;
        box-sizing: border-box;
        font-size: 35px;
        color: white;
    }

    article div:nth-child(1){
        /*
        必须写一个(最好都写)
        */
        flex: 1 1 200px;
    }
    article div:nth-child(2){

        flex: 1 3 200px;
    }
    article div:nth-child(3){
        flex: 0 0 200px;
    }




style>
<body>
<article>
    <div>1div>
    <div>2div>
    <div>3div>
article>
body>
html>

13.改变元素的顺序

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Titletitle>
head>
<style>
    *{
        padding-left: 50px;
        padding-top: 15px;
    }
    article{
        border: solid 5px silver;
        width: 400px;
        height: 500px;
        padding: 10px;
        display: flex;
        flex-flow: column;
        justify-content: space-between;
    }
    article div{
        background: blueviolet;
        background-clip: content-box;
        padding: 10px;
        flex: 1;
        font-size: 55px;
        color: white;
    }
    /*
    值越小越在上面
    */

    article :nth-child(1){
        order: 1;
    }

    article :nth-child(2){
        order: -1;
    }

    article :nth-child(3){
        order: -11;
    }

style>
<body>
<article>
    <div>1div>
    <div>2div>
    <div>3div>
article>
body>
html>

14.改变元素顺序的实例

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Titletitle>
head>
<style>
    *{
        margin: 0;
        padding: 0;
    }
    article{
        flex: 1;
        border: solid 5px silver;
        width: 400px;
        height: 500px;
        padding: 10px;
        display: flex;
        flex-direction: column;
    }
    article section{
        flex: 1 0 100px;
        background: blueviolet;
        background-clip: content-box;
        padding: 10px;
        text-align: center;
        display: flex;
        flex-direction: column;
    }
    article section span{
        padding: 10px;
        background: black;
        cursor: pointer;
        color: #fff;

    }
    article section div{
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: center;
        font-size: 35px;
    }
style>
<body>
<article>
    <section>
       <div>simon1.comdiv>
        <span onclick="up(this)">upspan>
    section>

    <section>
        <div>simon2.comdiv>
        <span onclick="up(this)">upspan>
    section>

    <section>
        <div>simon3.comdiv>
        <span onclick="up(this)">upspan>
    section>
article>
<script>
    function up(el){
        let order = getComputedStyle(el.parentElement,null).order
        el.parentElement.style.order = order * 1- 1;
    }
script>

body>
html>

15.弹性布局操作文本结点

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Titletitle>
head>
<style>
  article{
    border: solid 5px silver;
    width: 400px;
    height: 500px;
    padding: 10px;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
style>
<body>

<article>
  simonup
<div>
  simonmid
div>
  simondown
article>
body>
html>

16.定位元素在弹性布局中的表现

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Titletitle>
head>
<style>
    *{
        margin: 0;
        padding: 0;
    }
    article{
        border: solid 5px silver;
        width: 400px;
        height: 500px;
        padding: 10px;
        display: flex;
        flex-flow: column;
        justify-content: space-between;
        position: relative;
    }
    article *{
        flex: 1;
        background: blueviolet;
        background-clip: content-box;
        padding: 10px;
        font-size: 35px;
        color: white;
    }
    article :nth-child(1){
        /*
        绝对定位在弹性布局原来的空间位置不保留
        */
        /*position: absolute;*/
        /*background-color: black;*/
        /*left: 100px;*/
        /*top: 200px;*/
        /*
        相对定位会保留
        */
        position: relative;
        background-color: black;
        left: 100px;
        top: 200px;
    }
style>
<body>
<article>
    <div>1div>
    <div>2div>
    <div>3div>
article>
body>
html>

17.微信弹出界面demo

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Titletitle>
    <style>
        *{
            margin: 0;
            padding: 0;
        }

        body{
            height: 100vh;
            display: flex;
            flex-flow: column;
        }

        main{
            flex: 1;
            background: #f3f3f3;
        }
        footer{
            height: 50px;
            background: #eee;
            display: flex;
            justify-content: space-between;
            flex-flow: row;
            border-top: solid 1px #ccc;
        }
        footer section{
            flex: 1;
            border-right: solid 1px #ccc;
            display: flex;
            flex-flow:column-reverse;
        }

        footer section:last-child{
            border-right: none;
        }
        footer section h4{
            flex: 0 0 50px;
            display: flex;
            flex-flow: column;
            justify-content: center;
            text-align: center;
            cursor: pointer;
        }
        footer section ul{
            display: flex;
            flex-flow: column;
            border:solid 1px #ccc;
            margin: 5px;
            border-radius: 10px;
            text-align: center;

        }
        footer section ul li{

            flex: 1 0 50px;
            display: flex;
            flex-flow: column;
            justify-content: center;
            border-bottom: solid 1px #ccc;
            cursor: pointer;
        }
        footer section ul li:last-child{
            border-bottom: none;
        }

    style>
head>
<body>
<main>

main>
<footer>
    <section>
        <h4>教程h4>
        <ul>
            <li>
                PHP
            li>
            <li>
                CSS
            li>
        ul>
    section>
    <section>
        <h4>直播h4>
    section>
    <section>
        <h4>软件h4>
    section>
footer>
body>
html>

18.margin自动撑满小技巧

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Titletitle>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        nav{
            margin: 0 auto;
            width: 1000px;
            height: 60px;
            background: #f3f3f3;
            box-shadow: 0 0 0 rgba(0,0,0,.3);
            display: flex;
            justify-content: space-between;
        }
        ul{
            list-style: none;
            display: flex;
            align-items: center;
        }
        ul:first-child{
            /*
            到最左边直接
            */
            /*margin-right: auto;
            这个就是小技巧
            这个非常好用! 直接将两个ul左右分开了
            一个居左一个居右,暂时不懂原理
            但是想到了其他方法实现
            用主轴线方向的弹性元素方向

            直接用菜单栏的主轴多行元素设置space-between即可
            */
            display: flex;
        }

        ul:first-child > li{
            margin: 0 10px;
        }

        ul:nth-child(2) > li{
            margin: 0 10px;
            width: 50px;
            height: 50px;
            background: #9b59b6;
            border-radius: 50%;
        }

    style>
head>
<body>
<nav>
    <ul>
        <li>首页li>
        <li>视频教程li>
        <li>晚八点直播li>
    ul>
    <ul>
        <li>li>
        <li>li>
        <li>li>
    ul>
nav>
body>
html>

你可能感兴趣的:(学习,css,前端)