css3弹性盒模型

介绍

弹性盒模型是css3的一种新的布局方式,是一种当前页面需要适应不同的屏幕大小及设备类型时确保拥有恰当的行为的布局方式。

![在这里插入图片描述]( https://img-blog.csdnimg.cn/b55293c700f0427fa22e9cc6f8208f92.png?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwMzU3NzY5,size_16,color_FFFFFF,t_70)

  • 主轴(main axis)
  • 交叉轴(cross axis)

注意:生命display:flex 默认水平排列

弹性盒模型使用(父元素)

1. 创建基本代码

<!DOCTYPE html>
<html>
<head>
	<title></title>
</head>
<style type="text/css">

</style>
<body>

   <div class="warp">
     <div class="box box-1">1</div>
     <div class="box box-2">2</div>
     <div class="box box-3">3</div>
   </div>


<style type="text/css">
  .warp{
  	height: 880px;
  	border: 20px solid antiquewhite;
  }

  .box{
  	height: 200px;
  	width: 200px;
  	color: white;
  	font-size: 40px;
  }
  .box-1{
  	background: #FAC863

  }
  .box-2{
    background:	#1B2B34
  }
  .box-3{
  	background: #2ecc71
  }
  
</style>


</body>

</html>

![在这里插入图片描述]( https://img-blog.csdnimg.cn/746c3bd2b0cb4edd970d948b5503f71e.png?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwMzU3NzY5,size_16,color_FFFFFF,t_70)

2. 定义弹性模型

.warp{
  	height: 880px;
  	border: 20px solid antiquewhite;
  	/*定义弹性模型*/
  	display: flex;
  }

![在这里插入图片描述]( https://img-blog.csdnimg.cn/035116224e514ed1a666217faa15bd5e.png?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwMzU3NzY5,size_16,color_FFFFFF,t_70)

发现元素内部默认为水平居中

3. 水平居中

.warp{
  	height: 880px;
  	border: 20px solid antiquewhite;
  	/*定义弹性模型*/
  	display: flex;
  	/*水平居中*/
    justify-content: center;
  }

![在这里插入图片描述]( https://img-blog.csdnimg.cn/2c5dc8da5364464084cf9b625c08c3ec.png?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwMzU3NzY5,size_16,color_FFFFFF,t_70)

4. 水平开始排列

 .warp{
  	height: 880px;
  	border: 20px solid antiquewhite;
  	/*定义弹性模型*/
  	display: flex;
  	/*水平开始排列*/
    justify-content: flex-start;
  }

![在这里插入图片描述]( https://img-blog.csdnimg.cn/55ba7c17def44b688f7fe62c657ce2e6.png?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwMzU3NzY5,size_16,color_FFFFFF,t_70)

5. 水平结尾排列

.warp{
  	height: 880px;
  	border: 20px solid antiquewhite;
  	/*定义弹性模型*/
  	display: flex;
  	/*水平结尾排列*/
    justify-content: flex-end;
  }

![在这里插入图片描述]( https://img-blog.csdnimg.cn/b986bf49e5394f7284295475f83beca9.png?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwMzU3NzY5,size_16,color_FFFFFF,t_70)

6. 水平分散排列(占用开头)

.warp{
  	height: 880px;
  	border: 20px solid antiquewhite;
  	/*定义弹性模型*/
  	display: flex;
  	/*水平平均排列*/
    justify-content: space-between;
  }

![在这里插入图片描述]( https://img-blog.csdnimg.cn/4ee064be8323462f9d69b466c1878cb8.png?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwMzU3NzY5,size_16,color_FFFFFF,t_70)

7. 水平分散排列(不占用开头,开头距离不均匀)

在.warp{
  	height: 880px;
  	border: 20px solid antiquewhite;
  	/*定义弹性模型*/
  	display: flex;
  	/*水平平均排列*/
    justify-content: space-around;
  }

![在这里插入图片描述]( https://img-blog.csdnimg.cn/ff401518967f4d729ee4621dd1c8e6d0.png?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwMzU3NzY5,size_16,color_FFFFFF,t_70)

8. 水平分散排列(不占用开头,开头距离均匀)

.warp{
  	height: 880px;
  	border: 20px solid antiquewhite;
  	/*定义弹性模型*/
  	display: flex;
  	/*水平平均排列*/
    justify-content: space-evenly;
  }

![在这里插入图片描述]( https://img-blog.csdnimg.cn/af0b7020cd9d4add8b3097bfd35bb81f.png?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwMzU3NzY5,size_16,color_FFFFFF,t_70)

9. 垂直

.warp{
  	height: 880px;
  	border: 20px solid antiquewhite;
  	/*定义弹性模型*/
  	display: flex;
  	/*垂直*/
    flex-direction: column;
  }

![在这里插入图片描述]( https://img-blog.csdnimg.cn/ea9a3e33d25d4517843fed18e795ff9e.png?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwMzU3NzY5,size_16,color_FFFFFF,t_70)

10. 垂直居中

.warp{
  	height: 880px;
  	border: 20px solid antiquewhite;
  	/*定义弹性模型*/
  	display: flex;
  	/*垂直排列*/
    flex-direction: column;
    /*居中*/
   justify-content: center; 

  }

![在这里插入图片描述]( https://img-blog.csdnimg.cn/1048517119d4441b8426b923d83c3a52.png?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwMzU3NzY5,size_16,color_FFFFFF,t_70)

11.垂直水平居中(常用)

.warp{
  	height: 880px;
  	border: 20px solid antiquewhite;
  	/*定义弹性模型*/
  	display: flex;
  	/*垂直排列*/
    flex-direction: column;
    /*居中*/
   justify-content: center; 
    /*水平居中*/
    align-items: center;
  }

![在这里插入图片描述]( https://img-blog.csdnimg.cn/1835d707e9d44f6da67adb0b1c833670.png?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwMzU3NzY5,size_16,color_FFFFFF,t_70)

12.水平垂直居中(常用)

.warp{
  	height: 880px;
  	border: 20px solid antiquewhite;
  	/*定义弹性模型*/
  	display: flex;
    /*垂直居中*/
   justify-content: center; 
    /*水平居中*/
    align-items: center;
  }

![在这里插入图片描述]( https://img-blog.csdnimg.cn/44a75decff1a43259e4d466134864b66.png?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwMzU3NzY5,size_16,color_FFFFFF,t_70)

弹性盒模型使用(子元素)

1.如何保证子元素在自适应的时候样式不发生改变

![在这里插入图片描述]( https://img-blog.csdnimg.cn/1d924b4376b24837a08031659683e989.png?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwMzU3NzY5,size_16,color_FFFFFF,t_70)

.warp{
  	height: 880px;
  	border: 20px solid antiquewhite;
  	/*定义弹性模型*/
  	display: flex;
    /*垂直居中*/
   justify-content: center; 
    /*水平居中*/
    align-items: center;
     /*空间不足自动换行,不改变原有子元素样式*/
    flex-wrap: wrap;
  }

![在这里插入图片描述]( https://img-blog.csdnimg.cn/b529feaf7f374b34af5fea61d6609758.png?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwMzU3NzY5,size_16,color_FFFFFF,t_70)

2.如何让子元素充分站满父元素空间?

![在这里插入图片描述]( https://img-blog.csdnimg.cn/51240d30f5084d2da1345ba7eddd4c19.png?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwMzU3NzY5,size_16,color_FFFFFF,t_70)

.box-1{
  	background: #FAC863;
    flex: 1;
  }

![在这里插入图片描述]( https://img-blog.csdnimg.cn/0268de07bbfb4536ae515eba3db6d841.png?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwMzU3NzY5,size_16,color_FFFFFF,t_70)

3. 如何不修改标签顺序而改变标签顺序呢

![在这里插入图片描述]( https://img-blog.csdnimg.cn/d9664ec75aa94c39a958330b2376b96f.png?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwMzU3NzY5,size_16,color_FFFFFF,t_70)

.box-1{
  	background: #FAC863;
  	 order: 3;
  }

  .box-2{
    background:	#1B2B34;
     order: 2;
  }
  .box-3{
  	background: #2ecc71;
  	 order: 1;
  }

![在这里插入图片描述]( https://img-blog.csdnimg.cn/f322e40f2e4f4ea68dd7720ad1b71eb9.png?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwMzU3NzY5,size_16,color_FFFFFF,t_70)

posted @ 2022-04-12 17:29  xiye1  阅读(54)  评论(0)    收藏  举报