bootstrap div 居中_Bootstrap栅格系统布局的对齐方式

这篇博客详细介绍了Bootstrap栅格系统中的布局对齐方法,包括垂直对齐(行和列的单独对齐)、水平对齐策略、列排序、列偏移、间距调整以及嵌套使用。对齐方式涵盖了从顶对齐、居中到底对齐,以及左对齐、居中、右对齐等不同场景。此外,还提到了如何创建列间距和进行列的顺序调整。
摘要由CSDN通过智能技术生成

1e532bf7f2551e9c6beedfea7694239d.png

垂直对齐

1、行的对齐方式
align-items-start 顶对齐
align-items-center 中间对齐
align-items-end 底对齐

<!DOCTYPE html>
<html>

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
	<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
		integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
	<title>栅格系统布局</title>
	<style>
		body{
			padding-bottom: 1000px;
		}
		.row div{
			height: 100px;
			background: green;
			border: 1px solid #000;
			color: #fff;
		}
		.v-align{
			height: 100px;
			background: rgba(255,0,0,0.1);
			margin: 10px -15px;
		}
		.v-align div{
			height: 40px;
			line-height: 40px;
			background: rgba(86, 61, 124, 0.15);
			border: 1px solid rgba(86, 61, 124, 0.2);
			color: #333;
		}
	</style>
</head>

<body>
	<div class="container">
		<div class="row v-align align-items-start">
			<div class="col">垂直对齐-顶部对齐-行的对齐方式</div>
			<div class="col">垂直对齐-顶部对齐-行的对齐方式</div>
			<div class="col">垂直对齐-顶部对齐-行的对齐方式</div>
		</div>
		<div class="row v-align align-items-center">
			<div class="col">垂直对齐-中间对齐-行的对齐方式</div>
			<div class="col">垂直对齐-中间对齐-行的对齐方式</div>
			<div class="col">垂直对齐-中间对齐-行的对齐方式</div>
		</div>
		<div class="row v-align align-items-end">
			<div class="col">垂直对齐-底对齐-行的对齐方式</div>
			<div class="col">垂直对齐-底对齐-行的对齐方式</div>
			<div class="col">垂直对齐-底对齐-行的对齐方式</div>
		</div>
	</div>
</body>

</html>

cd02f1367bb24bcb60a817c4d5ff4d4d.png

2、列的单独对齐方式
align-self-start 顶对齐
align-self-center 中间对齐
align-self-end 底对齐

<!DOCTYPE html>
<html>

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
	<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
		integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
	<title>栅格系统布局</title>
	<style>
		body{
			padding-bottom: 1000px;
		}
		.row div{
			height: 100px;
			background: green;
			border: 1px solid #000;
			color: #fff;
		}
		.v-align{
			height: 100px;
			background: rgba(255,0,0,0.1);
			margin: 10px -15px;
		}
		.v-align div{
			height: 40px;
			line-height: 40px;
			background: rgba(86, 61, 124, 0.15);
			border: 1px solid rgba(86, 61, 124, 0.2);
			color: #333;
		}
	</style>
</head>

<body>
	<div class="container">
		<div class="row v-align">
			<div class="col align-self-start">垂直对齐-顶对齐-列的对齐方式</div>
			<div class="col align-self-center">垂直对齐-中间对齐-列的对齐方式</div>
			<div class="col align-self-end">垂直对齐-底对齐-列的对齐方式</div>
		</div>
	</div>
</body>

</html>

a773c3cc8714dc02eef7dc1b63b468c0.png

水平对齐

1、justify-content-start 左对齐
2、justify-content-center 居中对齐
3、justify-content-end 右对齐
4、justify-content-around 分散居中对齐(每个元素两侧的间距是相等的)
5、justify-content-between 左右两端对齐(元素之间的间距是自动平分的)

<!DOCTYPE html>
<html>

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
	<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
		integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
	<title>栅格系统布局</title>
	<style>
		body{
			padding-bottom: 1000px;
		}
		.row div{
			height: 100px;
			background: green;
			border: 1px solid #000;
			color: #fff;
		}
		.v-align{
			height: 100px;
			background: rgba(255,0,0,0.1);
			margin: 10px -15px;
		}
		.v-align div{
			height: 40px;
			line-height: 40px;
			background: rgba(86, 61, 124, 0.15);
			border: 1px solid rgba(86, 61, 124, 0.2);
			color: #333;
		}
	</style>
</head>

<body>
	<div class="container">
		<div class="row v-align justify-content-start">
			<div class="col-4">水平对齐-左对齐</div>
			<div class="col-4">水平对齐-左对齐</div>
		</div>
		<div class="row v-align justify-content-center">
			<div class="col-4">水平对齐-居中对齐</div>
			<div class="col-4">水平对齐-居中对齐</div>
		</div>
		<div class="row v-align justify-content-end">
			<div class="col-4">水平对齐-右对齐</div>
			<div class="col-4">水平对齐-右对齐</div>
		</div>
		<div class="row v-align justify-content-around">
			<div class="col-4">水平对齐-分散居中对齐</div>
			<div class="col-4">水平对齐-分散居中对齐</div>
		</div>
		<div class="row v-align justify-content-between">
			<div class="col-4">水平对齐-左右两端对齐</div>
			<div class="col-4">水平对齐-左右两端对齐</div>
		</div>
	</div>
</body>

</html>

c7a0f1335ccdef13ac46d112ceda665d.png

列排序

4.x的版本使用.order-{breakpoint}-

(3.x的版本使用的是.col-{breakpoint}-push-* .col-{breakpoint}-pull-*来排序)

<!DOCTYPE html>
<html>

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
	<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
		integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
	<title>栅格系统布局</title>
	<style>
		body{
			padding-bottom: 1000px;
		}
		.row div{
			height: 100px;
			background: green;
			border: 1px solid #000;
			color: #fff;
		}
		.v-align{
			height: 100px;
			background: rgba(255,0,0,0.1);
			margin: 10px -15px;
		}
		.v-align div{
			height: 40px;
			line-height: 40px;
			background: rgba(86, 61, 124, 0.15);
			border: 1px solid rgba(86, 61, 124, 0.2);
			color: #333;
		}
	</style>
</head>

<body>
	<div class="container">
		<div class="row mt-5">
			<div class="col">第1列</div>
			<div class="col order-5">第2列</div>
			<div class="col order-6">第3列</div>
		</div>
		<div class="row mt-5">
			<div class="col">第1列</div>
			<div class="col order-xl-5">第2列</div>	<!-- 只有当屏幕尺寸>=1200的时候,才会进行排序 -->
			<div class="col order-xl-2">第3列</div>
		</div>
		<div class="row mt-5">
			<div class="col">第1列</div>
			<div class="col order-first">第2列</div><!-- order-first代表排在第1位,order-last代表排在最后一位 -->
			<div class="col order-last">第3列</div>
			<div class="col">第4列</div>
		</div>
	</div>
</body>

</html>

becd223fbe2284b76744fc4ad577edd5.png

列偏移

使用offset-{breakpoint}-*

<!DOCTYPE html>
<html>

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
	<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
		integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
	<title>栅格系统布局</title>
	<style>
		body{
			padding-bottom: 1000px;
		}
		.row div{
			height: 100px;
			background: green;
			border: 1px solid #000;
			color: #fff;
		}
		.v-align{
			height: 100px;
			background: rgba(255,0,0,0.1);
			margin: 10px -15px;
		}
		.v-align div{
			height: 40px;
			line-height: 40px;
			background: rgba(86, 61, 124, 0.15);
			border: 1px solid rgba(86, 61, 124, 0.2);
			color: #333;
		}
	</style>
</head>

<body>
	<div class="container">
		<!-- 列偏移,使用offset-{breakpoint}-* -->
		<div class="row mt-5">
			<div class="col-md-4">第1列</div>
			<div class="col-md-4 offset-md-4">往右偏移4列</div>
		</div>
		<div class="row mt-5">
			<div class="col-3 offset-md-3">第1列</div>
			<div class="col-3 offset-md-3">第2列</div>
		</div>
		<div class="row mt-5">
			<div class="col-sm-5 col-md-6">小屏占5列,中屏占6列</div>
			<div class="col-sm-5 offset-sm-3 col-md-6 offset-md-5">小屏偏移3列,中屏偏移5列</div>
		</div>
	</div>
</body>

</html>

7572dd9288b374afb7bba7efce5e9f2f.png

间距:使用margin工具可以让列之间产生间距

mr-{breakpoint}-auto 使右侧的列远离到最右边
ml-{breakpoint}-auto 使左侧的列远离到最左边

<!DOCTYPE html>
<html>

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
	<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
		integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
	<title>栅格系统布局</title>
	<style>
		body{
			padding-bottom: 1000px;
		}
		.row div{
			height: 100px;
			background: green;
			border: 1px solid #000;
			color: #fff;
		}
		.v-align{
			height: 100px;
			background: rgba(255,0,0,0.1);
			margin: 10px -15px;
		}
		.v-align div{
			height: 40px;
			line-height: 40px;
			background: rgba(86, 61, 124, 0.15);
			border: 1px solid rgba(86, 61, 124, 0.2);
			color: #333;
		}
	</style>
</head>

<body>
	<div class="container">
		<div class="row mt-5">
			<div class="col-md-4">第1列</div>
			<div class="col-md-4 ml-auto">第2列,跑到最右边</div>
		</div>
		<div class="row mt-5">
			<div class="col-md-3 ml-md-auto">在中屏下,离左边距离自动计算</div>
			<div class="col-md-3 ml-md-auto">在中屏下,离左边距离自动计算</div>
		</div>
		<div class="row mt-5">
			<div class="col-auto mr-auto">宽度由内容撑开</div>
			<div class="col-auto">宽度由内容撑开</div>
		</div>
	</div>
</body>

</html>

嵌套

每一个列里面可以再继续放行,那嵌套里面的元素就会以父级的宽度为基准,再分12个列.

<!DOCTYPE html>
<html>

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
	<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
		integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
	<title>栅格系统布局</title>
	<style>
		body{
			padding-bottom: 1000px;
		}
		.row div{
			height: 100px;
			background: green;
			border: 1px solid #000;
			color: #fff;
		}
		.v-align{
			height: 100px;
			background: rgba(255,0,0,0.1);
			margin: 10px -15px;
		}
		.v-align div{
			height: 40px;
			line-height: 40px;
			background: rgba(86, 61, 124, 0.15);
			border: 1px solid rgba(86, 61, 124, 0.2);
			color: #333;
		}
	</style>
</head>

<body>
	<div class="container">
		<div class="row mt-5">
			<div class="col-sm-9" style="height: 150px;background: grey;">
				父级的第1列
				<div class="row">
					<div class="col-sm-8 col-6">子级的第1列,小屏下占8列,超小屏下占6列</div>
					<div class="col-sm-4 col-6">子级的第2列,小屏下占4列,超小屏下占6列</div>
				</div>
			</div>
			<div class="col-sm-3" style="height: 150px;background: pink;">父级的第2列</div>
		</div>
	</div>
</body>

</html>

df0229f1609198327a0af30a3ea9092c.png
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值