弹性布局换行左对齐(justify-content: space-between)

第一种办法:

因为每一行元素是固定的,所以我们拿5个元素为一行作为案例,废话不多说直接上案例.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
	.flex {
		width: 700px;
		height: 400px;
		display: flex;
		justify-content: space-between;
		flex-wrap: wrap;
	}
	.flex>div{
		background: skyblue;
		width: 120px;
		height: 120px;
	}
    //最后一个元素在第2列的情况
	.flex div:last-child:nth-child(5n + 2) {
	  margin-right: calc((100% - 120px) / 4 * 3);
	}
    //最后一个元素在第3列的情况
	.flex div:last-child:nth-child(5n + 3) {
	  margin-right: calc((100% - 120px) / 4 * 2);
	}
   //最后一个元素在第4列的情况
	.flex div:last-child:nth-child(5n + 4) {
	  margin-right: calc((100% - 120px) / 4 * 1);
	}
</style>
</head>
<body>
	<div class="flex">
		<div>库里</div>
		<div>科比</div>
		<div>詹姆斯</div>
		<div>欧文</div>
		<div>杜兰特</div>
		<div>乔丹</div>
        <div>乔治</div>
	</div>
</body>
</html>

第二种办法:

直接使用网格布局,比较方便.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
	.flex {
		width: 700px;
		height: 400px;
		display: grid;
		justify-content: space-around;
		grid-template-columns: repeat(auto-fill, 120px);
        grid-gap: 20px; 
	}
	.flex>div{
		background: skyblue;
		width: 120px;
		height: 120px;
	}
</style>
</head>
<body>
	<div class="flex">
		<div>库里</div>
		<div>科比</div>
		<div>詹姆斯</div>
		<div>欧文</div>
		<div>杜兰特</div>
		<div>乔丹</div>
		<div>乔治</div>
	</div>
</body>
</html>

第三种办法:

直接计算 ,分配剩余空间

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<style>
			.box {
				display: flex;
				flex-wrap: wrap;
				width: 40%;
				border: 1px solid red;
			}
			.son1 {
				width: 100px;
				height: 100px;
				background: deepskyblue;
			}
			.son2 {
				width: 100px;
				height: 100px;
				background: pink;
			}
			.son3 {
				width: 100px;
				height: 100px;
				background: yellow;
			}
			.son4 {
				width: 100px;
				height: 100px;
				background: green;
			}
			.box > div {
				--n: 5;
				--space:calc(100% - var(--n) * 100px);
				--h:calc(var(--space)/var(--n) /2);
				margin: 10px var(--h);
			}
		</style>
		<div class="box">
			<div class="son1">1</div>
			<div class="son2">2</div>
			<div class="son3">3</div>
			<div class="son4">4</div>
			<div class="son1">1</div>
			<div class="son2">2</div>
			<div class="son3">3</div>
			<div class="son4">4</div>
			<div class="son1">1</div>
			<div class="son2">2</div>
			<div class="son3">3</div>
			<div class="son4">4</div>
		</div>
	</body>
</html>
  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值