CSS

flex布局

2018-07-24  本文已影响0人  前端葱叶

By Leaf



在前端的学习过程中,布局可以说是非常重要的,之前一直用DIV和CSS布局,但是深受定位、浮动等属性的困扰。后来学习了flex布局才发现,只要熟悉flex布局,大部分复杂的网页都可以用flex布局实现。这里主要是将之前学习的flex知识加以复习和巩固,便于自己以后忘记查阅。

一、简单理解flex布局

首先我们来理解一下flex布局的一些基本概念(图是我从网上找来的,我觉得对理解flex布局很有用):

image.png
任何一个容器都可以指定为flex布局

任何一个容器都可以指定为flex布局,任何容器耶必须先指定flex布局display:flex才能使用flex布局的所有属性。
flex的属性有:

接下来对每一个属性和属性值展开demo:

<!--demo,html代码-->
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>flex布局</title>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        .container div {
            width: 100px;
            height: 100px;
            border: 1px solid red;
        }
        .container{
            display: flex;
            flex-direction: row;
        }
    </style>
</head>

<body>
    <div class="container">
        <div>1</div>
        <div>2</div>
        <div>3</div>
        <div>4</div>
    </div>
</body>

</html>
  1. flex-direction 确定主轴方向

flex-direction: row | row-reverse | column | column-reverse

  .container{
            display: flex;
            flex-direction: row;
        }
image.png
  .container{
            display: flex;
            flex-direction: row-reverse;
        }
image.png
 .container{
            display: flex;
            flex-direction: column;
        }
image.png
 .container{
            display: flex;
            flex-direction: column-reverse;
        }
image.png
  1. flex-wrap 允许项目是否换行

flex-wrap:nowrap | wrap | wrap-reverse;

  .container{
            width: 300px;
            border: 1px solid black;
            display: flex;
            flex-direction: row;
            flex-wrap: nowrap;
        }
image.png
  .container{
            width: 300px;
            border: 1px solid black;
            display: flex;
            flex-direction: row;
            flex-wrap: wrap;
        }
image.png
  1. justify-content确定项目在主轴方向的对齐方式

justify-content:flex-start | flex-end | center | space-between | space-around

 .container{
            display: flex;
            flex-direction: row;
            flex-wrap: wrap;
            justify-content: flex-start;
        }
image.png
 .container{
            display: flex;
            flex-direction: row;
            flex-wrap: wrap;
            justify-content: flex-end;
        }
image.png
 .container{
            display: flex;
            flex-direction: row;
            flex-wrap: wrap;
            justify-content: center;
        }
image.png
 .container{
            display: flex;
            flex-direction: row;
            flex-wrap: wrap;
            justify-content: space-between;
        }
image.png
 .container{
            display: flex;
            flex-direction: row;
            flex-wrap: wrap;
            justify-content: space-around;
        }
image.png
 .container{
            display: flex;
            flex-direction: row;
            flex-wrap: wrap;
            justify-content: space-evenly;
        }
image.png

综上所诉,
justify-content确定项目在主轴方向的对齐方式的大图如下(图是我在学习的过程中在网上看到的,这个图一目了然,很容易理解):

image.png
  1. align-items确定项目在交叉轴方向的对齐方式。

align-items:flex-start | flex-end | center | baseline | stretch;

为了效果明显,我们要修改div的样式,给每一个div设置分别高度20px、40px、60px、80px。

 .container div {
            width: 100px;
        }

        .container div:nth-of-type(1) {
            height: 20px;
            border: 1px solid red;
        }
        .container div:nth-of-type(2) {
            height: 40px;
            border: 1px solid blue;
        }
        .container div:nth-of-type(3) {
            height: 60px;
            border: 1px solid rgb(255, 0, 157);
        }
        .container div:nth-of-type(4) {
            height: 80px;
            border: 1px solid green;
        }

        .container {
            display: flex;
            flex-direction: row;
            flex-wrap: wrap;
            align-items: flex-start;
        }
image.png
        .container {
            display: flex;
            flex-direction: row;
            flex-wrap: wrap;
            align-items: flex-end;
        }
image.png
 .container {
            display: flex;
            flex-direction: row;
            flex-wrap: wrap;
            height: 300px;
            border: 1px solid black;
            align-items: center;
        }
image.png
 .container {
            display: flex;
            flex-direction: row;
            flex-wrap: wrap;
            height: 300px;
            border: 1px solid black;
            align-items: baseline;
        }
image.png
   .container div {
            width: 100px;
        }

        .container div:nth-of-type(1) {
            /* height: 20px; */
            border: 1px solid red;
        }

        .container div:nth-of-type(2) {
            /* height: 40px; */
            border: 1px solid blue;
        }

        .container div:nth-of-type(3) {
            /* height: 60px; */
            border: 1px solid rgb(255, 0, 157);
        }

        .container div:nth-of-type(4) {
            /* height: 80px; */
            border: 1px solid green;
        }

        .container {
            display: flex;
            flex-direction: row;
            flex-wrap: wrap;
            height: 300px;
            border: 1px solid black;
            align-items: stretch;
        }
image.png

综上所述:


image.png
  1. align-content确定了在交叉轴方向的对齐方式及额外空间分配,类似于主轴上justify-content的作用。

align-content: stretch | flex-start | flex-end | center | space-between | space-around ;

image.png
image.png

在这里献上几位大神对flex布局的讲解,因为有了他们的深入讲解,我才理解了flex布局,这里根据学到知识自己整合了一下,便于自己以后忘记查阅,若有错别之处,望大神之处,比心,感谢。
30分钟学会flex布局
flex-弹性布局原来如此简单
flex小技巧
flex快速上手小记
(图好像没放好,等我有空再整理)

上一篇 下一篇

猜你喜欢

热点阅读