Flex布局练习-网格布局

Flex布局练习-网格布局


学习来源:阮一峰的flex布局实战
网格布局

<style type="text/css">
		/*设置最外层父元素*/
		.div1{
			width: 100px;
			height: 100px;
			background-color: grey;
			display: flex;
			display: -moz-flex;
			display; -webkit-flex;
		}
		/*设置显示字体*/
		div{
			font-size:10px;
			color: white;
		}
	</style>

在这里插入图片描述

<div class="div1" style="flex-wrap: wrap; align-content: flex-start;">
	<div style="display: flex; width: 100px; height: 20px;">
		<div style="flex-grow: 1; background-color: red; height: 20px;">50%</div>
		<div style="flex-grow: 1; background-color: green; height: 20px;">50%</div>
	</div>
	<div style="display: flex; width: 100px; height: 20px;">
		<div style="flex-grow: 1; background-color: teal; height: 20px;">33%</div>
		<div style="flex-grow: 1; background-color: gold; height: 20px;">33%</div>
		<div style="flex-grow: 1; background-color: orange; height: 20px;">33%</div>
	</div>
	<div style="display: flex; width: 100px; height: 20px;">
		<div style="flex-grow: 1; background-color: pink; height: 20px;">25%</div>
		<div style="flex-grow: 1; background-color: purple; height: 20px;">25%</div>
		<div style="flex-grow: 1; background-color: blue; height: 20px;">25%</div>
		<div style="flex-grow: 1; background-color: black; height: 20px;">25%</div>
	</div>
	<div style="display: flex; width: 100px; height: 40px;">
		<div style="flex-grow: 1; height: 40px; background-color: red;">50%</div>
		<div style="flex-grow: 1; height: 40px; background-color: green;">50%</div>
	</div>
</div>
上面的实现思路就是使用子元素属性中的flex-grow来实现,调整flex-grow值来设置属性

在这里插入图片描述

<div class="div1" style="flex-wrap: wrap; align-content: flex-start; height:90px;">
	<div style="width: 100px; height: 30px; display: flex;">
		<div style="flex: 0 0 50%; height:30px; background-color: red;">50%</div>
		<div style="flex-grow: 1; height: 30px; background-color: green;">自适应</div>
		<div style="flex-grow: 1; height: 30px; background-color: blue;">自适应</div>
	</div>
	<div style="width: 100px; height: 30px; display: flex;">
		<div style="flex-grow: 1; height: 30px; background-color: orange;">自适应</div>
		<div style="flex-grow: 0; flex-shrink: 0; flex-basis: 33.3333%; height: 30px; background-color: teal;">33%</div>
	</div>
	<div style="width: 100px; height: 30px; display: flex;">
		<div style="height: 30px; background-color: pink; flex: 0 0 25%;">25%</div>
		<div style="height: 30px; background-color: black; flex-grow: 1;">自适应</div>
		<div style="height: 30px; background-color: gold; flex-grow: 0; flex-shrink: 0; flex-basis: 33.3333%;">33%</div>
	</div>
</div>
上述案例是部分固定,剩余自适应。固定的话,设置flex-grow和flex-shrink都为0不参与放大和缩小,
设置flex-basis为固定值即可。对于自适应的部分,将flex-grow调为1即可,会自动分配剩余空间。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Vanghua

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值