grid-template|auto/rows(行)|columns (列)開始學習 Grid 要做的第一件事情就是劃格子,本章主要給大家講解如何畫格子。掌握好這個掌握好這個技能是學習 Grid 布局的基礎。 1. 官方定義grid-template-columns 該屬性是基于 網格列. 的維度,去定義網格線的名稱和網格軌道的尺寸大小。 2. 解釋grid-template-columns 網格的列的寬度,我們可以理解為項目的寬度,這樣更容易學習。 3. 語法grid-template-columns@H_301_98@:none | px | | em| rem | fr | auto| minmax@H_301_98@(min,max@H_301_98@) | auto| repeat@H_301_98@; grid-template-rows@H_301_98@:none | px | | em| rem | fr | auto| minmax@H_301_98@(min,max@H_301_98@) | auto| repeat@H_301_98@; grid-auto-columns@H_301_98@:none | px | | em| rem | fr | auto| minmax@H_301_98@(min,max@H_301_98@) | auto| @H_301_98@; grid-auto-rows@H_301_98@:none | px | | em| rem | fr | auto| minmax@H_301_98@(min,max@H_301_98@) | auto| @H_301_98@; 說明:grid-template-columns 和 grid-template-rows 接受多個值,并且它們可以混合使用。grid-auto-columns 和 grid-auto-rows 接受 1 個值。 函數語法: grid-template-rows@H_301_98@:repeat@H_301_98@(,px rem em,fr@H_301_98@) 說明:repeat的意思是重復,上面的意思每 4 行的高度分別是 10px 1rem 1em,1fr 一共重復 2 次,共 8 行。 grid-template-rows@H_301_98@: px minmax@H_301_98@(px,px@H_301_98@) 說明:minmax 的意思是取最大和最小,上面的意思是第 2 行的高度最小是 40px 最大是 60px. 4. 兼容性
5. 實例
@H_301_98@<div class@H_301_98@=@H_301_98@"demo@H_301_98@"@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"item@H_301_98@"@H_301_98@>1@H_301_98@</div@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"item@H_301_98@"@H_301_98@>2@H_301_98@</div@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"item@H_301_98@"@H_301_98@>3@H_301_98@</div@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"item@H_301_98@"@H_301_98@>4@H_301_98@</div@H_301_98@> @H_301_98@</div@H_301_98@> .demo@H_301_98@{ display@H_301_98@:grid@H_301_98@; grid-template-columns@H_301_98@: none@H_301_98@; grid-template-rows@H_301_98@:none@H_301_98@; grid-auto-columns@H_301_98@: px@H_301_98@; grid-auto-rows@H_301_98@: px@H_301_98@; color@H_301_98@:#fff@H_301_98@; text-align@H_301_98@: center@H_301_98@; @H_301_98@} 效果圖 <!DOCTYPE html> @H_301_98@<html lang@H_301_98@=@H_301_98@"en@H_301_98@"@H_301_98@> @H_301_98@<head@H_301_98@> @H_301_98@<Meta charset@H_301_98@=@H_301_98@"UTF-8@H_301_98@"@H_301_98@> @H_301_98@<Meta name@H_301_98@=@H_301_98@"viewport@H_301_98@" content@H_301_98@=@H_301_98@"width=device-width, initial-scale=1.0@H_301_98@"@H_301_98@> @H_301_98@<title@H_301_98@>Document@H_301_98@</title@H_301_98@> @H_301_98@<style@H_301_98@> .demo@H_301_98@{ display@H_301_98@:grid@H_301_98@; grid-template-columns@H_301_98@: px px@H_301_98@; grid-template-rows@H_301_98@:none@H_301_98@; grid-auto-rows@H_301_98@: px@H_301_98@; color@H_301_98@:#fff@H_301_98@; text-align@H_301_98@: center@H_301_98@; @H_301_98@} .item:nth-of-type(1)@H_301_98@{ background@H_301_98@: red@H_301_98@; @H_301_98@} .item:nth-of-type(2)@H_301_98@{ background@H_301_98@: green@H_301_98@; @H_301_98@} .item:nth-of-type(3)@H_301_98@{ background@H_301_98@: purple@H_301_98@; @H_301_98@} .item:nth-of-type(4)@H_301_98@{ background@H_301_98@: yellowgreen@H_301_98@; @H_301_98@} @H_301_98@</style@H_301_98@> @H_301_98@</head@H_301_98@> @H_301_98@<body@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"demo@H_301_98@"@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"item@H_301_98@"@H_301_98@>1@H_301_98@</div@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"item@H_301_98@"@H_301_98@>2@H_301_98@</div@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"item@H_301_98@"@H_301_98@>3@H_301_98@</div@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"item@H_301_98@"@H_301_98@>4@H_301_98@</div@H_301_98@> @H_301_98@</div@H_301_98@> @H_301_98@</body@H_301_98@> @H_301_98@</html@H_301_98@>
@H_301_98@<div class@H_301_98@=@H_301_98@"demo@H_301_98@"@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"item@H_301_98@"@H_301_98@>1@H_301_98@</div@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"item@H_301_98@"@H_301_98@>2@H_301_98@</div@H_301_98@> @H_301_98@</div@H_301_98@> .demo@H_301_98@{ display@H_301_98@:grid@H_301_98@; grid-template-columns@H_301_98@: px auto@H_301_98@; color@H_301_98@:#fff@H_301_98@; text-align@H_301_98@: center@H_301_98@; @H_301_98@} 效果圖 <!DOCTYPE html> @H_301_98@<html lang@H_301_98@=@H_301_98@"en@H_301_98@"@H_301_98@> @H_301_98@<head@H_301_98@> @H_301_98@<Meta charset@H_301_98@=@H_301_98@"UTF-8@H_301_98@"@H_301_98@> @H_301_98@<Meta name@H_301_98@=@H_301_98@"viewport@H_301_98@" content@H_301_98@=@H_301_98@"width=device-width, initial-scale=1.0@H_301_98@"@H_301_98@> @H_301_98@<title@H_301_98@>Document@H_301_98@</title@H_301_98@> @H_301_98@<style@H_301_98@> .demo@H_301_98@{ display@H_301_98@:grid@H_301_98@; grid-template-columns@H_301_98@: px auto@H_301_98@; color@H_301_98@:#fff@H_301_98@; text-align@H_301_98@: center@H_301_98@; @H_301_98@} .item:nth-of-type(1)@H_301_98@{ background@H_301_98@: red@H_301_98@; @H_301_98@} .item:nth-of-type(2)@H_301_98@{ background@H_301_98@: green@H_301_98@; @H_301_98@} @H_301_98@</style@H_301_98@> @H_301_98@</head@H_301_98@> @H_301_98@<body@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"demo@H_301_98@"@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"item@H_301_98@"@H_301_98@>1@H_301_98@</div@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"item@H_301_98@"@H_301_98@>2@H_301_98@</div@H_301_98@> @H_301_98@</div@H_301_98@> @H_301_98@</body@H_301_98@> @H_301_98@</html@H_301_98@>
@H_301_98@<div class@H_301_98@=@H_301_98@"demo@H_301_98@"@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"item@H_301_98@"@H_301_98@>1@H_301_98@</div@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"item@H_301_98@"@H_301_98@>2@H_301_98@</div@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"item@H_301_98@"@H_301_98@>3@H_301_98@</div@H_301_98@> @H_301_98@</div@H_301_98@> .demo@H_301_98@{ display@H_301_98@:grid@H_301_98@; grid-template-columns@H_301_98@: px auto px@H_301_98@; color@H_301_98@:#fff@H_301_98@; text-align@H_301_98@: center@H_301_98@; @H_301_98@} 效果圖 <!DOCTYPE html> @H_301_98@<html lang@H_301_98@=@H_301_98@"en@H_301_98@"@H_301_98@> @H_301_98@<head@H_301_98@> @H_301_98@<Meta charset@H_301_98@=@H_301_98@"UTF-8@H_301_98@"@H_301_98@> @H_301_98@<Meta name@H_301_98@=@H_301_98@"viewport@H_301_98@" content@H_301_98@=@H_301_98@"width=device-width, initial-scale=1.0@H_301_98@"@H_301_98@> @H_301_98@<title@H_301_98@>Document@H_301_98@</title@H_301_98@> @H_301_98@<style@H_301_98@> .demo@H_301_98@{ display@H_301_98@:grid@H_301_98@; grid-template-columns@H_301_98@: px auto px@H_301_98@; color@H_301_98@:#fff@H_301_98@; text-align@H_301_98@: center@H_301_98@; @H_301_98@} .item:nth-of-type(1)@H_301_98@{ background@H_301_98@: red@H_301_98@; @H_301_98@} .item:nth-of-type(2)@H_301_98@{ background@H_301_98@: green@H_301_98@; @H_301_98@} .item:nth-of-type(3)@H_301_98@{ background@H_301_98@: purple@H_301_98@; @H_301_98@} @H_301_98@</style@H_301_98@> @H_301_98@</head@H_301_98@> @H_301_98@<body@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"demo@H_301_98@"@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"item@H_301_98@"@H_301_98@>1@H_301_98@</div@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"item@H_301_98@"@H_301_98@>2@H_301_98@</div@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"item@H_301_98@"@H_301_98@>3@H_301_98@</div@H_301_98@> @H_301_98@</div@H_301_98@> @H_301_98@</body@H_301_98@> @H_301_98@</html@H_301_98@>
.demo@H_301_98@{ display@H_301_98@:grid@H_301_98@; grid-template-columns@H_301_98@: px auto px@H_301_98@; grid-template-rows@H_301_98@: px@H_301_98@; color@H_301_98@:#fff@H_301_98@; text-align@H_301_98@: center@H_301_98@; @H_301_98@} 效果圖 <!DOCTYPE html> @H_301_98@<html lang@H_301_98@=@H_301_98@"en@H_301_98@"@H_301_98@> @H_301_98@<head@H_301_98@> @H_301_98@<Meta charset@H_301_98@=@H_301_98@"UTF-8@H_301_98@"@H_301_98@> @H_301_98@<Meta name@H_301_98@=@H_301_98@"viewport@H_301_98@" content@H_301_98@=@H_301_98@"width=device-width, initial-scale=1.0@H_301_98@"@H_301_98@> @H_301_98@<title@H_301_98@>Document@H_301_98@</title@H_301_98@> @H_301_98@<style@H_301_98@> .demo@H_301_98@{ display@H_301_98@:grid@H_301_98@; grid-template-columns@H_301_98@: px auto px@H_301_98@; grid-template-rows@H_301_98@:px@H_301_98@; color@H_301_98@:#fff@H_301_98@; text-align@H_301_98@: center@H_301_98@; @H_301_98@} .item:nth-of-type(1)@H_301_98@{ background@H_301_98@: red@H_301_98@; @H_301_98@} .item:nth-of-type(2)@H_301_98@{ background@H_301_98@: green@H_301_98@; @H_301_98@} .item:nth-of-type(3)@H_301_98@{ background@H_301_98@: purple@H_301_98@; @H_301_98@} @H_301_98@</style@H_301_98@> @H_301_98@</head@H_301_98@> @H_301_98@<body@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"demo@H_301_98@"@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"item@H_301_98@"@H_301_98@>1@H_301_98@</div@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"item@H_301_98@"@H_301_98@>2@H_301_98@</div@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"item@H_301_98@"@H_301_98@>3@H_301_98@</div@H_301_98@> @H_301_98@</div@H_301_98@> @H_301_98@</body@H_301_98@> @H_301_98@</html@H_301_98@>
.demo@H_301_98@{ display@H_301_98@:grid@H_301_98@; grid-template-columns@H_301_98@: px px@H_301_98@; grid-template-rows@H_301_98@: px@H_301_98@; color@H_301_98@:#fff@H_301_98@; text-align@H_301_98@: center@H_301_98@; @H_301_98@} 效果圖 <!DOCTYPE html> @H_301_98@<html lang@H_301_98@=@H_301_98@"en@H_301_98@"@H_301_98@> @H_301_98@<head@H_301_98@> @H_301_98@<Meta charset@H_301_98@=@H_301_98@"UTF-8@H_301_98@"@H_301_98@> @H_301_98@<Meta name@H_301_98@=@H_301_98@"viewport@H_301_98@" content@H_301_98@=@H_301_98@"width=device-width, initial-scale=1.0@H_301_98@"@H_301_98@> @H_301_98@<title@H_301_98@>Document@H_301_98@</title@H_301_98@> @H_301_98@<style@H_301_98@> .demo@H_301_98@{ display@H_301_98@:grid@H_301_98@; grid-template-columns@H_301_98@: px px@H_301_98@; grid-template-rows@H_301_98@: px@H_301_98@; color@H_301_98@:#fff@H_301_98@; text-align@H_301_98@: center@H_301_98@; @H_301_98@} .item:nth-of-type(1)@H_301_98@{ background@H_301_98@: red@H_301_98@; @H_301_98@} .item:nth-of-type(2)@H_301_98@{ background@H_301_98@: green@H_301_98@; @H_301_98@} .item:nth-of-type(3)@H_301_98@{ background@H_301_98@: purple@H_301_98@; @H_301_98@} @H_301_98@</style@H_301_98@> @H_301_98@</head@H_301_98@> @H_301_98@<body@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"demo@H_301_98@"@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"item@H_301_98@"@H_301_98@>1@H_301_98@</div@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"item@H_301_98@"@H_301_98@>2@H_301_98@</div@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"item@H_301_98@"@H_301_98@>3@H_301_98@</div@H_301_98@> @H_301_98@</div@H_301_98@> @H_301_98@</body@H_301_98@> @H_301_98@</html@H_301_98@> 說明:我們容器里面有 3 個項目 而只設定了第一行的高度因此,第 2 行的高度是文字撐開的高度。
.demo@H_301_98@{ display@H_301_98@:grid@H_301_98@; grid-template-columns@H_301_98@: px px@H_301_98@; grid-auto-rows@H_301_98@: px@H_301_98@; color@H_301_98@:#fff@H_301_98@; text-align@H_301_98@: center@H_301_98@; @H_301_98@} 效果圖 <!DOCTYPE html> @H_301_98@<html lang@H_301_98@=@H_301_98@"en@H_301_98@"@H_301_98@> @H_301_98@<head@H_301_98@> @H_301_98@<Meta charset@H_301_98@=@H_301_98@"UTF-8@H_301_98@"@H_301_98@> @H_301_98@<Meta name@H_301_98@=@H_301_98@"viewport@H_301_98@" content@H_301_98@=@H_301_98@"width=device-width, initial-scale=1.0@H_301_98@"@H_301_98@> @H_301_98@<title@H_301_98@>Document@H_301_98@</title@H_301_98@> @H_301_98@<style@H_301_98@> .demo@H_301_98@{ display@H_301_98@:grid@H_301_98@; grid-template-columns@H_301_98@: px px@H_301_98@; grid-auto-rows@H_301_98@:px@H_301_98@; color@H_301_98@:#fff@H_301_98@; text-align@H_301_98@: center@H_301_98@; @H_301_98@} .item:nth-of-type(1)@H_301_98@{ background@H_301_98@: red@H_301_98@; @H_301_98@} .item:nth-of-type(2)@H_301_98@{ background@H_301_98@: green@H_301_98@; @H_301_98@} .item:nth-of-type(3)@H_301_98@{ background@H_301_98@: purple@H_301_98@; @H_301_98@} @H_301_98@</style@H_301_98@> @H_301_98@</head@H_301_98@> @H_301_98@<body@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"demo@H_301_98@"@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"item@H_301_98@"@H_301_98@>1@H_301_98@</div@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"item@H_301_98@"@H_301_98@>2@H_301_98@</div@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"item@H_301_98@"@H_301_98@>3@H_301_98@</div@H_301_98@> @H_301_98@</div@H_301_98@> @H_301_98@</body@H_301_98@> @H_301_98@</html@H_301_98@>
.demo@H_301_98@{ display@H_301_98@:grid@H_301_98@; grid-template-columns@H_301_98@: px minmax@H_301_98@(px,px@H_301_98@)@H_301_98@; grid-auto-rows@H_301_98@: px@H_301_98@; color@H_301_98@:#fff@H_301_98@; text-align@H_301_98@: center@H_301_98@; @H_301_98@} 效果圖 <!DOCTYPE html> @H_301_98@<html lang@H_301_98@=@H_301_98@"en@H_301_98@"@H_301_98@> @H_301_98@<head@H_301_98@> @H_301_98@<Meta charset@H_301_98@=@H_301_98@"UTF-8@H_301_98@"@H_301_98@> @H_301_98@<Meta name@H_301_98@=@H_301_98@"viewport@H_301_98@" content@H_301_98@=@H_301_98@"width=device-width, initial-scale=1.0@H_301_98@"@H_301_98@> @H_301_98@<title@H_301_98@>Document@H_301_98@</title@H_301_98@> @H_301_98@<style@H_301_98@> .demo@H_301_98@{ display@H_301_98@:grid@H_301_98@; grid-template-columns@H_301_98@: px minmax@H_301_98@(px,px@H_301_98@)@H_301_98@; grid-auto-rows@H_301_98@:px@H_301_98@; color@H_301_98@:#fff@H_301_98@; text-align@H_301_98@: center@H_301_98@; @H_301_98@} .item:nth-of-type(1)@H_301_98@{ background@H_301_98@: red@H_301_98@; @H_301_98@} .item:nth-of-type(2)@H_301_98@{ background@H_301_98@: green@H_301_98@; @H_301_98@} .item:nth-of-type(3)@H_301_98@{ background@H_301_98@: purple@H_301_98@; @H_301_98@} @H_301_98@</style@H_301_98@> @H_301_98@</head@H_301_98@> @H_301_98@<body@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"demo@H_301_98@"@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"item@H_301_98@"@H_301_98@>1@H_301_98@</div@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"item@H_301_98@"@H_301_98@>2@H_301_98@</div@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"item@H_301_98@"@H_301_98@>3@H_301_98@</div@H_301_98@> @H_301_98@</div@H_301_98@> @H_301_98@</body@H_301_98@> @H_301_98@</html@H_301_98@>
.demo@H_301_98@{ display@H_301_98@:grid@H_301_98@; grid-template-columns@H_301_98@: fr fr fr@H_301_98@; grid-auto-rows@H_301_98@: px@H_301_98@; color@H_301_98@:#fff@H_301_98@; text-align@H_301_98@: center@H_301_98@; @H_301_98@} 效果圖 也可以用小數。 .demo@H_301_98@{ display@H_301_98@:grid@H_301_98@; grid-template-columns@H_301_98@: fr fr fr@H_301_98@; grid-auto-rows@H_301_98@: px@H_301_98@; color@H_301_98@:#fff@H_301_98@; text-align@H_301_98@: center@H_301_98@; @H_301_98@} 效果圖 <!DOCTYPE html> @H_301_98@<html lang@H_301_98@=@H_301_98@"en@H_301_98@"@H_301_98@> @H_301_98@<head@H_301_98@> @H_301_98@<Meta charset@H_301_98@=@H_301_98@"UTF-8@H_301_98@"@H_301_98@> @H_301_98@<Meta name@H_301_98@=@H_301_98@"viewport@H_301_98@" content@H_301_98@=@H_301_98@"width=device-width, initial-scale=1.0@H_301_98@"@H_301_98@> @H_301_98@<title@H_301_98@>Document@H_301_98@</title@H_301_98@> @H_301_98@<style@H_301_98@> .demo@H_301_98@{ display@H_301_98@:grid@H_301_98@; grid-template-columns@H_301_98@: fr fr fr@H_301_98@; grid-auto-rows@H_301_98@:px@H_301_98@; color@H_301_98@:#fff@H_301_98@; text-align@H_301_98@: center@H_301_98@; @H_301_98@} .item:nth-of-type(1)@H_301_98@{ background@H_301_98@: red@H_301_98@; @H_301_98@} .item:nth-of-type(2)@H_301_98@{ background@H_301_98@: green@H_301_98@; @H_301_98@} .item:nth-of-type(3)@H_301_98@{ background@H_301_98@: purple@H_301_98@; @H_301_98@} @H_301_98@</style@H_301_98@> @H_301_98@</head@H_301_98@> @H_301_98@<body@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"demo@H_301_98@"@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"item@H_301_98@"@H_301_98@>1@H_301_98@</div@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"item@H_301_98@"@H_301_98@>2@H_301_98@</div@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"item@H_301_98@"@H_301_98@>3@H_301_98@</div@H_301_98@> @H_301_98@</div@H_301_98@> @H_301_98@</body@H_301_98@> @H_301_98@</html@H_301_98@>
.demo@H_301_98@{ display@H_301_98@:grid@H_301_98@; grid-template-columns@H_301_98@: repeat@H_301_98@(,px@H_301_98@)@H_301_98@; grid-auto-rows@H_301_98@:px@H_301_98@; color@H_301_98@:#fff@H_301_98@; text-align@H_301_98@: center@H_301_98@; @H_301_98@} 效果圖 <!DOCTYPE html> @H_301_98@<html lang@H_301_98@=@H_301_98@"en@H_301_98@"@H_301_98@> @H_301_98@<head@H_301_98@> @H_301_98@<Meta charset@H_301_98@=@H_301_98@"UTF-8@H_301_98@"@H_301_98@> @H_301_98@<Meta name@H_301_98@=@H_301_98@"viewport@H_301_98@" content@H_301_98@=@H_301_98@"width=device-width, initial-scale=1.0@H_301_98@"@H_301_98@> @H_301_98@<title@H_301_98@>Document@H_301_98@</title@H_301_98@> @H_301_98@<style@H_301_98@> .demo@H_301_98@{ display@H_301_98@:grid@H_301_98@; grid-template-columns@H_301_98@: repeat@H_301_98@(,px@H_301_98@)@H_301_98@; grid-auto-rows@H_301_98@:px@H_301_98@; color@H_301_98@:#fff@H_301_98@; text-align@H_301_98@: center@H_301_98@; @H_301_98@} .item:nth-of-type(1)@H_301_98@{ background@H_301_98@: red@H_301_98@; @H_301_98@} .item:nth-of-type(2)@H_301_98@{ background@H_301_98@: green@H_301_98@; @H_301_98@} .item:nth-of-type(3)@H_301_98@{ background@H_301_98@: purple@H_301_98@; @H_301_98@} @H_301_98@</style@H_301_98@> @H_301_98@</head@H_301_98@> @H_301_98@<body@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"demo@H_301_98@"@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"item@H_301_98@"@H_301_98@>1@H_301_98@</div@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"item@H_301_98@"@H_301_98@>2@H_301_98@</div@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"item@H_301_98@"@H_301_98@>3@H_301_98@</div@H_301_98@> @H_301_98@</div@H_301_98@> @H_301_98@</body@H_301_98@> @H_301_98@</html@H_301_98@>
.demo@H_301_98@{ display@H_301_98@:grid@H_301_98@; grid-template-columns@H_301_98@: repeat@H_301_98@(auto-fill,px@H_301_98@)@H_301_98@; grid-auto-rows@H_301_98@:px@H_301_98@; color@H_301_98@:#fff@H_301_98@; text-align@H_301_98@: center@H_301_98@; @H_301_98@} 效果圖 <!DOCTYPE html> @H_301_98@<html lang@H_301_98@=@H_301_98@"en@H_301_98@"@H_301_98@> @H_301_98@<head@H_301_98@> @H_301_98@<Meta charset@H_301_98@=@H_301_98@"UTF-8@H_301_98@"@H_301_98@> @H_301_98@<Meta name@H_301_98@=@H_301_98@"viewport@H_301_98@" content@H_301_98@=@H_301_98@"width=device-width, initial-scale=1.0@H_301_98@"@H_301_98@> @H_301_98@<title@H_301_98@>Document@H_301_98@</title@H_301_98@> @H_301_98@<style@H_301_98@> .demo@H_301_98@{ display@H_301_98@:grid@H_301_98@; grid-template-columns@H_301_98@: repeat@H_301_98@(auto-fill,px@H_301_98@)@H_301_98@; grid-auto-rows@H_301_98@:px@H_301_98@; color@H_301_98@:#fff@H_301_98@; text-align@H_301_98@: center@H_301_98@; @H_301_98@} .item:nth-of-type(1)@H_301_98@{ background@H_301_98@: red@H_301_98@; @H_301_98@} .item:nth-of-type(2)@H_301_98@{ background@H_301_98@: green@H_301_98@; @H_301_98@} .item:nth-of-type(3)@H_301_98@{ background@H_301_98@: purple@H_301_98@; @H_301_98@} @H_301_98@</style@H_301_98@> @H_301_98@</head@H_301_98@> @H_301_98@<body@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"demo@H_301_98@"@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"item@H_301_98@"@H_301_98@>1@H_301_98@</div@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"item@H_301_98@"@H_301_98@>2@H_301_98@</div@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"item@H_301_98@"@H_301_98@>3@H_301_98@</div@H_301_98@> @H_301_98@</div@H_301_98@> @H_301_98@</body@H_301_98@> @H_301_98@</html@H_301_98@>
.demo@H_301_98@{ display@H_301_98@:grid@H_301_98@; grid-template-columns@H_301_98@: repeat@H_301_98@(auto-fit,px@H_301_98@)@H_301_98@; grid-auto-rows@H_301_98@:px@H_301_98@; color@H_301_98@:#fff@H_301_98@; text-align@H_301_98@: center@H_301_98@; @H_301_98@} 效果圖 <!DOCTYPE html> @H_301_98@<html lang@H_301_98@=@H_301_98@"en@H_301_98@"@H_301_98@> @H_301_98@<head@H_301_98@> @H_301_98@<Meta charset@H_301_98@=@H_301_98@"UTF-8@H_301_98@"@H_301_98@> @H_301_98@<Meta name@H_301_98@=@H_301_98@"viewport@H_301_98@" content@H_301_98@=@H_301_98@"width=device-width, initial-scale=1.0@H_301_98@"@H_301_98@> @H_301_98@<title@H_301_98@>Document@H_301_98@</title@H_301_98@> @H_301_98@<style@H_301_98@> .demo@H_301_98@{ display@H_301_98@:grid@H_301_98@; grid-template-columns@H_301_98@: repeat@H_301_98@(auto-fit,px@H_301_98@)@H_301_98@; grid-auto-rows@H_301_98@:px@H_301_98@; color@H_301_98@:#fff@H_301_98@; text-align@H_301_98@: center@H_301_98@; @H_301_98@} .item:nth-of-type(1)@H_301_98@{ background@H_301_98@: red@H_301_98@; @H_301_98@} .item:nth-of-type(2)@H_301_98@{ background@H_301_98@: green@H_301_98@; @H_301_98@} .item:nth-of-type(3)@H_301_98@{ background@H_301_98@: purple@H_301_98@; @H_301_98@} @H_301_98@</style@H_301_98@> @H_301_98@</head@H_301_98@> @H_301_98@<body@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"demo@H_301_98@"@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"item@H_301_98@"@H_301_98@>1@H_301_98@</div@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"item@H_301_98@"@H_301_98@>2@H_301_98@</div@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"item@H_301_98@"@H_301_98@>3@H_301_98@</div@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"item@H_301_98@"@H_301_98@>3@H_301_98@</div@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"item@H_301_98@"@H_301_98@>3@H_301_98@</div@H_301_98@> @H_301_98@<div class@H_301_98@=@H_301_98@"item@H_301_98@"@H_301_98@>3@H_301_98@</div@H_301_98@> @H_301_98@</div@H_301_98@> @H_301_98@</body@H_301_98@> @H_301_98@</html@H_301_98@> 小結
.demo@H_301_98@{ grid-template-columns@H_301_98@: fr fr px rem@H_301_98@; @H_301_98@} 3 minmax() 中的值也可以使用 fr,例如: .demo@H_301_98@{ grid-template-columns@H_301_98@: minmax@H_301_98@(fr,fr@H_301_98@)@H_301_98@; @H_301_98@} 它們的規則是一個范圍,左邊是最小值,右側是最大值。
repeat() 和 minmax() 一起使用: .demo@H_301_98@{ grid-template-columns@H_301_98@:repeat@H_301_98@(,minmax@H_301_98@(px,px@H_301_98@) px px@H_301_98@)@H_301_98@; @H_301_98@} |