第二节 弹性盒子( justify-content属性、align-items属性、flex-direction属性、flex-wrap属性)

一、弹性盒子的定义

弹性盒子( Flexible Box 或 flexbox):CSS3的一种新布局模式。

是一种当页面需要适应不同的屏幕大小以及设备类型时确保元素拥有恰当的行为的布局方式。

二、flex-direction属性:决定主轴的方向(即项目的排列方向)

  • row(默认值):主轴为水平方向,起点在左端;
  •  row-reverse:主轴为水平方向,起点在右端;
  • column:主轴为垂直方向,起点在上沿;
  • column-reverse:主轴为垂直方向,起点在下沿。
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <style>
        * {
            margin: 0px;
            padding: 0px;
        }

        .box {
             width: 500px;
			height: 500px;
			background-color: #E83E78;
            display: flex;
            /*flex-direction: row;               水平方向,起点在左边;
            flex-direction: row-reverse;         水平方向,起点在右边;
            flex-direction: column;              垂直方向,起点在上边;
            flex-direction: column-reverse;      垂直方向,起点在下边。
            */
        }
        .box .box1{
				width: 200px;
				height: 200px;	
		}
			.box .box1:nth-of-type(1){
				background-color: #FFC0CB;
		}
			.box .box1:nth-of-type(2){
				background-color: #f0f;
		}
			.box .box1:nth-of-type(3){
				background-color: #00f;
	    }
    </style>
    <title>弹性盒子</title>
</head>
<body>
<div class="box">
    <div class="box1">项目1</div>
    <div class="box1">项目2</div>
    <div class="box1">项目3</div>
</div>
</body>
</html>

三、flex-wrap属性:

默认情况下,项目都排在一条线(又称”轴线”)上。flex-wrap属性定义,如果一条轴线排不下,如何换行。

  • nowrap(默认):不换行;
  • wrap:换行,第一行在上方;
  •  wrap-reverse:换行,第一行在下方。
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <style>
        * {
            margin: 0px;
            padding: 0px;
        }

        .box{
				display: flex;
				width: 500px;
				height: 500px;
				background-color: #E83E78;
            /*flex-wrap: nowrap;        不换行;
              flex-wrap: wrap;          换行,第一行在上方;
              flex-wrap: wrap-reverse;  换行,第一行在下方。*/

        }
        .box .box1{
			width: 200px;
			height: 200px;	
		}
		.box .box1:nth-of-type(1){
			background-color: #FFC0CB;
		}
		.box .box1:nth-of-type(2){
			background-color: #f0f;
		}
		.box .box1:nth-of-type(3){
			background-color: #00f;
		}
		.box .box1:nth-of-type(4){
			background-color: #ff0;
		}
			
		</style>
	</head>
	<body>
		<div class="box">
			<div class="box1">盒子1</div>
			<div class="box1">盒子2</div>
			<div class="box1">盒子3</div>
			<div class="box1">盒子4</div>
		</div>
	</body>
</html>

四、flex-flow属性:

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

五、justify-content属性:定义了项目在主轴上的对齐方式。

  • flex-start(默认值):左对齐;
  •  flex-end:右对齐;
  • center: 居中;
  • space-between:两端对齐,项目之间的间隔都相等;
  • space-around:每个项目两侧的间隔相等。项目之间的间隔比项目与边框的间隔大一倍。
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <style>
        * {
            margin: 0px;
            padding: 0px;
        }

       .box{
			display: flex;
			width: 500px;
			height: 500px;
			background-color: #E83E78;
          
            /*justify-content: flex-start;       左对齐;
            justify-content: flex-end;         右对齐;
            justify-content: center;           居中;
            justify-content: space-between;    两端对齐,项目之间的间隔都相等;
            justify-content: space-around;     每个项目两侧的间隔相等。
*/

        }

        .box .box1{
				width: 200px;
				height: 200px;	
			}
			.box .box1:nth-of-type(1){
				background-color: #FFC0CB;
			}
			.box .box1:nth-of-type(2){
				background-color: #f0f;
			}
			.box .box1:nth-of-type(3){
				background-color: #00f;
			}
			.box .box1:nth-of-type(4){
				background-color: #ff0;
			}
			
		</style>
	</head>
	<body>
		<div class="box">
			<div class="box1">盒子1</div>
			<div class="box1">盒子2</div>
			<div class="box1">盒子3</div>
			<div class="box1">盒子4</div>
		</div>
	</body>
</html>

六、align-items属性:定义项目在交叉轴上如何对齐。

  •  flex-start:交叉轴的起点对齐;
  •  flex-end:交叉轴的终点对齐;
  •  center:与交叉轴的中点对齐;
  •  stretch(默认值):轴线占满整个交叉轴。
  • baseline 与第一行文字基线对齐
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <style>
        * {
            margin: 0px;
            padding: 0px;
        }

       .box{
			display: flex;
			width: 500px;
			height: 500px;
			background-color: #E83E78;
            align-items: flex-start;          交叉轴的起点对齐;
            align-items: flex-end;            交叉轴的终点对齐;
            align-items: center;              与交叉轴的中点对齐;
            align-items: stretch;             轴线占满整个交叉轴
            align-items: baseline             与第一行文字基线对齐
        }

       .box .box1{
				width: 200px;
				height: 200px;	
			}
			.box .box1:nth-of-type(1){
				background-color: #FFC0CB;
			}
			.box .box1:nth-of-type(2){
				background-color: #f0f;
			}
			.box .box1:nth-of-type(3){
				background-color: #00f;
			}
			.box .box1:nth-of-type(4){
				background-color: #ff0;
			}
			
		</style>
	</head>
	<body>
		<div class="box">
			<div class="box1">盒子1</div>
			<div class="box1">盒子2</div>
			<div class="box1">盒子3</div>
			<div class="box1">盒子4</div>
		</div>
	</body>
</html>

七、align-content属性:定义了多根轴线的对齐方式;

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

       .box{
			display: flex;
			width: 500px;
			height: 500px;
			background-color: #E83E78;
            align-content: flex-start;          交叉轴的起点对齐;
            align-content: flex-end;            交叉轴的终点对齐;
            align-content: center;              与交叉轴的中点对齐;
            align-content: space-between;       与交叉轴两端对齐,轴线之间的间隔平均分布;
            align-content: space-around;        每根轴线两侧的间隔都相等
            align-content: stretch;             轴线占满整个交叉轴
        }

       .box .box1{
				width: 200px;
				height: 200px;	
			}
			.box .box1:nth-of-type(1){
				background-color: #FFC0CB;
			}
			.box .box1:nth-of-type(2){
				background-color: #f0f;
			}
			.box .box1:nth-of-type(3){
				background-color: #00f;
			}
			.box .box1:nth-of-type(4){
				background-color: #ff0;
			}
			
		</style>
	</head>
	<body>
		<div class="box">
			<div class="box1">盒子1</div>
			<div class="box1">盒子2</div>
			<div class="box1">盒子3</div>
			<div class="box1">盒子4</div>
		</div>
	</body>
</html>
  • 10
    点赞
  • 75
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: justify-content:flex-start; 是 CSS 中用于设置 flex 容器内的子元素在主轴上的对齐方式为起点对齐。即让子元素沿着主轴从容器的起点开始排列。该属性通常与 display: flex; 或 display: inline-flex; 一起使用。 ### 回答2: justify-content: flex-start; 是一种CSS属性,可以用于定义flex容器中flex项的水平对齐方式。当设置为justify-content: flex-start;时,flex项会在容器的起始位置进行水平对齐。 具体来说,当使用flex容器时,默认情况下flex项会自动填满整个容器的宽度。如果有多个flex项,并且设置了justify-content: flex-start;,那么这些flex项会尽可能靠近容器的起始位置进行水平对齐。 举个例子,假设有一个水平方向的flex容器,里面有三个flex项。当设置了justify-content: flex-start;后,这三个flex项会分别在容器的起始位置、起始位置后的一个位置和起始位置后的第二个位置进行水平对齐。 请注意,如果容器的宽度超过了flex项的总宽度,那么这些flex项之间会有一定的间隙。这是因为justify-content: flex-start;只关注flex项的对齐方式,而不会调整flex项之间的间距。如果需要调整flex项之间的间距,可以使用其他的CSS属性,如margin或padding。 总结起来,justify-content: flex-start;用于将flex容器中的flex项在水平方向上靠近容器的起始位置进行对齐。这是在flex布局中非常常用的一种对齐方式。 ### 回答3: justify-content: flex-start; 是一种CSS属性,用于在屏幕上对齐flex容器中的子元素,将其位于主轴的起始位置。 使用justify-content: flex-start;,子元素将会沿着主轴的起始位置对齐,即从flex容器的左侧开始排列。 这意味着在水平方向上,子元素会左对齐,占据整个flex容器的左侧空间,并且任何剩余的空间都会留在容器的右侧。 在垂直方向上,子元素的对齐方式不受justify-content的影响,而是由align-items属性决定。 通过使用justify-content: flex-start;,我们可以轻松实现一个左对齐的布局,适用于多种场景,比如导航栏、图片缩略图等等。 此外,通过与其他属性的组合使用,我们还可以创建更复杂的布局效果,比如通过flex-wrap属性来实现子元素的自动换行,再配合flex-direction属性可以控制排列的方向。 总而言之,justify-content: flex-start; 可以通过定义子元素在主轴上的对齐方式,实现左对齐的布局效果。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值