flex布局 父元素属性之 align-items 子元素在侧轴上的排列方式(单项子元素)

align-items

align-items :设置子元素在侧轴上(默认侧轴为y轴)的排列方式


align-items的属性值:

属性值含义
flex-start子元素贴着侧轴的头部排列(默认值)
flex-end子元素贴着侧轴的尾部排列
center子元素在侧轴上居中对齐
stretch拉伸,使得子元素在侧轴方向上与父元素等大


具体实例(以侧轴为y轴为列):

flex-start

	<body>
		<div>
			<span>1</span>
			<span>2</span>
			<span>3</span>
		</div>
	</body>
	<style type="text/css">
			div{
				display: flex;	
				width: 800px;
				height: 400px;
				background-color: orange;
				align-items: flex-start;	/*默认值*/
			}
			div span{
				width: 200px;
				height: 100px;
				background: skyblue;
			}
	</style>

执行结果:子元素贴着侧轴的头部排列(顶端对齐
在这里插入图片描述

flex-end

	<body>
		<div>
			<span>1</span>
			<span>2</span>
			<span>3</span>
		</div>
	</body>
	<style type="text/css">
			div{
				display: flex;	
				width: 800px;
				height: 400px;
				background-color: orange;
				align-items: flex-end;	
			}
			div span{
				width: 200px;
				height: 100px;
				background: skyblue;
			}
	</style>

执行结果:子元素贴着侧轴的尾部排列(底端对齐
在这里插入图片描述

center

	<body>
		<div>
			<span>1</span>
			<span>2</span>
			<span>3</span>
		</div>
	</body>
	<style type="text/css">
			div{
				display: flex;	
				width: 800px;
				height: 400px;
				background-color: orange;
				align-items: center;	
			}
			div span{
				width: 200px;
				height: 100px;
				background: skyblue;
			}
	</style>

执行结果:子元素在侧轴上居中对齐
在这里插入图片描述

stretch

	<body>
		<div>
			<span>1</span>
			<span>2</span>
			<span>3</span>
		</div>
	</body>
	<style type="text/css">
			div{
				display: flex;	
				width: 800px;
				height: 400px;
				background-color: orange;
				align-items: stretch;	
			}
			div span{
				width: 200px;
				/*height: 100px;*/	
				background: skyblue;
			}
	</style>

执行结果:子元素沿着侧轴(y轴)方向拉伸,使得子元素与父元素等高
在这里插入图片描述
值得一提的是:在使用stretch属性值时,不要给子元素高度/宽度(侧轴为y轴时不给高度,侧轴为x轴时不给宽度),否则在侧轴方向上将没有拉伸效果

如:

	<body>
		<div>
			<span>1</span>
			<span>2</span>
			<span>3</span>
		</div>
	</body>
	<style type="text/css">
			div{
				display: flex;	
				width: 800px;
				height: 400px;
				background-color: orange;
				align-items: stretch;	
			}
			div span{
				width: 200px;
				height: 100px;
				background: skyblue;
			}
	</style>

执行结果:因为给子元素span设置了高度为100px,故在侧轴上实现不了拉伸效果
在这里插入图片描述


下面来看侧轴为x轴的情况:

flex-start

	<body>
		<div>
			<span>1</span>
			<span>2</span>
			<span>3</span>
		</div>
	</body>
	<style type="text/css">
			div{
				display: flex;	
				width: 800px;
				height: 400px;
				background-color: orange;
				flex-direction: column;
				align-items: flex-start;	/*默认值 可不写*/
			}
			div span{
				width: 200px;
				height: 100px;
				background: skyblue;
			}
	</style>

执行结果:子元素贴着侧轴的头部排列(左端对齐
在这里插入图片描述

flex-end

	<body>
		<div>
			<span>1</span>
			<span>2</span>
			<span>3</span>
		</div>
	</body>
	<style type="text/css">
			div{
				display: flex;	
				width: 800px;
				height: 400px;
				background-color: orange;
				flex-direction: column;
				align-items: flex-end;	
			}
			div span{
				width: 200px;
				height: 100px;
				background: skyblue;
			}
	</style>

执行结果:子元素贴着侧轴的尾部排列(右端对齐
在这里插入图片描述

center

	<body>
		<div>
			<span>1</span>
			<span>2</span>
			<span>3</span>
		</div>
	</body>
	<style type="text/css">
			div{
				display: flex;	
				width: 800px;
				height: 400px;
				background-color: orange;
				flex-direction: column;
				align-items: center;	
			}
			div span{
				width: 200px;
				height: 100px;
				background: skyblue;
			}
	</style>

执行结果:子元素在侧轴上居中对齐
在这里插入图片描述

stretch

	<body>
		<div>
			<span>1</span>
			<span>2</span>
			<span>3</span>
		</div>
	</body>
	<style type="text/css">
			div{
				display: flex;	
				width: 800px;
				height: 400px;
				background-color: orange;
				flex-direction: column;
				align-items: stretch;	
			}
			div span{
				/*width: 200px; 	不给子元素宽度*/
				height: 100px;	
				background: skyblue;
			}
	</style>

执行结果:子元素沿着侧轴方向拉伸,使得子元素与父元素等宽
在这里插入图片描述
注意:align-items属性只应用于侧轴上的子元素是单项(单行或单列)的情况,如果出现子元素有多项(换行或换列)的情况,即flex-wrap:wrap;,则不能使用align-items,而要使用align-content属性,该属性的解释放在下一篇博客。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值