π΄ Why Grid?
βͺ 격μ 무λ¬λ‘ λμΌν μνμ’μ° κ°κ²©μ λκ³ μμ΄ν μ λ°°μΉνκ³ μΆμ λ, flexλ₯Ό μ¬μ©νλ©΄ κ°λ³μ μΌλ‘ μ€μ ν΄μ€μΌ νλ μμλ€μ΄ λ무 λ§μΌλ©°, μμ΄ν μ μΆκ°ν λλ§λ€ cssλ₯Ό μΆκ°ν΄μ€μΌ νλ λ²κ±°λ‘μμ΄ μλ€.
π CSS Grid Basic Concepts
.father {
display: grid;
grid-template-columns: 250px 250px 250px;
grid-template-rows: 100px 50px 300px;
column-gap: 5px;
row-gap: 10px;
}
π‘ Grid Template Areas
.grid {
display: grid;
grid-template-columns: auto 200px;
grid-template-rows: 100px repeat(2, 200px) 100px;
grid-template-areas:
"header header header header"
"content content content nav"
"content content content nav"
"footer footer footer footer"
}
.header {
background-color: #2ecc71;
grid-area: header;
}
.content {
background-color: #3498db;
grid-area: content;
}
.nav {
background-color: #8e44ad;
grid-area: nav;
}
.footer {
background-color: #f39c12;
grid-area: footer;
}
<div class="grid">
<div class="header"></div>
<div class="content"></div>
<div class="nav"></div>
<div class="footer"></div>
</div>
π’ Rows and Columns
.grid {
display: grid;
grid-template-columns: repeat(4, 100px);
grid-template-rows: repeat(4, 100px);
gap: 10px;
}
.header {
background-color: #2ecc71;
/* first method */
grid-column-start: 1;
grid-column-end: 5;
/* second method */
grid-column: 1 / -1;
/* third method */
grid-column: span 4;
}
.content {
background-color: #3498db;
/* first method */
grid-column-start: 1;
grid-column-end: 4;
grid-row-start: 2;
grid-row-end: 4;
/* second method */
grid-column: 1 / -2;
grid-row: 2 / 4;
/* third method */
grid-column: 1 / -2;
grid-row: span 2;
}
.nav {
background-color: #8e44ad;
/* first method */
grid-row-start: 2;
grid-row-end: 4;
/* second method */
grid-row: 2 / 4;
/* third method */
grid-row: span 2;
}
.footer {
background-color: #f39c12;
/* first method */
grid-column-start: 1;
grid-column-end: 5;
/* second method */
grid-column: 1 / -1;
/* third method */
grid-column: span 4;
}
π΅ Grid Template
.grid {
display: grid;
grid-template-columns: repeat(4, 100px);
grid-template-rows: repeat(4, 100px);
gap: 5px;
/* gridλ λμ΄κ° μμΌλ―λ‘ μ§μ μ§μ ν΄μ€μΌ νλ€. */
height: 50vh;
grid-template:
"header header header header" 1fr
"content content content nav" 2fr
"footer footer footer footer" 1fr / 1fr 1fr 1fr 1fr;
}
.header {
background-color: #2ecc71;
grid-area: header;
}
.content {
background-color: #3498db;
grid-area: content;
}
.nav {
background-color: #8e44ad;
grid-area: nav;
}
.footer {
background-color: #f39c12;
grid-area: footer;
}
π£ Place Items, Place Content, Place Self
.grid {
display: grid;
gap: 5px;
height: 50vh;
grid-template-columns: repeat(4, 1fr);
grid-template-rows: repeat(4, 1fr);
place-items: stretch center; /* vertical horizontal */
}
βͺ place-itemsλ 그리λ λ΄μ λͺ¨λ μμ΄ν λ€μ λ°°μΉλ₯Ό μμ§, μν μμλλ‘ ν λ²μ μ§μ νλλ‘ μ€μ ν΄μ£Όλ μμ±μ΄λ€.
βͺ justify-itemsλ‘ μνμ, align-itemsλ‘ μμ§μ λ°λ‘ μ§μ ν μλ μλ€.
βͺ stretchλ κ°λ‘ μΈλ‘μ κ°λ³κ°μ 쀬μ λλ§ μ μ© κ°λ₯ν μμ±μ΄λ€.
.grid {
background: grey;
color: white;
display: grid;
gap: 5px;
height: 250vh;
grid-template-columns: repeat(4, 100px);
grid-template-rows: repeat(4, 100px);
place-content: end center; /* vertical horizontal */
}
βͺ place-contentλ 그리λ νμ λ°°μΉλ₯Ό μμ§, μν μμλλ‘ ν λ²μ μ§μ νλλ‘ μ€μ ν΄μ£Όλ μμ±μ΄λ€.
βͺ justify-contentλ‘ μνμ, align-contentλ‘ μμ§μ λ°λ‘ μ§μ ν μλ μλ€.
.header {
background-color: #2ecc71;
place-self: start end;
}
βͺ place-selfλ 그리λ λ΄μ νλμ μμ΄ν μ λ°°μΉλ₯Ό μμ§, μν μμλλ‘ ν λ²μ μ§μ νλλ‘ μ€μ ν΄μ£Όλ μμ±μ΄λ€.
βͺ justify-selfλ‘ μνμ, align-selfλ‘ μμ§μ λ°λ‘ μ§μ ν μλ μλ€.
π΄ Auto Columns and Rows
<div class="grid">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
...
<div class="item">50</div>
</div>
.gird {
color: white;
display; grid;
gap: 5px;
grid-template-columns: repeat(4, 100px);
grid-template-rows: repeat(4, 100px);
grid-auto-flow: column;
grid-auto-columns: 100px;
}
βͺ grid-auto-flowλ₯Ό ν΅ν΄ gridμ μ λ ¬ λ°©ν₯μ μ ν μ μλ€.
βͺ grid-auto-columns νΉμ grid-auto-rowsλ₯Ό ν΅ν΄ templateμ μ§μ λ λ°λ³΅λ νμμ μμ΄ν λΏλ§ μλλΌ λͺ¨λ μμ΄ν λ€μ ν¬κΈ°λ₯Ό μ§μ ν μ μλ€.
βͺ νμ§λ§, templateμ ν νΉμ μ΄μ κ°―μλ₯Ό μ§μ ν΄μ£Όλλ° μ¬μ ν νμνλ€.
π minmax
βͺ gridμ ν¬κΈ°κ° νλ©΄μ ν¬κΈ°μ λ°λΌ κ°λ³μ μΌλ‘ λ λ, μ΅μ ν¬κΈ°μ μ΅λ ν¬κΈ°λ₯Ό μ ν΄μ€ μ μλ€.
βͺ μ΅μ ν¬κΈ°λ³΄λ€ μμ ν¬κΈ°κ° λλ νλ©΄μμλ μμ΄ν μ ν¬κΈ°κ° μ€μ§ μκ³ μ€ν¬λ‘€μ΄ μμ±λλ€.
.grid {
display: grid;
gap: 5px;
grid-template-columns: repeat(5, minmax(100px, 1fr)); /* min, max */
grid-template-rows: repeat(4, 100px);
grid-auto-columns: 100px;
}
π‘ auto-fit and auto-fill
.grid:first-child {
/* elementλ€μ κ°μ μ¬μ΄μ¦λ‘ μ€μ */
grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
}
.grid:last-child {
/* elementλ₯Ό μ λμ μΈ μ¬μ΄μ¦λ‘ μ€μ */
grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
}
π’ min-content max-content
βͺ min-content, max-contentλ 그리λ λ΄λΆ μμ΄ν μ ν¬κΈ°μ λ§μΆ°μ 그리λμ ν¬κΈ°λ₯Ό μ§μ ν΄μ€λ€.
.grid {
grid-template-columns: repeat(5, minmax(min-content, 1fr));
}
'CSS' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[ CSS ] Flexbox (0) | 2022.07.10 |
---|---|
[ CSS ] CSS κ³ κΈ μ νμ (0) | 2022.04.22 |