1. ๊ฐ์
ํ๋ ์น ๋ ์ด์์์ ๋ด๋นํ๊ณ ์๋ ๊ฐ์ฅ ํฐ ํ๋กํผํฐ๊ฐ ๋ฐ๋ก Grid์ด๋ค. ๋์ ๊ฒฝ์ฐ ์ ์ฒด์ ์ธ ํ์ Grid๋ก ์ก๊ณ ์์ ๋ถ๋ถ ๋ถ๋ถ์ Flex๋ฅผ ์ฌ์ฉํ๊ณ ์๋ค. ์ฌ์ค ์ง๊ธ์ ์์ ๋ถ๋ถ๋ Flex๋ณด๋ค๋ Grid๋ฅผ ์ฌ์ฉํ๋ ํธ์ด๋ค.
์ด์ ์ ์ฌ๋ฆฐ Flexbox Layout์ ํ ๋ฐฉํฅ ๋ ์ด์์ ์์คํ
์ด๋ผ๋ฉด Grid Layout์ ๋ ๋ฐฉํฅ ๋ ์ด์์ ์์คํ
์ด๋ค. ๋ฐ๋ผ์ Flex๋ณด๋ค ๋ ๋ณต์ก์ ์ธ ๋ ์ด์์ ํํ์ด ๊ฐ๋ฅํ๋ค.
2. ์ฉ์ด ์ ๋ฆฌ
๊ทธ๋ฆฌ๋ ์ปจํ
์ด๋(Grid Container): display: grid
๋ฅผ ์ ์ฉํ๋, Grid์ ์ ์ฒด ์์ญ์ด๋ค. Grid ์ปจํ
์ด๋ ์์ ์์๋ค์ด Grid ๊ท์น์ ์ํฅ์ ๋ฐ์ ์ ๋ ฌ๋๋ค.
๊ทธ๋ฆฌ๋ ์์ดํ
(Grid Item): Grid ์ปจํ
์ด๋์ ์์ ์์๋ค์ด๋ค. ๊ทธ๋ฆฌ๋ ์์ดํ
๋ค์ด Grid ๊ท์น์ ์ํด ๋ฐฐ์น๋๋ค.
๊ทธ๋ฆฌ๋ ํธ๋(Grid Track): Grid์ ํ(Row) ๋๋ ์ด(Column)
๊ทธ๋ฆฌ๋ ์
(Grid Cell): Grid์ ํ ์นธ์ ๊ฐ๋ฅดํจ๋ค. <div>
๊ฐ์ ์ค์ html ์์๋ Grid ์์ดํ
์ด๊ณ , ์ด๋ฐ Grid ์์ดํ
ํ๋๊ฐ ๋ค์ด๊ฐ๋ '๊ฐ์์ ์นธ(ํ)'์ด Grid ์
์ด๋ค.
๊ทธ๋ฆฌ๋ ๋ผ์ธ(Grid Line): Grid ์
์ ๊ตฌ๋ถํ๋ ์ ์ด๋ค.
๊ทธ๋ฆฌ๋ ๋ฒํธ(Grid Number): Grid ๋ผ์ธ์ ๊ฐ ๋ฒํธ์ด๋ค.
๊ทธ๋ฆฌ๋ ๊ฐญ(Grid Gap): Grid ์
์ฌ์ด์ ๊ฐ๊ฒฉ์ด๋ค.
๊ทธ๋ฆฌ๋ ์์ญ(Grid Area): Grid ๋ผ์ธ์ผ๋ก ๋๋ฌ์ธ์ธ ์ฌ๊ฐํ ์์ญ์ผ๋ก, ๊ทธ๋ฆฌ๋ ์
์ ์งํฉ์ด๋ค.
3. Usage
Grid ๊ท์น์ ์ ์ฉํ๊ณ ์ ํ๋ ์์๋ค์ ๋ถ๋ชจ์ display: grid;
๋ฅผ ์ ์ฉ์ ํ๋ฉด ๋๋ค.
.grid-container {
display: grid;
}
display: inline-grid
๋ ์๋ค. ์์ดํ
์ ๋ฐฐ์น์ ๊ด๋ จ์ด ์๋ค๊ฐ ๋ณด๋ค๋, ์ปจํ
์ด๋๊ฐ ์ฃผ๋ณ ์์๋ค๊ณผ ์ด๋ป๊ฒ ์ด์ฐ๋ฌ์ง์ง ๊ฒฐ์ ํ๋ ๊ฐ์ด๋ค. inline-grid
๋ inline-block
์ฒ๋ผ ๋์ํ๋ค.
.grid-container {
display: inline-grid;
}
4. ๊ทธ๋ฆฌ๋ ํํ ์ ์
4-1. grid-template-columns
grid-template-columns
ํ๋กํผํฐ๋ grid columns์ ๋ฐฐ์น๋ฅผ ์ง์ ํ๋๋ฐ ์ฌ์ฉํ๋ค.
.grid-container {
display: grid;
grid-template-columns: 100px 200px 300px;
}
์์ ์ฝ๋๋ grid-container๋ก ๊ฐ์ธ์ ธ ์๋ ์์์์๋ค์ ๊ทธ๋ฆฌ๋๋ก ๋ฐฐ์นํ๊ณ column์ 3์ด๋ก ๋ง๋ค๊ณ ์ฒซ๋ฒ์งธ column ๋ถํฐ 100px, 200px, 300px๋งํผ์ ํฌ๊ธฐ๋ก ๋ง๋ค๊ฒ ๋ค๋ ์๋ฏธ์ด๋ค.
๋ ๋ค๋ฅธ ์ฌ์ฉ๋ฒ์ ๋ณด์.
.grid-container {
display: grid;
grid-template-columns: 1fr 2fr 2fr;
}
์์ ์ฝ๋์์ fr์ fraction์ผ๋ก ์ซ์ ๋น์จ๋๋ก ๊ทธ๋ฆฌ๋ ํธ๋์ ํฌ๊ธฐ๋ฅผ ๋๋๋ค. ์ฆ 1fr 2fr 2fr
์ 1:2:2 ๋น์ค์ผ 3๊ฐ์ column์ ๋ง๋ค๊ฒ ๋ค๋ ์๋ฏธ์ด๋ค.
.grid-container {
display: grid;
grid-template-columns: 100px 2fr 2fr;
}
์์ ์ฝ๋์ ๊ฐ์ด px๊ณผ fr๋ฅผ ํจ๊ป ์ฌ์ฉํ ์ ์๋ค. %๋ก๋ ํฌ๊ธฐ๋ฅผ ์ง์ ํ ์ ์๋ค.
<!DOCTYPE html>
<html>
<head>
<style>
div {
font-size: 24px;
}
.box {
border: 2px solid black;
border-radius: 10px;
text-align: center;
padding: 20px 0px;
background-color: aliceblue;
}
.grid-container1 {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
margin-bottom: 20px;
}
.grid-container2 {
display: grid;
grid-template-columns: 160px 200px 300px;
margin-bottom: 20px;
}
.grid-container3 {
display: grid;
grid-template-columns: 200px 1fr 400px;
}
</style>
</head>
<body>
<div>
grid-template-columns: 1fr 1fr 1fr;
<div class="grid-container1">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
</div>
<div>
grid-template-columns: 160px 200px 300px;
<div class="grid-container2">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
</div>
<div>
grid-template-columns: 200px 1fr 400px;
<div class="grid-container3">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
</div>
</body>
</html>
4-2. grid-template-rows
grid-template-columns
์ ์ด(column)์ ๋ฐฐ์น๋ผ๋ฉด grid-template-rows
์ ํ(row)์ ๋ฐฐ์น์ด๋ค. ๊ทธ ์ธ์ ๋ด์ฉ์ ๊ฐ๋ค.
<!DOCTYPE html>
<html>
<head>
<style>
div {
font-size: 24px;
}
.box {
border: 2px solid black;
border-radius: 10px;
text-align: center;
padding: 20px 0px;
background-color: aliceblue;
}
.grid-container1 {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: 1fr 2fr;
margin-bottom: 20px;
}
.grid-container2 {
display: grid;
grid-template-columns: 1fr 200px 300px;
grid-template-rows: 160px 300px;
margin-bottom: 20px;
}
</style>
</head>
<body>
<div>
grid-template-columns: 1fr 1fr 1fr; / grid-template-rows: 1fr 2fr;
<div class="grid-container1">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
</div>
<div>
grid-template-columns: 1fr 200px 300px; / grid-template-rows: 160px 300px;
<div class="grid-container2">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
</div>
</body>
</html>
4-3. repeat ํจ์
grid-template-columns, grid-template-rows
์ ํ๋กํผํฐ๊ฐ์ผ๋ก ์ฌ์ฉ๋๋ repeat๋ ๋ฐ๋ณต๋๋ ๊ฐ์ ์๋์ผ๋ก ์ฒ๋ฆฌํ ์ ์๋ ํจ์์ด๋ค.
์ฌ์ฉ๋ฒ grid-template-columns: repeat(๋ฐ๋ณตํ์, ๋ฐ๋ณต๊ฐ)
.grid-container {
display: grid;
grid-template-columns: repeat(5, 1fr);
}
.grid-container {
display: grid;
grid-template-columns: 1fr 1fr 1fr 1fr 1fr;
}
์์ ๋ ์ฝ๋๋ ๊ฐ์ ๊ฒฐ๊ณผ๋ฅผ ๋ํ๋ธ๋ค.
4-4. minmax ํจ์
์ต์๊ฐ๊ณผ ์ต๋๊ฐ์ ์ง์ ํ ์ ์๋ ํจ์์ด๋ค.
minmax(100px, auto)
์ ์๋ฏธ๋ '์ต์ํ 100px, ์ต๋๋ ์๋์ผ๋ก(auto) ๋์ด๋๊ฒ ํ๋ค.'์ด๋ค. ์๋ฌด๋ฆฌ ๋ด์ฉ์ ์์ด ์ ๋๋ผ๋ ์ต์ํ์ ๋์ด 100px์ ํ๋ณด๋๊ณ , ๋ด์ฉ์ด ๋ง์ 100px์ด ๋์ด๊ฐ๋ฉด ์์์ ๋์ด๋๋๋ก ํ๋ค.
<!DOCTYPE html>
<html>
<head>
<style>
div {
font-size: 24px;
}
.box {
border: 2px solid black;
border-radius: 10px;
text-align: center;
padding: 20px 0px;
background-color: aliceblue;
}
.grid-container1 {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: auto 1fr;
margin-bottom: 20px;
}
.grid-container2 {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(2, minmax(100px, auto));
}
</style>
</head>
<body>
<div>
grid-template-columns: repeat(3, 1fr); grid-template-rows: auto 1fr;
<div class="grid-container1">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">
Lorem ipsum, dolor sit amet consectetur adipisicing elit. Eligendi
eius illo nisi id ut vel, neque aliquam sapiente ullam tempore beatae
sit aperiam consequuntur! Soluta amet culpa ab omnis quo!
</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
</div>
<div>
grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(2,
minmax(100px, auto));
<div class="grid-container2">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">
Lorem ipsum, dolor sit amet consectetur adipisicing elit. Eligendi
eius illo nisi id ut vel, neque aliquam sapiente ullam tempore beatae
sit aperiam consequuntur! Soluta amet culpa ab omnis quo!
</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
</div>
</body>
</html>
4-5. auto-fill, auto-fit
auto-fill
๊ณผ auto-fit
์ column์ ๊ฐ์๋ฅผ ๋ฏธ๋ฆฌ ์ ํ์ง ์๊ณ ์ค์ ๋ ๋๋น๊ฐ ํ์ฉํ๋ ํ ์ต๋ํ ์
์ ์ฑ์ด๋ค.
auto-fill
๊ณผ auto-fit
์ ์ฐจ์ด๋ ๋จ๋ ๊ณต๊ฐ์ ์ฑ์ฐ์ง ์๋๋ ์ฑ์ฐ๋์ด๋ค. auto-fill
์ ๋จ์ ๊ณต๊ฐ์ ์ฑ์ฐ์ง ์๊ณ auto-fit
์ ๋จ์ ๊ณต๊ฐ์ ์ฑ์ด๋ค.
<!DOCTYPE html>
<html>
<head>
<style>
div {
font-size: 24px;
box-sizing: border-box;
}
.box {
border: 2px solid black;
border-radius: 10px;
text-align: center;
padding: 20px 0px;
background-color: aliceblue;
}
.grid-container1 {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(25%, auto));
margin-bottom: 20px;
}
.grid-container2 {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(25%, auto));
}
</style>
</head>
<body>
<div>
<div class="grid-container1">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
</div>
<div>
<div class="grid-container2">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
</div>
</body>
</html>
๋ด๊ฐ ์๊ฐํ ๊ฒฐ๊ณผ๊ฐ ๋ค๋ฅด๋ค. ์๋ fill์ด๊ธฐ ๋๋ฌธ์ ์์๋ ๊ฒฐ๊ณผ์ด์ง๋ง ์๋๋ fit์ผ๋ก ์ ํด๋ 2๋ฒ์งธ ์ด์ด ๋ชจ๋ ๋๋น๋ฅผ ์ฐจ์งํ์ง ์๋๋ค. ์ค๊น?
5. ๊ฐ๊ฒฉ๋ง๋ค๊ธฐ
๊ทธ๋ฆฌ๋ ์
์ฌ์ด์ ๊ฐ๊ฒฉ์ ์ค์ ์ ํ๊ธฐ ์ํด์๋ row-gap
, column-gap
ํ๋กํผํฐ๋ฅผ ์ฌ์ฉํ๋ค. ๊ฐ๊ฐ row(์ด), column(ํ)์ ๊ฐ๊ฒฉ์ ์ง์ ํ๋ฉฐ gap
ํ๋กํผํฐ๋ row-gap
๊ณผ column-gap
๋ฅผ ์ง์ ํ ์ ์๋ shorthand์ด๋ค.
์ฌ์ฉ๋ฒ gap: row-gap, column-gap
row-gap
๊ณผ column-gap
์ด ๊ฐ์ผ๋ฉด ํ๋๋ง ์ ์ด๋ ์ด๊ณผ ํ์ ๋ชจ๋ ์ ์ฉ์ด ๋๋ค.
<!DOCTYPE html>
<html>
<head>
<style>
div {
font-size: 24px;
box-sizing: border-box;
}
.box {
border: 2px solid black;
border-radius: 10px;
text-align: center;
padding: 10px 0px;
background-color: aliceblue;
}
.grid-container1 {
display: grid;
grid-template-columns: repeat(3, 1fr);
column-gap: 10px;
row-gap: 10px;
margin-bottom: 20px;
}
.grid-container2 {
display: grid;
grid-template-columns: repeat(3, 1fr);
column-gap: 30px;
row-gap: 5px;
margin-bottom: 20px;
}
.grid-container3 {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 40px 10px;
/* row-gap: 40px, column-gap: 10px*/
margin-bottom: 20px;
}
.grid-container4 {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 20px;
/* row-gap: 20px, column-gap: 20px*/
}
</style>
</head>
<body>
<div>
column-gap: 10px; row-gap: 10px;
<div class="grid-container1">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
</div>
<div>
column-gap: 30px; row-gap: 5px;
<div class="grid-container2">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
</div>
<div>
gap: 40px 10px;
<div class="grid-container3">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
</div>
<div>
gap: 20px;
<div class="grid-container4">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
</div>
</body>
</html>
6. ๊ทธ๋ฆฌ๋ ํํ๋ฅผ ์๋์ผ๋ก ์ ์
grid-template-columns
๋๋ grid-template-rows
์ ํต์ ๋ฅผ ๋ฒ์ด๋ ์์น์ ์๋ ํธ๋์ ํฌ๊ธฐ๋ฅผ ์ง์ ํ๋ ์์ฑ์ด๋ค.
์๋์ ์ฝ๋๋ฅผ ๋ณด์.
.grid-container {
display: grid;
grid-template-rows: repeat(3, minmax(320px, auto));
}
ํด๋น css๋ 3๋ฒ์งธ ์ด๊น์ง๋ง ํธ๋ ์ ํฌ๊ธฐ๋ฅผ ์ง์ ํ๊ณ ์๋ค. ํ์ง๋ง ๋ง์ฝ 4๋ฒ์งธ ์ด์์ ์ด์ด ์๊ธธ ๊ฒฝ์ฐ ์ง์ ํด์ฃผ์ง ์์ ๋ค๋ฅธ ํฌ๊ธฐ์ ํธ๋ ์ด ์๊ธธ ์ ์๋ค. ์ด๋ 4๋ฒ์งธ ์ด์์ ์ด์ด ๋ฐ๋ก ํต์ ๋ฅผ ๋ฒ์ด๋ ์์น์ ์๋ ํธ๋ ์ด๋ค. ์ฆ, ์ฐ๋ฆฌ๊ฐ ์์ฑํ๊ณ ์ ํ๋ column๊ณผ row์ ๊ฐ์๋ฅผ ๋ชจ๋ฅผ ๋ grid-template-columns
๋๋ grid-template-rows
ํ๋กํผํฐ๋ฅผ ์ฌ์ฉํ๋ฉด ๋๋ค. ์ด๋ ๋ชจ๋ column๊ณผ row๋ ํต์ ๋ฅผ ๋ฒ์ด๋ ์์น์ ์๋ ํธ๋ ์ด๋ผ๊ณ ํ ์ ์๋ค.
<!DOCTYPE html>
<html>
<head>
<style>
div {
font-size: 24px;
box-sizing: border-box;
}
.box {
border: 2px solid black;
border-radius: 10px;
text-align: center;
padding: 20px 0px;
background-color: aliceblue;
}
.grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-rows: minmax(120px, auto);
}
</style>
</head>
<body>
<div>
<div class="grid-container">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">
Lorem ipsum dolor sit amet consectetur, adipisicing elit. Omnis unde
molestias sequi facere repudiandae architecto molestiae inventore
ducimus rem, modi rerum, saepe reprehenderit tempore nemo minima ipsa
laboriosam odit dolore?
</div>
<div class="box">6</div>
</div>
</div>
</body>
</html>
7. grid-column, grid-row
grid-column
, grid-row
ํ๋กํผํฐ๋ Grid ์์ดํ
์ ์ ์ฉํ๋ ํ๋กํผํฐ๋ก, ๊ฐ ์
์ ์์ญ์ ์ง์ ํ๋ค. Grid์๋ ์๋์ ๊ฐ์ ๋ฒํธ๊ฐ ๋งค๊ฒจ์ ธ ์๋ค.
์ด๋ฌํ Grid ๋ฒํธ๋ฅผ ์ฌ์ฉํ์ฌ ์์ญ์ ์ง์ ํ๋ค.
์ฌ์ฉ๋ฒ grid-column: <์์๋ฒํธ> / <๋๋ฒํธ>
์ฌ์ฉ๋ฒ grid-row: <์์๋ฒํธ> / <๋๋ฒํธ>
๋๋ ๋ช ๊ฐ์ ์
์ ์ฐจ์งํ๊ฒ ํ ๊ฒ์ด์ง๋ฅผ ์ง์ ํ ์๋ ์๋ค.
์ฌ์ฉ๋ฒ grid-column: <์์๋ฒํธ> / span<์์นํ ์์ญ ์
์>
์ฌ์ฉ๋ฒ grid-row: <์์๋ฒํธ> / span<์์นํ ์์ญ ์
์>
<!DOCTYPE html>
<html>
<head>
<style>
div {
font-size: 24px;
box-sizing: border-box;
}
.box {
border: 2px solid black;
border-radius: 10px;
text-align: center;
padding: 20px 0px;
background-color: aliceblue;
}
.grid-container {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-auto-rows: minmax(120px, auto);
}
.box:nth-child(1) {
grid-column: 1 / 4;
grid-row: 1 / 3;
}
.box:nth-child(4) {
grid-column: 1 / span 2;
}
.box:nth-child(8) {
grid-column: 1 / -1;
grid-row: 4 / 6;
}
</style>
</head>
<body>
<div>
<div class="grid-container">
<div class="box">grid-column: 1 / 4; grid-row: 1 / 3;</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">grid-column: 1 / span 2;</div>
<div class="box">5</div>
<div class="box">6</div>
<div class="box">7</div>
<div class="box">grid-column: 1 / -1; grid-row: 4 / 6;</div>
<div class="box">9</div>
<div class="box">10</div>
</div>
</div>
</body>
</html>
8. ์ธ๋ก ๋ฐฉํฅ ์ ๋ ฌ
8-1. align-items
align-items
๋ ์ปจํ
์ด๋์ ์ ์ฉํ๋ ํ๋กํผํฐ๋ก ์์ดํ
๋ค์ ์ธ๋ก(column์ถ) ๋ฐฉํฅ์ผ๋ก ์ ๋ ฌํ๋ค.
<!DOCTYPE html>
<html>
<head>
<style>
div {
font-size: 24px;
box-sizing: border-box;
}
.parents {
background-color: greenyellow;
padding: 5px;
margin-bottom: 20px;
}
.box {
border: 2px solid black;
border-radius: 10px;
text-align: center;
padding: 20px 0px;
background-color: aliceblue;
}
.grid-container {
height: 300px;
display: grid;
grid-template-columns: repeat(3, 1fr);
}
.stretch {
align-items: stretch;
}
.start {
align-items: start;
}
.end {
align-items: end;
}
.center {
align-items: center;
}
</style>
</head>
<body>
<div class="parents">
align-items: stretch;
<div class="grid-container stretch">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
</div>
<div class="parents">
align-items: start;
<div class="grid-container start">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
</div>
<div class="parents">
align-items: end;
<div class="grid-container end">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
</div>
<div class="parents">
align-items: center;
<div class="grid-container center">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
</div>
</body>
</html>
8-2. align-content
align-content
๋ ์ปจํ
์ด๋์ ์ ์ฉํ๋ ํ๋กํผํฐ๋ก Gird ์์ดํ
๋ค์ ๋์ด๋ฅผ ๋ชจ๋ ํฉํ ๊ฐ์ด Grid ์ปจํ
์ด๋์ ๋์ด๋ณด๋ค ์์ ๋ Grid ์์ดํ
๋ค์ ํต์งธ๋ก ์ ๋ ฌํ๋ค.
<!DOCTYPE html>
<html>
<head>
<style>
div {
font-size: 24px;
box-sizing: border-box;
}
.parents {
background-color: greenyellow;
padding: 5px;
margin-bottom: 20px;
}
.box {
border: 2px solid black;
border-radius: 10px;
text-align: center;
padding: 20px 0px;
background-color: aliceblue;
}
.grid-container {
height: 200px;
display: grid;
grid-template-columns: repeat(3, 1fr);
}
.start {
align-content: start;
}
.end {
align-content: end;
}
.center {
align-content: center;
}
.space-between {
align-content: space-between;
}
.space-around {
align-content: space-around;
}
.space-evenly {
align-content: space-evenly;
}
</style>
</head>
<body>
<div class="parents">
align-content: start;
<div class="grid-container start">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
</div>
<div class="parents">
align-content: end;
<div class="grid-container end">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
</div>
<div class="parents">
align-content: center;
<div class="grid-container center">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
</div>
<div class="parents">
align-content: space-between;
<div class="grid-container space-between">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
</div>
<div class="parents">
align-content: space-around;
<div class="grid-container space-around">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
</div>
<div class="parents">
align-content: space-evenly;
<div class="grid-container space-evenly">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
</div>
</body>
</html>
8-3. align-self
align-self
๋ Grid ์์ดํ
์ ์ ์ฉํ๋ ํ๋กํผํฐ๋ก ํด๋น ์์ดํ
์ ์ธ๋ก(column์ถ) ๋ฐฉํฅ์ผ๋ก ์ ๋ ฌํ๋ค.
<!DOCTYPE html>
<html>
<head>
<style>
div {
font-size: 24px;
box-sizing: border-box;
}
.parents {
background-color: greenyellow;
padding: 5px;
margin-bottom: 20px;
}
.box {
border: 2px solid black;
border-radius: 10px;
text-align: center;
padding: 20px 0px;
background-color: aliceblue;
}
.grid-container {
height: 300px;
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-rows: 1fr;
}
.box:nth-child(1) {
align-self: stretch;
}
.box:nth-child(2) {
align-self: start;
}
.box:nth-child(5) {
align-self: center;
}
.box:nth-child(6) {
align-self: end;
}
</style>
</head>
<body>
<div class="parents">
<div class="grid-container start">
<div class="box">align-self: stretch;</div>
<div class="box">align-self: start;</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">align-self: center;</div>
<div class="box">align-self: end;</div>
</div>
</div>
</body>
</html>
9. ๊ฐ๋ก ๋ฐฉํฅ ์ ๋ ฌ
9-1. justify-items
justify-items
ํ๋กํผํฐ๋ Gird ์ปจํ
์ด๋์ ์ ์ฉํ๋ฉฐ ์์ดํ
๋ค์ ๊ฐ๋ก(row์ถ) ๋ฐฉํฅ์ผ๋ก ์ ๋ ฌํ ๋ ์ฌ์ฉํ๋ค.
<!DOCTYPE html>
<html>
<head>
<style>
div {
font-size: 24px;
box-sizing: border-box;
}
.parents {
background-color: greenyellow;
padding: 5px;
margin-bottom: 20px;
}
.box {
border: 2px solid black;
border-radius: 10px;
text-align: center;
padding: 20px 40px;
background-color: aliceblue;
}
.grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
}
.start {
justify-items: start;
}
.end {
justify-items: end;
}
.center {
justify-items: center;
}
.stretch {
justify-items: stretch;
}
</style>
</head>
<body>
<div class="parents">
justify-items: start;
<div class="grid-container start">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
</div>
<div class="parents">
justify-items: end;
<div class="grid-container end">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
</div>
<div class="parents">
justify-items: center;
<div class="grid-container center">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
</div>
<div class="parents">
justify-items: stretch;
<div class="grid-container stretch">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
</div>
</body>
</html>
9-2. justify-content
justify-content
ํ๋กํผํฐ๋ Grid ์ปจํ
์ด๋์ ์ ์ฉํ๋ฉฐ Grid ์์ดํ
๋ค์ ๋๋น๋ฅผ ๋ชจ๋ ํฉํ ๊ฐ์ด Grid ์ปจํ
์ด๋์ ๋๋น๋ณด๋ค ์์ ๋ Grid ์์ดํ
๋ค์ ํต์งธ๋ก ์ ๋ ฌํ๋ค.
<!DOCTYPE html>
<html>
<head>
<style>
div {
font-size: 24px;
box-sizing: border-box;
}
.parents {
background-color: greenyellow;
padding: 5px;
margin-bottom: 20px;
}
.box {
border: 2px solid black;
border-radius: 10px;
width: 200px;
text-align: center;
padding: 20px 40px;
background-color: aliceblue;
}
.grid-container {
display: grid;
grid-template-columns: repeat(3, auto);
}
.stretch {
justify-content: stretch;
}
.start {
justify-content: start;
}
.center {
justify-content: center;
}
.end {
justify-content: end;
}
.space-between {
justify-content: space-between;
}
.space-around {
justify-content: space-around;
}
.space-evenly {
justify-content: space-evenly;
}
</style>
</head>
<body>
<div class="parents">
justify-content: stretch;
<div class="grid-container stretch">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
</div>
<div class="parents">
justify-content: start;
<div class="grid-container start">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
</div>
<div class="parents">
justify-content: center;
<div class="grid-container center">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
</div>
<div class="parents">
justify-content: end;
<div class="grid-container end">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
</div>
<div class="parents">
justify-content: space-between;
<div class="grid-container space-between">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
</div>
<div class="parents">
justify-content: space-around;
<div class="grid-container space-around">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
</div>
<div class="parents">
justify-content: space-evenly;
<div class="grid-container space-evenly">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
</div>
</body>
</html>
9-3. justify-self
justify-self
๋ Grid ์์ดํ
์ ์ ์ฉํ๋ ํ๋กํผํฐ๋ก ํด๋น ์์ดํ
์ ๊ฐ๋ก(row์ถ) ๋ฐฉํฅ์ผ๋ก ์ ๋ ฌํ๋ค.
<!DOCTYPE html>
<html>
<head>
<style>
div {
font-size: 24px;
box-sizing: border-box;
}
.parents {
background-color: greenyellow;
padding: 5px;
margin-bottom: 20px;
}
.box {
border: 2px solid black;
border-radius: 10px;
text-align: center;
padding: 20px 40px;
background-color: aliceblue;
}
.grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
}
.box:nth-child(1) {
justify-self: stretch;
}
.box:nth-child(2) {
justify-self: start;
}
.box:nth-child(5) {
justify-self: center;
}
.box:nth-child(6) {
justify-self: end;
}
</style>
</head>
<body>
<div class="parents">
<div class="grid-container">
<div class="box">justify-self: stretch;</div>
<div class="box">justify-self: start;</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">justify-self: center;</div>
<div class="box">justify-self: end;</div>
</div>
</div>
</body>
</html>
10. ์ ๋ ฌ shorthand
10-1. place-content
align-content
์ justify-content
๋ฅผ ๊ฐ์ด ์ธ ์ ์๋ shorthand์ด๋ค.
์ฌ์ฉ๋ฒ place-content: <align-content> <justify-content>
ํ๋์ ๊ฐ๋ง ์ฐ๋ฉด ๋ ํ๋กํผํฐ ๋ชจ๋์ ์ ์ฉ๋๋ค.
10-2. place-slef
align-slef
์ justify-slef
๋ฅผ ๊ฐ์ด ์ธ ์ ์๋ shorthand์ด๋ค.
์ฌ์ฉ๋ฒ place-slef: <align-slef> <justify-slef>
ํ๋์ ๊ฐ๋ง ์ฐ๋ฉด ๋ ํ๋กํผํฐ ๋ชจ๋์ ์ ์ฉ๋๋ค.
11. order
๊ฐ Grid ์์ดํ
๋ค์ ์๊ฐ์ ๋์ด ์์๋ฅผ ๊ฒฐ์ ํ๋ ์์ฑ์ด๋ค.
์ซ์๊ฐ์ด ๋ค์ด๊ฐ๋ฉด, ์์ ์ซ์์ผ ์๋ก ๋จผ์ ๋ฐฐ์น๋๋ค. "์๊ฐ์ " ์์์ผ ๋ฟ HTML ์์ฒด์ ๊ตฌ์กฐ๋ฅผ ๋ฐ๊พธ์ง ์๋ค.
12. z-index
Z์ถ ์ ๋ ฌ์ ํ ์ ์๋ ํ๋กํผํฐ์ด๋ค. ์ซ์๊ฐ ํด ์๋ก ์๋ก ์ฌ๋ผ๊ฐ๋ค.
<!DOCTYPE html>
<html>
<head>
<style>
div {
font-size: 24px;
box-sizing: border-box;
}
.parents {
background-color: greenyellow;
padding: 5px;
margin-bottom: 20px;
}
.box {
border: 2px solid black;
border-radius: 10px;
text-align: center;
padding: 20px 40px;
background-color: aliceblue;
}
.grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 10px;
}
.box:nth-child(5) {
z-index: 1;
background-color: burlywood;
opacity: 0.8;
transform: scale(2);
}
</style>
</head>
<body>
<div class="parents">
<div class="grid-container">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
<div class="box">7</div>
<div class="box">8</div>
<div class="box">9</div>
</div>
</div>
</body>
</html>
13. Conclusion
๋ด๊ฐ ์๋ฐ์คํฌ๋ฆฝํธ์ ์น ํ๋ ์์ํฌ/๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ค React๋ฅผ ์ฌ๋ํ๋ ๊ฒ๊ณผ ๊ฐ์ด CSS์์๋ Grid๋ฅผ ๊ฐ์ฅ ๋ง์ด ์ฌ๋ํ๋ค. ๊ทธ ๋งํผ ์ฌ์ฉํ๋ ๋น์ค์ด ๋ง๊ณ ์ ์ฉํ๊ฒ ์ฌ์ฉํ๊ณ ์๋ค๋ ๋ป์ด๋ค. ํ์คํ Grid๋ฅผ ์ฒ์ ๊ณต๋ถํ ๋ ํ๋ํ๋ ์ดํดํ๋ ค๊ณ ๋ง์ ์๊ฐ์ ๋ณด๋๋๋ฐ ์ง๊ธ์ Flex์ ํจ๊ป ์๋ก ๋น๊ตํ๋ฉฐ ๊ณต๋ถํ๋ ์ฝ๊ฒ ์ดํด๊ฐ ๋์๋ค. ๋ค๋ง ์ฃผ๋ก ์ฌ์ฉํ์ง ์๋ ํ๋กํผํฐ๊ฐ ์๋๋ฐ ๋ฐ๋ก justify-content
์ align-content
์ด๋ค. ์ด ๋ ์์ฑ์ Flex์์๋ ๋ง์ด ์ฌ์ฉํ์ง๋ง Grid์์๋ ํ ๋ฒ๋ ์ฌ์ฉํ์ง ์์๋ ํ๋กํผํฐ์ธ๊ฑฐ ๊ฐ๋ค. ๊ทธ๋๋ ์ด๋ค ํน์ง์ ๊ฐ์ง๊ณ ์๋์ง ์ง๊ณ ๋์ด๊ฐ์.
๊ทธ๋ฆฌ๊ณ ์ฐ์ฐํ๊ฒ.. auto-fill
, auto-fit
์ด ๋ด ์๊ฐ๋๋ก, ์์ํ๋๋ก ์๋ํ์ง ์์์ ์กฐ๊ธ์ ์์ธํ๋ค... ํด๊ฒฐ์ฑ
์ ์ฐพ์๋ด์ผ๊ฒ ๋ค.๐ญ
์ฐธ๊ณ
The CSS Grid Enchiridion
์ด๋ฒ์์ผ๋ง๋ก CSS Grid๋ฅผ ์ตํ๋ณด์
Understanding CSS Grid: Grid Lines
๐
๐
2022-07-25