弹性盒子模型(1)

本文详细介绍了CSS3中的弹性盒子模型,包括布局模式、基本属性和容器属性,如flex-direction、flex-wrap、flex-flow、justify-content、align-items以及align-content等,帮助理解如何在不同场景下使用这些属性实现灵活的布局效果。

弹性盒子模型

一、布局模式:

通过之前的学习我们已经学习了两种布局模式,分别是Tbael布局和div+css的盒子模型,而除了这两种布局模式,还有一种布局模式就是弹性盒子模型布局,而弹性盒子模型( Flexible Box)则是CSS3新增的一种布局方式,是一种新的用于在HTML页面实现布局的方式。使得当HTML页面适应不同尺寸的屏幕和不同的设备时,元素是可预测地运行。弹性盒子模型依旧使用div,但需要将div的display属性变为flex。

二、基本属性

1、容器(flex container):是一个块级标签(可以包含其他的页面元素)
2、项目(flex item):又称为子项(包含在容器的元素)
3、排列方向(direction):元素的布局方向

三、容器属性

1、flex-direction属性

flex-direction属性是用来定义容器中的布局方式也可以说是排列方式的一种属性,它分别有4种取值:

flex-direction属性取值作用
row(默认值)排列的主轴方向为水平排列,从左到右的方式排列
row-reverse排列的主轴方向为水平,从右往左的方式排列
columm主轴方向为垂直方向,从上到下排列
column-reverse主轴方向也为垂直方向,从下到上排列

flex-direction四种取值已经在表格中列出来了,接下来就一一用代码展示出来

1.1、flex-direction:row

代码展示

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>flex-direction</title>
</head>
<style>
.one,.two,.three{
    width: 200px;
    height: 200px;
    background-color: red;
    margin: 20px;
    color: black;
    line-height: 200px;
    text-align: center;
}
.d1{
    display: flex;
    flex-direction: row;
}
</style>
<body>
    <div class="d1">
        <div class="one">1</div>
        <div class="two">2</div>
        <div class="three">3</div>
    </div>
    
</body>
</html>

效果如下
在这里插入图片描述

可以看到在将div的display属性改为flex以及使用了flex-direction属性之后,我们在并没有使用浮动标签的情况下,使三个div水平自左向右排列。

1.2、flex-direction:row-reverse

代码展示

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>flex-direction</title>
</head>
<style>
.one,.two,.three{
    width: 200px;
    height: 200px;
    background-color: red;
    margin: 20px;
    color: black;
    line-height: 200px;
    text-align: center;
}
.d1{
    display: flex;
    flex-direction: row-reverse;
}
</style>
<body>
    <div class="d1">
        <div class="one">1</div>
        <div class="two">2</div>
        <div class="three">3</div>
    </div>
    
</body>
</html>

效果如下:

在这里插入图片描述

可以看到在设置为row-reverse后,排列主轴依旧为水平排列,不过变为了自右向左。

1.3、flex-direction:columm

代码展示

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>flex-direction</title>
</head>
<style>
.one,.two,.three{
    width: 200px;
    height: 200px;
    background-color: red;
    margin: 20px;
    color: black;
    line-height: 200px;
    text-align: center;
}
.d1{
    display: flex;
    flex-direction: column;
}
</style>
<body>
    <div class="d1">
        <div class="one">1</div>
        <div class="two">2</div>
        <div class="three">3</div>
    </div>
    
</body>
</html>

效果如下:
在这里插入图片描述

可以看到在设置为column属性后,排列主轴变为垂直排列,排列顺序也变成了自上而下。

1.4、flex-direction:columm-reverse

代码展示

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>flex-direction</title>
</head>
<style>
.one,.two,.three{
    width: 200px;
    height: 200px;
    background-color: red;
    margin: 20px;
    color: black;
    line-height: 200px;
    text-align: center;
}
.d1{
    display: flex;
    flex-direction: column-reverse;
}
</style>
<body>
    <div class="d1">
        <div class="one">1</div>
        <div class="two">2</div>
        <div class="three">3</div>
    </div>
    
</body>
</html>

在这里插入图片描述

可以看到在设置为column-reverse属性后,排列主轴依旧为垂直排列,排列顺序则变成了自下而上。

2、flex-wrap:

flex-wrap属性是定义容器内的元素是否换行,有三种取值:

flex-wrap属性取值作用
nowrap默认值,不换行
wrap换行,第一行在上方
wrap-reverse换行,第一行在下方

接下来我们继续一一用代码来将这些取值展示出来

2.1、wrap

代码展示

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>flex-wrap</title>
</head>
<style>
.one,.two,.three{
    width: 200px;
    height: 200px;
    background-color: red;
    margin: 20px;
    color: black;
    line-height: 200px;
    text-align: center;
}
.d1{
    width: 500px;
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
}
</style>
<body>
    <div class="d1">
        <div class="one">1</div>
        <div class="two">2</div>
        <div class="three">3</div>
    </div>
    
</body>
</html>

效果展示
在这里插入图片描述

当给div设置了宽度之后,当宽度不足以容纳下三个元素时,这时就使用flex-wrap:warp,就可以自动换到下一行

2.2、flex-wrap:wrap-reverse

代码展示

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>flex-wrap</title>
</head>
<style>
.one,.two,.three{
    width: 200px;
    height: 200px;
    background-color: red;
    margin: 20px;
    color: black;
    line-height: 200px;
    text-align: center;
}
.d1{
    width: 500px;
    display: flex;
    flex-direction: row;
    flex-wrap: wrap-reverse;
}
</style>
<body>
    <div class="d1">
        <div class="one">1</div>
        <div class="two">2</div>
        <div class="three">3</div>
    </div>
    
</body>
</html>

在这里插入图片描述

使用了wrap-reverse这个取值之后,换行顺序就会变为在第一行下方,顺序会颠倒过来。

3、flex-flow属性

flex-flow属性是flex-direction和flex-wrap的简写,默认值为row nowrap

代码展示

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>flex-direction</title>
</head>
<style>
.one,.two,.three{
    width: 200px;
    height: 200px;
    background-color: red;
    margin: 20px;
    color: black;
    line-height: 200px;
    text-align: center;
}
.d1{
    width: 500px;
    display: flex;
    flex-direction: row;
    flex-flow: row-reverse wrap;
}
</style>
<body>
    <div class="d1">
        <div class="one">1</div>
        <div class="two">2</div>
        <div class="three">3</div>
    </div>
    
</body>
</html>

在这里插入图片描述

flex-flow属性就是将flex-direction和flex-wrap结合成一种属性,这样做的最大好处就是可以使代码看起开非常的简洁。

4、justify-content

justify-content 属性定义了浏览器之间,如何分配顺着弹性容器主轴(或者网格行轴) 的元素之间及其周围的空间。

justify-content 属性取值取值
flex-start左对齐(默认) 。注意:表示容器的开始
space-between两端对齐,子项之间的宽度是相等的
flex-end右对齐(表示容器的结尾)
center居中
space-around每个项目(子项)两侧的间距相等

justify-content属性的常用取值一共有以上4个,接下来我们继续一一用代码来将这些取值展示出来

4.1、space-between

代码展示

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>justify-content</title>
</head>
<style>
    .one,.two,.three{
        width: 200px;
        height: 200px;
        margin: 20px;
        background-color: cyan;
        color: black;
        text-align: center;
        line-height: 200px;
    }
    .d1{
        width: 800px;
        border: solid 1px black;
        display: flex;
        justify-content: space-between;
    }
</style>
<body>
    <div class="d1">
        <div class="one">1</div>
        <div class="two">2</div>
        <div class="three">3</div>
    </div>
</body>
</html>

效果如下

在这里插入图片描述

space-between是两端和边框对齐,子项之间保持距离一致。

4.2、flex-end

代码展示

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>justify-content</title>
</head>
<style>
    .one,.two,.three{
        width: 200px;
        height: 200px;
        margin: 20px;
        background-color: cyan;
        color: black;
        text-align: center;
        line-height: 200px;
    }
    .d1{
        width: 800px;
        border: solid 1px black;
        display: flex;
        justify-content: flex-end;
    }
</style>
<body>
    <div class="d1">
        <div class="one">1</div>
        <div class="two">2</div>
        <div class="three">3</div>
    </div>
</body>
</html>

效果如下
在这里插入图片描述

flex-end取值是指元素从行尾末端位置开始排列,而且和flex-direction:row-reverse不同,flex-end是顺序不变,只是将元素从行尾位置开始排列,而row-reverse使用后元素顺序会变化,最先的元素会到末端,因为row-reverse是自右向左排列,flex-end则只是所以需要注意两者的区别。

4.3、center
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>justify-content</title>
</head>
<style>
    .one,.two,.three{
        width: 200px;
        height: 200px;
        margin: 20px;
        background-color: cyan;
        color: black;
        text-align: center;
        line-height: 200px;
    }
    .d1{
        width: 800px;
        border: solid 1px black;
        display: flex;
        justify-content: center;
    }
</style>
<body>
    <div class="d1">
        <div class="one">1</div>
        <div class="two">2</div>
        <div class="three">3</div>
    </div>
</body>
</html>

效果如下

在这里插入图片描述

center这个取值是我们的老熟人了,基本上使用它就是为了使元素居中,就不过多介绍了。

4.4、space-around

代码展示

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>justify-content</title>
</head>
<style>
    .one,.two,.three{
        width: 200px;
        height: 200px;
        margin: 20px;
        background-color: cyan;
        color: black;
        text-align: center;
        line-height: 200px;
    }
    .d1{
        width: 800px;
        border: solid 1px black;
        display: flex;
        justify-content: space-around;
    }
</style>
<body>
    <div class="d1">
        <div class="one">1</div>
        <div class="two">2</div>
        <div class="three">3</div>
    </div>
</body>
</html>

效果展示
在这里插入图片描述

space-around是使每个项目之间的间距都保持一致

5、align-items:

align-items 属性定义flex子项在flex容器的当前行的侧轴(纵轴)方向上的对齐方式。

align-items 属性取值作用
flex-start交叉轴的起点对齐
flex-end交叉轴的终点对齐
center交叉轴的中点对齐
baseline项目的第一行文字的基线对齐
stretch(默认值)如果项目未设置高度或设为auto,将占满整个容器的高度。
5.1、flex-start

代码展示

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
      .d1{
          width: 800px;
          height: auto;
          background-color:cyan;
          display: flex;
          flex-direction: row;
          align-items:flex-start;
      }
      .one{
          width: 100px;
          height: 80px;
          background-color: brown;
          color: black;
          text-align: center;
          line-height: 80px;
          margin: 20px;
      }
      .two{
          width: 80px;
          height: 100px;
          background-color: blue;
          color: black;
          text-align: center;
          line-height: 80px;
          margin: 20px;
      }
      .three{
          width: 150px;
          height: 100px;
          background-color: darkgoldenrod;
          color: black;
          text-align: center;
          line-height: 100px;
          margin: 20px;
      }
      .four{
          width: 100px;
          height: 150px;
          background-color: green;
          color: black;
          text-align: center;
          line-height: 150px;
          margin: 20px;
      }
</style>
<body>
    <div class="d1">
        <div class="one">1</div>
        <div class="two">2</div>
        <div class="three">3</div>
        <div class="four">4</div>
    </div>
</body>
</html>

在这里插入图片描述

flex-start是以交叉轴的起点对齐

5.2、flex-end

代码展示

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
      .d1{
          width: 800px;
          height: auto;
          background-color:cyan;
          display: flex;
          flex-direction: row;
          align-items:flex-end;
      }
      .one{
          width: 100px;
          height: 80px;
          background-color: brown;
          color: black;
          text-align: center;
          line-height: 80px;
          margin: 20px;
      }
      .two{
          width: 80px;
          height: 100px;
          background-color: blue;
          color: black;
          text-align: center;
          line-height: 80px;
          margin: 20px;
      }
      .three{
          width: 150px;
          height: 100px;
          background-color: darkgoldenrod;
          color: black;
          text-align: center;
          line-height: 100px;
          margin: 20px;
      }
      .four{
          width: 100px;
          height: 150px;
          background-color: green;
          color: black;
          text-align: center;
          line-height: 150px;
          margin: 20px;
      }
</style>
<body>
    <div class="d1">
        <div class="one">1</div>
        <div class="two">2</div>
        <div class="three">3</div>
        <div class="four">4</div>
    </div>
</body>
</html>

在这里插入图片描述

flex-end是以交叉轴的终点对齐

5.3、center
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
      .d1{
          width: 800px;
          height: auto;
          background-color:cyan;
          display: flex;
          flex-direction: row;
          align-items:center;
      }
      .one{
          width: 100px;
          height: 80px;
          background-color: brown;
          color: black;
          text-align: center;
          line-height: 80px;
          margin: 20px;
      }
      .two{
          width: 80px;
          height: 100px;
          background-color: blue;
          color: black;
          text-align: center;
          line-height: 80px;
          margin: 20px;
      }
      .three{
          width: 150px;
          height: 100px;
          background-color: darkgoldenrod;
          color: black;
          text-align: center;
          line-height: 100px;
          margin: 20px;
      }
      .four{
          width: 100px;
          height: 150px;
          background-color: green;
          color: black;
          text-align: center;
          line-height: 150px;
          margin: 20px;
      }
</style>
<body>
    <div class="d1">
        <div class="one">1</div>
        <div class="two">2</div>
        <div class="three">3</div>
        <div class="four">4</div>
    </div>
</body>
</html>

在这里插入图片描述

5.4、baseline
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
      .d1{
          width: 800px;
          height: auto;
          background-color:cyan;
          display: flex;
          flex-direction: row;
          align-items:baseline;
      }
      .one{
          width: 100px;
          height: 80px;
          background-color: brown;
          color: black;
          text-align: center;
          line-height: 80px;
          margin: 20px;
      }
      .two{
          width: 80px;
          height: 100px;
          background-color: blue;
          color: black;
          text-align: center;
          line-height: 80px;
          margin: 20px;
      }
      .three{
          width: 150px;
          height: 100px;
          background-color: darkgoldenrod;
          color: black;
          text-align: center;
          line-height: 100px;
          margin: 20px;
      }
      .four{
          width: 100px;
          height: 150px;
          background-color: green;
          color: black;
          text-align: center;
          line-height: 150px;
          margin: 20px;
      }
</style>
<body>
    <div class="d1">
        <div class="one">1</div>
        <div class="two">2</div>
        <div class="three">3</div>
        <div class="four">4</div>
    </div>
</body>
</html>

效果展示
在这里插入图片描述

在这里可以看到我虽然设置了baseline,但好像和center一样,这是因为baseline是以项目第一行文字为基线对齐,我的字体设置的是居中,所以效果看起来和center效果一样

5.5、stretch

代码展示

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
      .d1{
          width: 800px;
          height: 500px;
          background-color:cyan;
          display: flex;
          flex-direction: row;
          align-items:stretch;
      }
      .one{
          width: 100px;
          
          background-color: brown;
          color: black;
          text-align: center;
          line-height: 80px;
          margin: 20px;
      }
      .two{
          width: 80px;
         
          background-color: blue;
          color: black;
          text-align: center;
          line-height: 80px;
          margin: 20px;
      }
      .three{
          width: 150px;
          
          background-color: darkgoldenrod;
          color: black;
          text-align: center;
          line-height: 100px;
          margin: 20px;
      }
      .four{
          width: 100px;
          
          background-color: green;
          color: black;
          text-align: center;
          line-height: 150px;
          margin: 20px;
      }
</style>
<body>
    <div class="d1">
        <div class="one">1</div>
        <div class="two">2</div>
        <div class="three">3</div>
        <div class="four">4</div>
    </div>
</body>
</html>

在这里插入图片描述

stretch是align-items的默认值。元素被拉伸以适应容器。

6、align-content

align-content 属性是多根轴线的对齐方式,并且只有在设置多根轴线时才会有效,在单根轴线时属性无效。

align-content取值作用
flex-start与交叉轴的起点对齐
flex-end与交叉轴的终点对齐
center与交叉轴的中点对齐
space-between与交叉轴两端对齐,轴线之间的间隔平均分布
space-around每根轴线两侧的间隔都相等。所以,轴线之间的间隔比轴线与边框的间隔大一倍。
stretch(默认值)轴线占满整个交叉轴。

align-content属性的取值一共有6个,他的设置方式和align-items相似,只不过它是定义多跟轴线的对齐方式,接下来我们举个例子来展示他们的样子。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
      .d1{
          width: 800px;
          height: 800px;
          background-color:cyan;
          display: flex;
          flex-flow: row wrap;
          align-content:center;
      }
      .one{
          width: 100px;
          height: 80px;
          background-color: brown;
          color: black;
          text-align: center;
          line-height: 80px;
          margin: 20px;
      }
      .two{
          width: 80px;
         height: 100px;
          background-color: blue;
          color: black;
          text-align: center;
          line-height: 100px;
          margin: 20px;
      }
      .three{
          width: 150px;
          height: 100px;
          background-color: darkgoldenrod;
          color: black;
          text-align: center;
          line-height: 100px;
          margin: 20px;
      }
      .four{
          width: 100px;
          height: 150px;
          background-color: green;
          color: black;
          text-align: center;
          line-height: 150px;
          margin: 20px;
      }
</style>
<body>
    <div class="d1">
        <div class="one">1</div>
        <div class="two">2</div>
        <div class="three">3</div>
        <div class="four">4</div>
    </div>
</body>
</html>

在这里插入图片描述

center元素位于容器的中心。各行向弹性盒容器的中间位置堆叠。各行两两紧靠住同时在弹性盒容器中居中对齐,保持弹性盒容器的侧轴起始内容边界和第一行之间的距离与该容器的侧轴结束内容边界与第最后一行之间的距离相等。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值