1. 程式人生 > 其它 >flex佈局好好研究把,總是記不住,記性差了,為什麼

flex佈局好好研究把,總是記不住,記性差了,為什麼

複製下面程式碼,直接執行

程式碼裡註釋過大,大家忍一忍

預覽效果如下

<!DOCTYPE html>


<style>
    /*任何一個容器都可以指定為Flex佈局。
 * .box{display:flex;}
 * 行內元素也可以使用Flex佈局
 * .box{display:inline-flex;}
 * Webkit核心的瀏覽器,必須加上-webkit字首。
 * .box{
 * 	display:-webkit-flex;
 *  display:flex;
 * }
 * 注意,設為Flex佈局以後,子元素的float,clear和vertical-align屬性失效。
 * 採用 Flex 佈局的元素,稱為 Flex 容器(flex container),簡稱"容器"。它的
 * 所有子元素自動成為容器成員,稱為 Flex 專案(flex item),簡稱"專案"。
 * 容器的屬性
 * flex-direction屬性決定主軸的方向(即專案的排列方向)。row | row-reverse | column | column-reverse;
   flex-wrap屬性預設情況下,專案都排在一條線(又稱"軸線")上。flex-wrap屬性定義,如果一條軸線排不下,如何換行。 nowrap | wrap | wrap-reverse;
   flex-flow屬性是flex-direction屬性和flex-wrap屬性的簡寫形式,預設值為row nowrap。<flex-direction> || <flex-wrap>;
   justify-content屬性定義了專案在主軸上的對齊方式。flex-start | flex-end | center | space-between | space-around;
   align-items屬性定義專案在交叉軸上如何對齊。flex-start | flex-end | center | baseline | stretch;
   align-content屬性定義了多根軸線的對齊方式。如果專案只有一根軸線,該屬性不起作用。flex-start | flex-end | center | space-between | space-around | stretch;
 * 專案的屬性
 * order屬性定義專案的排列順序。數值越小,排列越靠前,預設為0。值為<integer>;
   flex-grow屬性定義專案的放大比例,預設為0,即如果存在剩餘空間,也不放大。如果所有專案的flex-grow屬性都為1,則它們將等分剩餘空間(如果有的話)。如果一個專案的flex-grow屬性為2,其他專案都為1,則前者佔據的剩餘空間將比其他項多一倍。值為<number>
   flex-shrink屬性定義了專案的縮小比例,預設為1,即如果空間不足,該專案將縮小。如果所有專案的flex-shrink屬性都為1,當空間不足時,都將等比例縮小。如果一個專案的flex-shrink屬性為0,其他專案都為1,則空間不足時,前者不縮小。負值對該屬性無效。值為<number>
   flex-basis屬性定義了在分配多餘空間之前,專案佔據的主軸空間(main size)。瀏覽器根據這個屬性,計算主軸是否有多餘空間。它的預設值為auto,即專案的本來大小。它可以設為跟width或height屬性一樣的值(比如350px),則專案將佔據固定空間。值為<length>
   flex屬性是flex-grow, flex-shrink 和 flex-basis的簡寫,預設值為0 1 auto。後兩個屬性可選。該屬性有兩個快捷值:auto (1 1 auto) 和 none (0 0 auto)。建議優先使用這個屬性,而不是單獨寫三個分離的屬性,因為瀏覽器會推算相關值。
   align-self屬性允許單個專案有與其他專案不一樣的對齊方式,可覆蓋align-items屬性。預設值為auto,表示繼承父元素的align-items屬性,如果沒有父元素,則等同於stretch。auto | flex-start | flex-end | center | baseline | stretch;
 * 
 * */

.first-face {
	display: flex;
	justify-content: center;
	align-items: center;
}
.second-face {
	display: flex;
	justify-content: space-between;
}
.second-face .pip:nth-of-type(2) {
	align-self: flex-end;
}
.third-face {
	display: flex;
	justify-content: space-between;
}
.third-face .pip:nth-of-type(2){
	align-self: center;
}
.third-face .pip:nth-of-type(3) {
	align-self: flex-end;
}
.fourth-face,.sixth-face {
	display: flex;
	justify-content: space-between;
}
.fourth-face .column, .sixth-face .column{
	display: flex;
	flex-direction: column;
	justify-content: space-between;
}
.fifth-face {
	display: flex;
	justify-content: space-between;
}
.fifth-face .column {
	display: flex;
	flex-direction: column;
	justify-content: space-between;
}
.fifth-face .column:nth-of-type(2){
	justify-content: center;
}
.
/*other*/
* {
	box-sizing: border-box;
}
html,body {
	height: 100%;
}
body {
	
	display: flex;
	align-items: center;
	justify-content: center;
	vertical-align: center;
	flex-wrap: wrap;
	align-content: center;
	font-family: "微軟雅黑";
    background: linear-gradient(top,#222,#333);	
}

[class$="face"] {
	margin: 16px;
	padding: 4px;
	background-color: #e7e7e7;
	width: 104px;
	height: 104px;
	object-fit: contain;
	box-shadow: 
	   inset 0 5px white,
	   inset 0 5px #bbb,
	   inset 0 5px #d7d7d7,
	   inset 0 -5px #d7d7d7;
	border-radius: 10%;
}

.pip {
	display: block;
	width: 24px;
	height: 24px;
	border-radius: 50%;/*圓角*/
	margin: 4px;/*整體偏移*/
	background-color: #333;/*背景顏色*/
	box-shadow: inset 0 3px #111,inset 0 -3px #555;/*陰影*/
}
</style>
<html>

	<head>
		<meta charset="UTF-8" />
		<title>Hello React!</title>
		<link rel="stylesheet" type="text/css" href="css/firstCSS.css" />
	</head>

	<body>
		<div class="first-face">
			<span class="pip"></span>
		</div>
		<div class="second-face">
			<span class="pip"></span>
			<span class="pip"></span>
		</div>
		<div class="third-face">
			<span class="pip"></span>
			<span class="pip"></span>
			<span class="pip"></span>
		</div>
		<div class="fourth-face">
			<div class="column">
				<span class="pip"></span>
				<span class="pip"></span>
			</div>
			<div class="column">
				<span class="pip"></span>
				<span class="pip"></span>
			</div>
		</div>
		<div class="fifth-face">
			<div class="column">
				<span class="pip"></span>
				<span class="pip"></span>
			</div>
			<div class="column">
				<span class="pip"></span>
			</div>
			<div class="column">
				<span class="pip"></span>
				<span class="pip"></span>
			</div>
		</div>
		<div class="sixth-face">
			<div class="column">
				<span class="pip"></span>
				<span class="pip"></span>
				<span class="pip"></span>
			</div>
			<div class="column">
				<span class="pip"></span>
				<span class="pip"></span>
				<span class="pip"></span>
			</div>
		</div>
		<div class="sixth-face">
			<div class="column">
				<span class="pip"></span>
				<span class="pip"></span>
				<span class="pip"></span>
			</div>
			<div class="column">
				<span class="pip"></span>
				<span class="pip"></span>
				<span class="pip"></span>
			</div>
			<div class="column">
				<span class="pip"></span>
				<span class="pip"></span>
				<span class="pip"></span>
			</div>
		</div>
		
		
	</body>

</html>