CSS μμ± 17. grid
βοΈ ν΄λΉ κΈμ ν¨μ€νΈμΊ νΌμ€ - νλ‘ νΈμλ κ°λ° κ°μμμ HTML & CSS, SASS(SCSS) Partμ λ°μμ κ°μ¬λμ κ°μμλ£λ₯Ό μ 리ν κ²μ λλ€.
CSS grid (그리λ)λ 2μ°¨μ(νκ³Ό μ΄)μ λ μ΄μμ μμ€ν
μ μ 곡νλ€.
flexible box λ λΉκ΅μ λ¨μν 1μ°¨μ λ μ΄μμμ μνμ¬ μ¬μ©λκ³ , μ’ λ 볡μ‘νλ μ΄μμμ μν΄μλ grid λ₯Ό μ¬μ©ν μ μλ€.
π‘ CSS grid λ μμ λΆν° ν΅(Hack)μΌλ‘ λΆλ¦° λ€μν λ μ΄μμ λ체 λ°©μλ€μ ν΄κ²°νκΈ° μν΄ λ§λ€μ΄μ§ νΉλ³ν CSS λͺ¨λμ΄λ€.
CSS Grid#
βοΈ grid μ ν¨μ¨μ μΈ νμ΅μ μν΄μλ νμ΄μ΄νμ€ λΈλΌμ°μ κ° μ’λ€.
grid λ flex μ λ§μ°¬κ°μ§λ‘ container μ item μ΄λΌλ λ κ°μ§ κ°λ
μΌλ‘ λλλ€. container λ items λ₯Ό κ°μΈλ λΆλͺ¨ μμμ΄κ³ κ·Έ μμ κ° item μ λ°°μΉν μ μλ€.
grid container#
μμ±#
| μμ± | μλ―Έ |
|---|---|
| display | grid container λ₯Ό μ μ |
| grid-template-rows | λͺ μμ ν(Track)μ ν¬κΈ°λ₯Ό μ μ |
| grid-template-columns | λͺ μμ μ΄(Track)μ ν¬κΈ°λ₯Ό μ μ |
| grid-template-areas | μμ(Area) μ΄λ¦μ μ°Έμ‘°ν΄ ν νλ¦Ώ μμ± |
| grid-template | grid-template-xxx μ λ¨μΆ μμ± |
| row-gap(grid-row-gap) | νκ³Ό ν μ¬μ΄μ κ°κ²©(Line)μ μ μ |
| column-gap(grid-column-gap) | μ΄κ³Ό μ΄ μ¬μ΄μ κ°κ²©(Line)μ μ μ |
| gap(grid-gap) | xxx-gap μ λ¨μΆ μμ± |
| grid-auto-rows | μμμ μΈ ν(Track)μ ν¬κΈ°λ₯Ό μ μ |
| grid-auto-columns | μμμ μΈ μ΄(Track)μ ν¬κΈ°λ₯Ό μ μ |
| grid-auto-flow | μλ λ°°μΉ μκ³ λ¦¬μ¦ λ°©μμ μ μ |
| grid | grid-template-xxx μ grid-auto-xxx μ λ¨μΆ μμ± |
| align-content | 그리λ μ½ν μΈ (Grid Contents)λ₯Ό μμ§(μ΄ μΆ) μ λ ¬ |
| justify-content | 그리λ μ½ν μΈ (Grid Contents)λ₯Ό μν(ν μΆ) μ λ ¬ |
| place-content | align-content μ justify-content μ λ¨μΆ μμ± |
| align-items | 그리λ μμ΄ν (item)λ€μ μμ§(μ΄ μΆ) μ λ ¬ |
| justify-items | 그리λ μμ΄ν (item)λ€μ μν(ν μΆ) μ λ ¬ |
| place-items | align-items μ justify-items μ λ¨μΆ μμ± |
display#
display μμ±μΌλ‘ grid container λ₯Ό μ μνλ€.
μ μλ 컨ν
μ΄λμ μμ μμλ€μ μλμΌλ‘ grid item λ‘ μ μλλ€.
μμ± κ°#
| κ° | μλ―Έ |
|---|---|
| grid | block νΉμ±μ grid container λ₯Ό μ μ |
| inline-grid | inline νΉμ±μ grid container λ₯Ό μ μ |
βοΈ flex , inline-flex μ λ§μ°¬κ°μ§λ‘ gird container μ μμ΄λ λ°©μμ μ μνλ©°, λ΄λΆμ μμ΄λ λ°©μμ λμ΄ μμ ν κ°λ€.
grid-template-rows , grid-template-columns#
λͺ μμ ν, μ΄(Track)μ ν¬κΈ°λ₯Ό μ μ
λμμ λΌμΈ(Line)μ μ΄λ¦λ μ μν μ μκ³ , fr (fraction, κ³΅κ° λΉμ¨) λ¨μλ₯Ό μ¬μ©ν μ μλ€.
μ¬μ©λ²#
grid-template-rows: 1νν¬κΈ° 2νν¬κΈ° ...;grid-template-rows: [μ μ΄λ¦] 1νν¬κΈ° [μ μ΄λ¦] 2νν¬κΈ° [μ μ΄λ¦] ...;grid-template-columns: 1μ΄ν¬κΈ° 2μ΄ν¬κΈ° ...;grid-template-columns: [μ μ΄λ¦] 1μ΄ν¬κΈ° [μ μ΄λ¦] 2μ΄ν¬κΈ° [μ μ΄λ¦] ...;/* κ° νκ³Ό μ΄μ ν¬κΈ°λ₯Ό μ μν©λλ€. */.container {grid-template-rows: 100px 200px;grid-template-columns: 100px 200px;}/* λμμ κ° λΌμΈμ μ΄λ¦λ μ μν μ μμ΅λλ€. */.container {grid-template-rows: [first] 100px [second] 200px [third];grid-template-columns: [first] 100px [second] 200px [third];}/* λΌμΈμ μ€λ³΅λ μ΄λ¦μ μ§μ ν μ μμ΅λλ€. */.container {grid-template-rows: [row1-start] 100px [row1-end row2-start] 200px [row2-end];grid-template-columns: [col1-start] 100px [col1-end col2-start] 200px [col2-end];}그리λ λΌμΈμ μ΄λ¦ μ§κΈ° - Layout using named grid lines
μμ #
<div class="container"><div class="item">1</div><div class="item">2</div><div class="item">3</div><div class="item">4</div><div class="item">5</div><div class="item">6</div></div>.container {display: grid;grid-template-rows: 100px 100px;grid-template-columns: repeat(3, 1fr);border: 4px solid lightgray;}.item {border: 2px dashed red;}βοΈ px λ¨μλ‘ κ°μ μ λ ₯ν μλ μμ§λ§ fr λ¨μλ₯Ό μ λ ₯νλ©΄ container μ λλΉκ° κ°λ³ν λ ν, μ΄μ ν¬κΈ°κ° κ°λ³νκ² λλ€.βοΈ repeat() ν¨μλ 2λ²μ§Έ μΈμλ₯Ό 1λ²μ§Έ μΈμλ§νΌ λ°λ³΅νλ€.
grid-template-areas#
μ§μ λ 그리λ μμ μ΄λ¦( grid-area )μ μ°Έμ‘°ν΄ κ·Έλ¦¬λ ν
νλ¦Ώμ μμ±
βοΈ grid-area λ grid container κ° μλ grid item μ μ μ©νλ μμ±μ΄λ€.
μμ #
<div class="container"><div class="item">Header</div><div class="item">Main</div><div class="item">Aside</div><div class="item">Footer</div></div>.container {width: 600px;display: grid;grid-template-rows: repeat(3, 100px);grid-template-columns: repeat(3, 1fr);grid-template-areas:"header header header""main main aside""footer footer footer";}.item {border: 10px solid red;}.item:nth-child(1) {grid-area: header;}.item:nth-child(2) {grid-area: main;}.item:nth-child(3) {grid-area: aside;}.item:nth-child(4) {grid-area: footer;}
grid-template-areas-1
βοΈ . (λ§μΉ¨ν)λ₯Ό μ¬μ©νκ±°λ λͺ μμ μΌλ‘ none μ μ λ ₯ν΄ λΉ μμμ μ μν μ μλ€.
grid-template#
grid-template-rows , grid-template-columns , grid-template-areas μ λ¨μΆμμ±
μ¬μ©λ²#
.container {grid-template: <grid-template-rows> / <grid-template-columns>;grid-template: <grid-template-areas>;}λλ
.container {grid-template:[1νμμμ μ΄λ¦] "AREAS" νλλΉ [1νλμ μ΄λ¦][2νμμμ μ΄λ¦] "AREAS" νλλΉ [2νλμ μ΄λ¦]/ <grid-template-columns>;}μμ #
<div class="container"><header>HEADER</header><main>MAIN</main><aside>ASIDE</aside><footer>FOOTER</footer></div>.container {display: grid;grid-template:"header header header" 80px"main main aside" 350px"footer footer footer" 130px/ 2fr 100px 1fr;}.container > * {border: 10px solid red;}header { grid-area: header; }main { grid-area: main; }aside { grid-area: aside; }footer { grid-area: footer; }row-gap(grid-row-gap) , column-gap(grid-column-gap) , gap(grid-gap)#
κ° νκ³Ό ν μ¬μ΄, μ΄κ³Ό μ΄ μ¬μ΄μ κ°κ²©(Gutter)μ μ§μ
λ λͺ ννκ² κ·Έλ¦¬λ μ (Grid Line)μ ν¬κΈ°λ₯Ό μ§μ
gap μ row-gap κ³Ό column-gap μ λ¨μΆ μμ±μ΄λ€.
μ¬μ©λ²#
.container {row-gap: ν¬κΈ°;column-gap: ν¬κΈ°;gap: <grid-row-gap> <grid-column-gap>;}.container {display: grid;grid-template-rows: repeat(2, 150px);grid-template-columns: repeat(3, 1fr);gap: 20px 10px;}/* νλμ κ°μΌλ‘ ν΅μΌν μ μμ΅λλ€. */.container {gap: 10px; /* row-gap: 10px; + column-gap: 10px; */}/* νλμ κ°λ§ μ μ©νκ³ μ νλ€λ©΄ λ€μκ³Ό κ°μ΄ μ¬μ©ν μ μμ΅λλ€. */.container {gap: 10px 0; /* row-gap */gap: 0 10px; /* column-gap */}βοΈ μ λμ¬ grid- λ λ μ΄μ μ¬μ©λμ§ μμ§λ§, μΌλΆ λ²μ μ λΈλΌμ°μ μ§μμ μν΄μ λμ¬μ μ¬μ©μ κ³ λ €ν μ μλ€.
grid-auto-rows , grid-auto-columns#
μμμ ν, μ΄(Track)μ ν¬κΈ°λ₯Ό μ μ
item μ΄ grid-template-rows λ grid-template-columns λ‘ μ μν λͺ
μμ ν, μ΄ μΈλΆμ λ°°μΉλλ κ²½μ° μμμ ν, μ΄μ ν¬κΈ°κ° μ μ©λλ€.
μμ #
<div class="container"><div class="item">1</div><div class="item">2</div><div class="item">3</div><div class="item">4</div><div class="item">5</div><div class="item">6</div></div>.container {width: 600px;display: grid;grid-template-rows: repeat(2, 100px); /* λͺ
μμ ν */grid-template-columns: repeat(3, 1fr); /* λͺ
μμ μ΄ */grid-auto-rows: 100px; /* μμμ ν */grid-auto-columns: 100px; /* μμμ μ΄ */}.item {border: 10px solid red;}.item:nth-child(1) {grid-column: 1 / 3;}.item:nth-child(2) {grid-column: 3 / 5;}.item:nth-child(3) {grid-column: span 2;}.item:nth-child(4) {grid-row: 5 / 7;grid-column: 6 / 7;}βοΈ μμμ ν¬κΈ°κ° μ μ©λ νκ³Ό μ΄μ μμ λΌμΈ λ²νΈλ§ μ¬μ©ν μ μκ³ , μμλ μ¬μ©ν μ μλ€.
grid-auto-flow#
λ°°μΉνμ§ μμ item μ μ΄λ€ λ°©μμ βμλ λ°°μΉ μκ³ λ¦¬μ¦βμΌλ‘ μ²λ¦¬ν μ§ μ μ
λ°°μΉν item μ grid-area (μ΄ν κ°λ³ μμ± ν¬ν¨)λ₯Ό μ¬μ©ν item
μμ±κ°#
| κ° | μλ―Έ | κΈ°λ³Έκ° |
|---|---|---|
| row | κ° ν μΆμ λ°λΌ μ°¨λ‘λ‘ λ°°μΉ | row |
| column | κ° μ΄ μΆμ λ°λΌ μ°¨λ‘λ‘ λ°°μΉ | |
| row dense(dense) | κ° ν μΆμ λ°λΌ μ°¨λ‘λ‘ λ°°μΉ, λΉ μμ λ©μ! | |
| column dense | κ° μ΄ μΆμ λ°λΌ μ°¨λ‘λ‘ λ°°μΉ, λΉ μμ λ©μ! |

grid-auto-flow-1

grid-auto-flow-2
μμ #
<div class="container"><div class="item">1</div><div class="item">2</div><div class="item">3</div><div class="item">4</div></div>.container {width: 600px;display: grid;grid-template-rows: repeat(3, 100px);grid-template-columns: repeat(3, 1fr);grid-auto-flow: column;}.item {border: 10px solid red;}.item:nth-child(1) {grid-column: span 2;}.item:nth-child(2) {grid-column: -1 / -3;}grid#
grid-template-xxx μ grid-auto-xxx μ λ¨μΆ μμ±
μ¬μ©λ²#
.container {grid: <grid-template>;grid: <grid-template-rows> / <grid-auto-flow> <grid-auto-columns>;grid: <grid-auto-flow> <grid-auto-rows> / <grid-template-columns>;}grid-template-rows&grid-template-columnsμ€μ grid-template-rows&grid-auto-flow: column;&grid-auto-columnsμ€μ grid-auto-flow: row;&grid-auto-rows&grid-template-columnsμ€μ grid-templateμ€μ
βοΈ grid-auto-flow λ₯Ό μμ±ν λλ auto-flow ν€μλλ₯Ό μ¬μ©νλλ°, / λ‘ κ΅¬λΆν΄ μμ±νλ μμΉκ° κ³§ row , column κ°μ μλ―Ένλ€. dense κ°μ auto-flow λ€μ λΆμ¬μ£Όλ©΄ λλ€.
align-content#
그리λ μ½ν μΈ (contents)λ₯Ό μμ§(μ΄ μΆ) μ λ ¬νλ€.
그리λ μ½ν
μΈ μ μΈλ‘ λλΉκ° 그리λ container λ³΄λ€ μμμΌ νλ€.
μμ± κ°#
| κ° | μλ―Έ | κΈ°λ³Έκ° |
|---|---|---|
| normal | stretch μ κ°λ€ | normal |
| start | μμμ (μμͺ½) μ λ ¬ | |
| center | μμ§ κ°μ΄λ° μ λ ¬ | |
| end | λμ (μλμͺ½) μ λ ¬ | |
| space-around | κ° ν μμλμ μ¬λ°±μ κ³ λ₯΄κ² μ λ ¬ | |
| space-between | 첫 νμ μμμ μ, λ νμ λμ μ μ λ ¬λκ³ λλ¨Έμ§ μ¬λ°±μΌλ‘ κ³ λ₯΄κ² μ λ ¬ | |
| space-evenly | λͺ¨λ μ¬λ°±μ κ³ λ₯΄κ² μ λ ¬ | |
| stretch | μ΄ μΆμ μ±μ°κΈ° μν΄ κ·Έλ¦¬λ μ½ν μΈ λ₯Ό λλ¦Ό |

align-content
justify-content#
그리λ μ½ν μΈ (contents)λ₯Ό μν(ν μΆ) μ λ ¬νλ€.
그리λ μ½ν
μΈ μ κ°λ‘ λλΉκ° 그리λ container λ³΄λ€ μμμΌ νλ€.
μμ± κ°#
| κ° | μλ―Έ | κΈ°λ³Έκ° |
|---|---|---|
| normal | stretchμ κ°λ€ | normal |
| start | μμμ (μΌμͺ½) μ λ ¬ | |
| center | μν κ°μ΄λ° μ λ ¬ | |
| end | λμ (μ€λ₯Έμͺ½) μ λ ¬ | |
| space-around | κ° μ΄ μ’μ°μ μ¬λ°±μ κ³ λ₯΄κ² μ λ ¬ | |
| space-between | 첫 μ΄μ μμμ μ, λ μ΄μ λμ μ μ λ ¬λκ³ λλ¨Έμ§ μ¬λ°±μΌλ‘ κ³ λ₯΄κ² μ λ ¬ | |
| space-evenly | λͺ¨λ μ¬λ°±μ κ³ λ₯΄κ² μ λ ¬ | |
| stretch | ν μΆμ μ±μ°κΈ° μν΄ κ·Έλ¦¬λ μ½ν μΈ λ₯Ό λλ¦Ό |

justify-content
align-items#
그리λ Item λ€μ μμ§(μ΄ μΆ) μ λ ¬νλ€. 그리λ Item μ μΈλ‘ λλΉκ° μμ μ΄ μν 그리λ ν(Track)μ ν¬κΈ°λ³΄λ€ μμμΌ νλ€.
| κ° | μλ―Έ | κΈ°λ³Έκ° |
|---|---|---|
| normal | stretchμ κ°λ€ | normal |
| start | μμμ (μμͺ½) μ λ ¬ | |
| center | μμ§ κ°μ΄λ° μ λ ¬ | |
| end | λμ (μλμͺ½) μ λ ¬ | |
| stretch | μ΄ μΆμ μ±μ°κΈ° μν΄ κ·Έλ¦¬λ μμ΄ν μ λλ¦Ό |

align-items
justify-items#
그리λ Item λ€μ μν(ν μΆ) μ λ ¬νλ€. 그리λ Item μ κ°λ‘ λλΉκ° μμ μ΄ μν 그리λ μ΄(Track)μ ν¬κΈ°λ³΄λ€ μμμΌ νλ€.
| κ° | μλ―Έ | κΈ°λ³Έκ° |
|---|---|---|
| normal | stretchμ κ°λ€. | normal |
| start | μμμ (μΌμͺ½) μ λ ¬ | |
| center | μν κ°μ΄λ° μ λ ¬ | |
| end | λμ (μ€λ₯Έμͺ½) μ λ ¬ | |
| stretch | ν μΆμ μ±μ°κΈ° μν΄ κ·Έλ¦¬λ μμ΄ν μ λλ¦Ό |

justify-items
grid Item#
μμ±#
| μμ± | μλ―Έ |
|---|---|
| grid-row-start | 그리λ μμ΄ν (Item)μ ν μμ μμΉ μ§μ |
| grid-row-end | 그리λ μμ΄ν μ ν λ μμΉ μ§μ |
| grid-row | grid-row-xxxμ λ¨μΆ μμ±(ν μμ/λ μμΉ) |
| grid-column-start | 그리λ μμ΄ν μ μ΄ μμ μμΉ μ§μ |
| grid-column-end | 그리λ μμ΄ν μ μ΄ λ μμΉ μ§μ |
| grid-column | grid-column-xxxμ λ¨μΆ μμ±(μ΄ μμ/λ μμΉ) |
| grid-area | μμ(Area) μ΄λ¦μ μ€μ νκ±°λ, grid-rowμ grid-columnμ λ¨μΆ μμ± |
| align-self | λ¨μΌ 그리λ μμ΄ν μ μμ§(μ΄ μΆ) μ λ ¬ |
| justify-self | λ¨μΌ 그리λ μμ΄ν μ μν(ν μΆ) μ λ ¬ |
| place-self | align-selfμ justify-selfμ λ¨μΆ μμ± |
| order | 그리λ μμ΄ν μ λ°°μΉ μμλ₯Ό μ§μ |
| z-index | 그리λ μμ΄ν μ μμ΄λ μμλ₯Ό μ§μ |
grid-row-start , grid-column-start , grid-row-end , grid-column-end#
item μ λ°°μΉνκΈ° μν΄ μ (Line)μ βμμ μμΉβμ βλ μμΉβλ₯Ό μ§μ νλ€.
βμ«μβλ₯Ό μ§μ νκ±°λ βμ μ΄λ¦βμ μ§μ νκ±°λ, span ν€μλλ₯Ό μ¬μ©νλ€.
μ¬μ©λ²#
.item {grid-row-start: 1;grid-row-end: 3;grid-column-start: 2;grid-column-end: 4;}βοΈ span ν€μλμ βμ«μβλ₯Ό μ‘°ν©νλ©΄ βμ«μβλ§νΌ λΌμΈμ νμ₯νλ κ°λ μΈλ°, λ§μ½ β λ μμΉβλ₯Ό μ§μ ν΄λκ³ , βμμ μμΉβμ span + βμ«μβλ₯Ό μ¬μ©νλ©΄ βλ μμΉβκΉμ§ κ°κΈ° μ span μ«μλ§νΌ νμ₯νλ€.
grid-row , grid-column#
item μ μνλ μμΉμ λκΈ° μν΄ μ¬μ©νλ μμ±
κ°κ° grid-row-start, grid-row-end ,grid-column-start, grid-column-end μλ¨μΆ μμ±μ΄λ€.
κ° μμ±μ / λ‘ κ΅¬λΆνλ€.
span ν€μλλ₯Ό μ¬μ©ν΄ βμ«μβλ₯Ό μ§μ νκ±°λ βμ μ΄λ¦βμ μ§μ ν΄ μ¬μ©ν μ μλ€.
μ¬μ©λ²#
.item {grid-row: <grid-row-start> / <grid-row-end>;grid-column: <grid-column-start> / <grid-column-end>;}grid-area#
grid-row-start , grid-column-start , grid-row-end , grid-column-end μλ¨μΆ μμ±μΌλ‘ μ¬μ©ν μλ μκ³ , grid-template-areas κ° μ°Έμ‘°ν μμ(Area) μ΄λ¦μμ€μ ν μλ μλ€.
μμ μ΄λ¦μ μ€μ ν κ²½μ° grid-row , grid-column μ κ°λ
μ 무μλλ€.
μ¬μ©λ²#
.item {grid-area: <grid-row-start> / <grid-column-start> / <grid-row-end> / <grid-column-end>;grid-area: μμμ΄λ¦;}βοΈ λ¨μΆ μμ±μ μμμ μ μνλ€.
align-self#
λ¨μΌ 그리λ Item μ μμ§(μ΄ μΆ) μ λ ¬. 그리λ Item μ μΈλ‘ λλΉκ° μμ μ΄ μν그리λ ν(Track)μ ν¬κΈ°λ³΄λ€ μμμΌ νλ€.
| κ° | μλ―Έ | κΈ°λ³Έκ° |
|---|---|---|
| normal | stretchμ κ°λ€ | normal |
| start | μμμ (μμͺ½) μ λ ¬ | |
| center | μμ§ κ°μ΄λ° μ λ ¬ | |
| end | λμ (μλμͺ½) μ λ ¬ | |
| stretch | μ΄ μΆμ μ±μ°κΈ° μν΄ κ·Έλ¦¬λ μμ΄ν μ λλ¦Ό |

align-self
justify-self#
λ¨μΌ 그리λ Item μ μν(ν μΆ) μ λ ¬νλ€. 그리λ Item μ κ°λ‘ λλΉκ° μμ μ΄μν 그리λ μ΄(Track)μ ν¬κΈ°λ³΄λ€ μμμΌ νλ€.
| κ° | μλ―Έ | κΈ°λ³Έκ° |
|---|---|---|
| normal | stretchμ κ°λ€ | normal |
| start | μμμ (μΌμͺ½) μ λ ¬ | |
| center | μν κ°μ΄λ° μ λ ¬ | |
| end | λμ (μ€λ₯Έμͺ½) μ λ ¬ | |
| stretch | ν μΆμ μ±μ°κΈ° μν΄ κ·Έλ¦¬λ μμ΄ν μ λλ¦Ό |

justify-self
order#
그리λ μμ΄ν μ΄ μλ λ°°μΉλλ μμλ₯Ό λ³κ²½ μ«μκ° μμμλ‘ μμ λ°°μΉλ¨
.container {display: grid;grid-template-rows: repeat(2, 1fr);grid-template-columns: repeat(3, 1fr);}.item:nth-child(1) { order: 1; }.item:nth-child(3) { order: 5; }.item:nth-child(5) { order: -1; }βοΈ μμλ μ¬μ© κ°λ₯

order
z-index#
item μ΄ μμ΄λ μμλ₯Ό λ³κ²½

z-index
βοΈ position μμ λ°°μ΄ κ°λ κ³Ό λμΌ
Grid function#
repeat#
repeat() ν¨μλ ν/μ΄(Track)μ ν¬κΈ° μ μλ₯Ό λ°λ³΅νλ€.
βλ°λ³΅λλ νμβμ βν/μ΄μ ν¬κΈ° μ μβλ₯Ό μΈμλ‘ μ¬μ©
grid-template-rows μ grid-template-columns μμ μ¬μ©
/* 9μ»¬λΌ κ·Έλ¦¬λ */.container { grid-template-columns: 100px 100px 100px 100px 100px 100px 100px 100px 100px;}.container { grid-template-columns: repeat(9, 100px);}.container {grid-template-rows: [row-start] 200px [row-end row-start] 200px [row-end];grid-template-columns: [col-start] 100px [col-end col-start] 100px [col-end col-start] 100px [col-end];}.container {grid-template-rows: repeat(2, [row-start] 200px [row-end]);grid-template-columns: repeat(3, [col-start] 100px [col-end]);}.container {grid-template: repeat(2, [row-start] 200px [row-end]) / repeat(3, [col-start] 100px [col-end]);}.container {/* 12μ»¬λΌ κ·Έλ¦¬λ */grid-template-columns: 1fr 2fr 1fr 2fr 1fr 2fr 1fr 2fr 1fr 2fr 1fr 2fr;}.container {grid-template-columns: repeat(6, 1fr 2fr);}minmax#
minmax() ν¨μλ ν/μ΄(Track)μ βμ΅μ/μ΅λ ν¬κΈ°βλ₯Ό μ μνλ€.
첫 λ²μ§Έ μΈμλ βμ΅μκ°βμ΄κ³ λ λ²μ§Έ μΈμλ βμ΅λκ°βμ΄λ€.
grid-template-row , grid-template-columns , grid-auto-rows κ·Έλ¦¬κ³ grid-auto-columns μμ μ¬μ©νλ€.
μΌλ° μμμ min-width μ max-width μμ±μ λμμ μ§μ νλ κ²κ³Ό μ μ¬νλ€.

minmax
βοΈ minmax()λ₯Ό ν΅ν΄ μμμ ν/μ΄(Track) ν¬κΈ°λ₯Ό μ’ λ μ μ°νκ² μ¬μ©ν μ μλ€. λ€μ μμ λ μμμ βν/μ΄βμ ν¬κΈ°λ₯Ό μ΅μ β200px/300pxβμΌλ‘ μ§μ νμ§λ§ autoλ₯Όν΅ν΄ 그리λ μμ΄ν μ ν¬κΈ°μ λ°λΌ νμ₯λ μ μλ€.
.container {grid-auto-rows: minmax(200px, auto);grid-auto-columns: minmax(300px, auto);}fit-content#
fit-content() ν¨μλ ν/μ΄(Track)μ ν¬κΈ°λ₯Ό 그리λ Item μ΄ ν¬ν¨νλ λ΄μ© (contents) ν¬κΈ°μ λ§μΆλ€. βλ΄μ©μ μ΅λ ν¬κΈ°βλ₯Ό μΈμλ‘ μ¬μ©νλ€.
βοΈ minmax(auto, max-content) μ μ μ¬
.container {grid-template-columns: fit-content(300px) fit-content(300px);}Grid Units#
그리λμμ μ¬μ©νλ μ£Όμ λ¨μλ€μ λν΄μ μμλ΄ μλ€.
fr#
fr (fractional unit)μ μ¬μ© κ°λ₯ν 곡κ°μ λν λΉμ¨μ λ§νλ€.
μμ #
.container {grid-template-columns: 1fr 2fr 100px 25%;}
fr
min-content#
그리λ Item μ΄ ν¬ν¨νλ λ΄μ©(contents)μ μ΅μ ν¬κΈ°
<div class="container"><div class="item">Hello HEROPY~</div><!-- ... --></div>.container {grid-template-columns: min-content 1fr;}
min-content
βοΈ νκΈμ μ¬μ©νλ κ²½μ° container μ word-break: keep-all; μ μ€μ νλ©΄ μ μμ μΌλ‘ λμ
max-content#
그리λ Item μ΄ ν¬ν¨νλ λ΄μ©(contents)μ μ΅λ ν¬κΈ°
<div class="container"><div class="item">Hello HEROPY~</div><!-- ... --></div>.container {grid-template-columns: max-content 1fr;}
max-content
auto-fill , auto-fit#
ν/μ΄(Track)μ κ°μλ₯Ό 그리λ container λ° ν/μ΄ ν¬κΈ°μ λ§κ² μλμΌλ‘(μμμ ) μ‘°μ νλ€. repeat() ν¨μμ κ°μ΄ μ¬μ©νλ©°, ν/μ΄κ³Ό Item κ°μκ° λͺ
νν νμκ°μκ±°λ λͺ
ννμ§ μμ κ²½μ° μ μ©.(λ°μν 그리λ)
βοΈ auto-fill κ³Ό auto-fit μ κ°λ¨ν μ°¨μ΄μ μ μ μΈνλ©΄ λμΌνκ² λμνλ€
λ€μ 4μ»¬λΌ κ·Έλ¦¬λ μμ μμ 컨ν μ΄λμ ν¬κΈ°κ° μμ΄ν λ€μ μμ©νκΈ° μΆ©λΆνμ§ μμκ²½μ° μμ΄ν μ λμΉκΈ° μμνλ€.
.container {grid-template-columns: repeat(4, minmax(120px, 1fr));}
auto-fill
λ§μ½ 4μ»¬λΌ κ·Έλ¦¬λλ₯Ό κ³ μ§ν νμκ° μλ€λ©΄, λ€μκ³Ό κ°μ΄ βλ°λ³΅νμβ( repeat() ν¨μμ 첫 λ²μ§Έ μΈμ)λ₯Ό auto-fill μ΄λ auto-fit μΌλ‘ μμ ν μ μλ€. μ΄λ 컨ν
μ΄λμ ν¬κΈ°κ° μμ΄ν
λ€μ μμ©νκΈ° μΆ©λΆνμ§ μμ κ²½μ° μμ΄ν
μ μλμΌλ‘ μ€ λ°κΏμ²λ¦¬νλ©°, κ·Έμ λ§κ² μμμ ν/μ΄λ μλμΌλ‘ μμ νλ€.
.container {grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));}
auto-fill
auto-fill κ³Ό auto-fit μ μ°¨μ΄#
auto-fillκ³Ό auto-fitμ μ°¨μ΄μ μ 그리λ 컨ν
μ΄λκ° νλμ ν/μ΄(Track)μ λͺ¨λ μμ΄ν
μ μμ©νκ³ λ¨λ 곡κ°μ΄ μμ λ λ°μνλ€. λ€μκ³Ό κ°μ΄ auto-fill μλ¨λ 곡κ°(λΉ νΈλ)μ κ·Έλλ‘ μ μ§νκ³ , auto-fit μ λ¨λ 곡κ°μ μΆμνλ€.
.container.auto-fill {grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));}.container.auto-fit {grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));}
auto-fill
Reference#
ν¨μ€νΈμΊ νΌμ€ - νλ‘ νΈμλ κ°λ° κ°μ - HTML & CSS, SASS(SCSS) Part by ParkYoungWoong