弹性盒子flex的相关属性

两个div并排行内显示,实现方式:

  1. 设置为行内样式,display:inline-block
  2. 设置floa浮动
  3. 设置position定位属性为absolute
  4. 设置弹性盒子display:flex

		<style type="text/css">
			.box{
				width: 500px;
				height: 300px;
				display:flex ;
			}
			.box-1{
				order: 1;/*位置越小越往前*/
				flex-grow: 1;/*平均分布*/
				background-color: black;
				flex-basis: 200px;
			}
			.box-2{
				order: 2;
				flex-grow: 1;
				flex-basis: 200px;
				background-color: bisque;
			}
			.box-3{
				order: 3;
				flex-grow: 1;
				flex-basis: 200px;
				background-color: aquamarine;
			}
		</style>
	</head>
	<body>
		<div class="box">
			<div class="box-1"></div>
			<div class="box-2"></div>
			<div class="box-3"></div>
		</div>
	</body>

flex-grow

flex-grow 属性用于设置或检索弹性盒子的扩展比率。

				order: 1;
				flex-grow: 1;
				background-color: black;
			}
			.box-2{
				order: 2;
				flex-grow: 2;/*第二个元素为其他元素得两倍*/
				background-color: bisque;
			}
			.box-3{
				order: 3;
				flex-grow: 1;
				background-color: aquamarine;
			}

flex-basis

设置弹性盒元素的初始长度为 80 像素
*若设置了父级盒子的宽度,只显示子级basis设置的像素长,覆盖掉width的值

<style type="text/css">
			.box{
				width: 200px;/*被basis覆盖*/
				height: 300px;
				display:flex ;
			}
			.box-1{
				order: 1;
				background-color: black;
				flex-basis: 400px;
			}
			.box-2{
				order: 2;
				flex-basis: 200px;
				background-color: bisque;
			}
			.box-3{
				order: 3;
				flex-basis: 200px;
				background-color: aquamarine;
			}
		</style>
	</head>
	<body>
		<div class="box">
			<div class="box-1"></div>
			<div class="box-2"></div>
			<div class="box-3"></div>
		</div>
	</body>

flex-shrink

flex-shrink的默认值为1,如果没有显示定义该属性,将会自动按照默认值1在所有因子相加之后计算比率来进行空间收缩。
*如果元素不是弹性盒对象的元素,则 flex-shrink 属性不起作用。

			.box{
				width: 500px;
				height: 300px;
				display:flex ;
			}
			.box-1{
				order: 1;
				flex-grow: 1;
				flex-shrink: 0;
				background-color: black;
				flex-basis: 200px;
			}
			.box-2{
				order: 2;
				flex-grow: 1;
				flex-shrink: 0;
				flex-basis: 200px;
				background-color:bisque;
			}
			.box-3{
				order: 3;
				flex-grow: 1;
				flex-shrink: 1;
				flex-basis: 200px;
				background-color: aquamarine;
			}

父元素box的宽度为500px,子元素被定义为200px,那么2003=600px,超出父元素100px,那么超出的100px就需要被box1-2-3笑话,所以1001+1001+1001=300px,那么1-2-3就被溢出(100*1/300)*100=33px,所以1-2-3的宽应该为100-33=67px

*flex是flex-grow,flex-shrink, flex-basis 的缩写形式,缩写规则:

如:flex 取值为 none,则计算值为 0 0 auto

.box{flex: none;}
.box{
    flex-grow: 0;
    flex-shrink: 0;
    flex-basis: auto;
}

当 flex取值为 auto,则计算值为 1 1 auto

.box {flex: auto;}
.box {
    flex-grow: 1;
    flex-shrink: 1;
    flex-basis: auto;
}

flex-direction

row:默认顺序
row-reverse:与row相反
column:将垂直显示,如列一样
column-reverse:与column相反

div
{
display:flex;
flex-direction:row-reverse;/*与row顺序相反==倒叙*/
}

flex-wrap

让弹性盒元素在必要的时候拆行(当父级width不够一行显示子级元素时,就会下一行显示)

nowrap:默认值,不灵活拆行
wrap:规定在必要的时候拆行或不拆行
wrap-reverse:规定在必要的时候拆行或不拆行,但是以相反的顺序

	.box{
				width: 500px;
				height: 300px;
				display:flex ;
				flex-wrap: wrap-reverse;
			}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值