CSS3 伸縮佈局

Merbng發表於2019-03-24
  • 主軸: Flex容器的主軸主要是用來配置Flex專案,預設是水平方向。
  • 側軸: 與主軸垂直的軸稱作側軸,預設是垂直方向的。
  • 方向: 預設主軸從左往右,側軸從上到下。 主軸和側軸並不是固定不變的,通過flex-direction可以互換

對瀏覽器的支援不一致

屬性詳解

1.min-width 最小寬度 、max-width 最大寬度 2.flex-direction 調整主軸方向(預設為水平方向) flex-direction:column(垂直排列) flex-direction:row(水平排列)

<!DOCTYPE html>
<html lang="zh">
	<head>
		<title></title>
	</head>
	<style type="text/css">
		* {
			margin: 0;
			padding: 0;
		}

		body {
			min-width: 320px;
			max-width: 540px;
			background-color: white;
			margin: 0 auto;
		}

		header {
			height: 108px;
		}

		header img {
			height: 100%;
			width: auto;
		}

		nav {
			border: 1px solid #ccc;
			padding: 4px;
		}

		.row {
			height: 90px;
			display: flex;
			border-radius: 5px;
			overflow: hidden;
			margin-bottom: 5px;
		}

		.row div {
			height: 100%;
			flex: 1;
			background-color: #ff687a;
			border-right: 1px solid white;
		}

		.row div:nth-child(3) {
			border-right: 0;
		}

		.row div a {
			display: block;
		}

		.row33 {
			display: flex;
			flex-direction: column;
		}

		.row33 a {
			flex: 1;
			text-align: center;
			line-height: 45px;
		}

		.row33 a:first-child {
			border-bottom: 1px solid white;
		}

		nav a {
			text-decoration: none;
			color: white;
			font-size: 14px;
			text-shadow: 0 2px 1px rgba(0, 0, 0, .2);
		}

		.row em {
			display: block;
			height: 45px;
			line-height: 45px;
			text-align: center;
			font-style: normal;
		}

		.row i {

			display: block;
			height: 43px;
			width: 43px;
			margin: -5px auto;
			background: url(images/ctrip.png) no-repeat 0 -127px;
			background-size: 104px auto;
			/* 瀏覽器字首 */
			-webkit-background-size: 104px;
			-moz-background-size: 104px;
			-ms-background-size: 104px;
			-o-background-size: 104px;
			background-position: 0 -288px;
		}

		.row .icon-flight {
			background-position: 0 -124px;
		}
	</style>
	<body>
		<header>
			<img src="images/banner.jpg" alt="">
		</header>
		<nav>
			<div class="row">
				<div><a href="#">
						<em>酒店</em>
						<i class="icon-flight"></i>
					</a>
				</div>
				<div class="row33">
					<a href="#">海外酒店</a>
					<a href="#">特價酒店</a>
				</div>
				<div class="row33">
					<a href="#">團購</a>
					<a href="#">民宿客棧</a>
				</div>
			</div>
			<div class="row">
				<div><a href="#">
						<em>酒店</em>
						<i></i>
					</a>
				</div>
				<div class="row33">
					<a href="#">海外酒店</a>
					<a href="#">特價酒店</a>
				</div>
				<div class="row33">
					<a href="#">團購</a>
					<a href="#">民宿客棧</a>
				</div>
			</div>
			<div class="row">
				<div><a href="#">
						<em>酒店</em>
						<i></i>
					</a>
				</div>
				<div class="row33">
					<a href="#">海外酒店</a>
					<a href="#">特價酒店</a>
				</div>
				<div class="row33">
					<a href="#">團購</a>
					<a href="#">民宿客棧</a>
				</div>
			</div>
			<div class="row">
				<div class="row33">
					<a href="#">海外酒店</a>
					<a href="#">特價酒店</a>
					</a>
				</div>
				<div class="row33">
					<a href="#">海外酒店</a>
					<a href="#">特價酒店</a>
				</div>
				<div class="row33">
					<a href="#">團購</a>
					<a href="#">民宿客棧</a>
				</div>
			</div>
		</nav>
	</body>
</html>
複製程式碼
  1. justify-content 調整主軸對齊(水平對齊) align-items 調整側軸對齊(垂直對齊)
<!DOCTYPE html>
<html lang="zh">
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<meta http-equiv="X-UA-Compatible" content="ie=edge">
		<title></title>
		<style type="text/css">
			section {
				width: 80%;
				height: 150px;
				margin: 100px auto;
				/* 給父親新增 伸縮佈局 */
				display: flex;
				/* section 最小的寬度是500 */
				min-width: 500px;
				/*預設水平分佈:row , 垂直分佈
				 
				 reverse翻轉
				 */
				flex-direction: row-reverse;
			}

			section div {
				height: 100%;

			}

			section div:nth-child(1) {
				background-color: pink;
				width: 200px;
			}

			section div:nth-child(2) {
				background-color: purple;
				flex: 2;
			}

			section div:nth-child(3) {
				background-color: red;
				flex: 1;
			}
		</style>
	</head>
	<body>
		<section>
			<div>1</div>
			<div>2</div>
			<div>3</div>
		</section>
	</body>
</html>
複製程式碼

事例程式碼地址

相關文章