๐ป/CSS
(45) [CSS] 3์ผ ๋ ์ด์์๊ณผ ํฌ์ง์ ๋ - Grid
๋ฐ๊ถ
2024. 4. 18. 13:46
Grid Layout์ด๋?
ํ์ด์ง๋ฅผ ์ฌ๋ฌ ์ฃผ์ ์์ญ์ผ๋ก ๋๋๊ฑฐ๋, ํฌ๊ธฐ์ ์์น ๋ฐ ๋ฌธ์ ๊ณ์ธต ๊ตฌ์กฐ์ ๊ด์ ์์
HTML ๊ธฐ๋ณธ ์์๋ก ์์ฑ๋ ์ฝํธ๋กค ๊ฐ์ ๊ด๊ณ๋ฅผ ์ ์ํ๋๋ฐ ํ์ํ๋ค.
ํ ์ด๋ธ๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก ๊ทธ๋ฆฌ๋ ๋ ์ด์์์ ์ธ๋ก ์ด๊ณผ ๊ฐ๋ก ํ์ ๊ธฐ์ค์ผ๋ก ์์๋ฅผ ์ ๋ ฌํ ์ ์๋ค.
Grid ๊ตฌ์ฑ
Grid๋ ๋ณต์์ ์์ ์์์ธ Grid Item๊ณผ ๊ทธ ์์ ๋ถ๋ชจ ์์์ธ Grid Container(๊ทธ๋ฆฌ๋ ์ปจํ ์ด๋)๋ก ๊ตฌ์ฑ๋๋ค. ์ ๋ ฌํ๋ ค๋ ใด์์์ ๋ถ๋ชจ ์์์ 'display:grid' ๋๋ 'display:line-grid'์์ฑ์ ์ ์ธํ๋ค.
Grid Container ์์ฑ
์์ฑ | ์๋ฏธ |
display | ๊ทธ๋ฆฌ๋ ์ปจํ ์ด๋๋ฅผ ์ ์ |
grid-tmplate-rows | ๋ช ์์ ํ์ ํฌ๊ธฐ๋ฅผ ์ ์ |
grid-tmplate-columns | ๋ช ์์ ์ด์ ํฌ๊ธฐ๋ฅผ ์ ์ |
grid-template-areas | ์์ญ ์ด๋ฆ์ ์ฐธ์กฐํด ํ ํ๋ฆฟ ์์ฑ |
row-gap | ํ๊ณผ ํ ์ฌ์ด์ ๊ฐ๊ฒฉ์ ์ ์ |
colum-gap | ์ด๊ณผ ์ด ์ฌ์ด์ ๊ฐ๊ฒฉ์ ์ ์ |
grid-auto-rows | ์์์ ์ธ ํ์ ํฌ๊ธฐ๋ฅผ ์ ์ |
grid-auto-columns | ์์์ ์ธ ์ด์ ํฌ๊ธฐ๋ฅผ ์ ์ |
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
* { box-sizing: border-box; }
.container {
width: 600px;
height: 600px;
margin: 50px;
background-color: #fff;
border: 10px solid #eee;
border-radius:10px;
display: grid;
grid-template-rows: 100px auto 100px;
grid-template-columns: 100px auto;
}
.item {
font-weight: 900;
font-size: 25px;
border: 1px solid #333;
border-radius: 3px;
display: flex;
align-items: center;
justify-content: center;
}
.item1 { background : rgb(255, 100, 70); }
.item2 { background : rgb(255, 165, 0); }
.item3 { background : rgb(50, 205, 50); }
.item4 { background : rgb(255, 105, 180); }
.item5 { background : rgb(30, 145, 255); }
.item6 { background : rgb(170, 170, 170); }
</style>
</head>
<body>
<h3>Grid Layout</h3>
<p>
grid-template-rows/grid-template-columns : ๋ช
์์ ํ/์ด(Track)์ ํฌ๊ธฐ๋ฅผ ์ ์
</p>
<div class="container">
<div class="item item1">1</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
<div class="item item4">4</div>
<div class="item item5">5</div>
<div class="item item6">6</div>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
* { box-sizing: border-box; }
.container {
width: 600px;
height: 600px;
margin: 50px;
background-color: #fff;
border: 10px solid #eee;
border-radius:10px;
font-weight: 900;
font-size: 25px;
display: grid;
grid-template-rows: 100px auto 100px;
grid-template-columns: 100px auto;
grid-template-areas:
"header header"
"nav body"
"footer footer"
}
.header {
grid-area: header;
border: 1px solid #3bc9db;
background: #99e9f2;
margin: 5px 5px;
display: flex;
justify-content: center;
align-items: center;
}
.nav {
grid-area: nav;
border: 1px solid #3bc9db;
background: #99e9f2;
margin: 5px 5px;
display: flex;
justify-content: center;
align-items: center;
}
.body {
grid-area: body;
border: 1px solid #3bc9db;
background: #99e9f2;
margin: 5px 5px;
display: flex;
justify-content: center;
align-items: center;
}
.footer {
grid-area: footer;
border: 1px solid #3bc9db;
background: #99e9f2;
margin: 5px 5px;
display: flex;
justify-content: center;
align-items: center;
}
</style>
</head>
<body>
<h3>Grid Layout</h3>
<p>
grid-template-areas - ์์ญ(Area) ์ด๋ฆ์ ์ฐธ์กฐํด ํ
ํ๋ฆฟ ์์ฑ
</p>
<div class="container">
<header class="header">HEADER</header>
<nav class="nav">NAV</nav>
<article class="body">BODY</article>
<footer class="footer">FOOTER</footer>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
* { box-sizing: border-box; }
.container {
width: 600px;
height: 600px;
margin: 50px;
background-color: #fff;
border: 10px solid #eee;
border-radius:10px;
display: grid;
grid-template: repeat(3, auto) / repeat(2, auto);
gap: 10px;
}
.item {
font-weight: 900;
font-size: 25px;
border: 1px solid #333;
border-radius: 3px;
padding: 10px;
display: flex;
align-items: center;
justify-content: center;
}
.item1 { background : rgb(255, 100, 70); }
.item2 { background : rgb(255, 165, 0); }
.item3 { background : rgb(50, 205, 50); }
.item4 { background : rgb(255, 105, 180); }
.item5 { background : rgb(30, 145, 255); }
.item6 { background : rgb(170, 170, 170); }
</style>
</head>
<body>
<h3>Grid Layout</h3>
<p>
grid-template : grid-template-rows, grid-template-columns, grid-template-areas์ ๋จ์ถ ์์ฑ<br>
gap : ๊ทธ๋ฆฌ๋ ๋ผ์ธ(ํ/์ด) ์ฌ์ด ๊ฐ๊ฒฉ(Gutter)์ ์กฐ์
</p>
<div class="container">
<div class="item item1">1</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
<div class="item item4">4</div>
<div class="item item5">5</div>
<div class="item item6">6</div>
</div>
</body>
</html>
Grid Items ์์ฑ
์์ฑ | ์๋ฏธ |
grid-row-start | ๊ทธ๋ฆฌ๋ ์์ดํ ์ ํ ์์ ์์น ์ง์ |
grid-row-end | ๊ทธ๋ฆฌ๋ ์์ดํ ์ ํ ๋ ์์น ์ง์ |
grid-column-start | ๊ทธ๋ฆฌ๋ ์์ดํ ์ ์ด ์์ ์์น ์ง์ |
grid-column-end | ๊ทธ๋ฆฌ๋ ์์ดํ ์ ์ด ๋ ์์น ์ง์ |
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
* { box-sizing: border-box; }
.container {
width: 600px;
height: 600px;
margin: 50px;
box-sizing: border-box;
background-color: #fff;
border: 10px solid #eee;
border-radius:10px;
display: grid;
grid-template-rows: repeat(4, 1fr);
grid-template-columns: repeat(3, 1fr);
}
.item {
font-weight: 900;
font-size: 25px;
border: 1px solid #333;
border-radius: 3px;
padding: 10px;
box-sizing: border-box;
display: flex;
align-items: center;
justify-content: center;
}
.item1 { grid-column: 1 / 4; } /* 3์นธ */
.item2 { grid-row: 2 / 4; } /* 2ํ */
.item3 { grid-column: 2 / span 2; } /* 2์ด๋ถํฐ 2์นธ */
.item6 { grid-column: 1 / span 3; } /* 1์ด๋ถํฐ 3์นธ */
.item1 { background : rgb(255, 100, 70); }
.item2 { background : rgb(255, 165, 0); }
.item3 { background : rgb(50, 205, 50); }
.item4 { background : rgb(255, 105, 180); }
.item5 { background : rgb(30, 145, 255); }
.item6 { background : rgb(170, 170, 170); }
</style>
</head>
<body>
<h3>Grid Layout</h3>
<p>
grid-row, grid-column ์์ฑ : ๊ทธ๋ฆฌ๋ ์์ดํ
ํ/์ด, ์์/๋ ๋ฐฐ์น ์ค์ ์ ๋จ์ถ ํ์ผ๋ก ์ค์ <br>
</p>
<div class="container">
<div class="item item1">1</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
<div class="item item4">4</div>
<div class="item item5">5</div>
<div class="item item6">6</div>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
* { box-sizing: border-box; }
.container {
width: 800px;
margin: 50px;
padding: 20px;
background-color: #e3fafc;
border: 1px solid #99e9f2;
display: grid;
grid-gap: 40px;
grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
grid-auto-rows: minmax(100px, auto);
grid-auto-flow: dense; /* ํฌ๊ธฐ๊ฐ ์์๊ฒ์ ๋จ๋ ๊ณต๊ฐ์ ๋ฐฐ์น */
/* ์ฃผ์ ์ฒ๋ฆฌํ๋ฉด 6์ ์์น๊ฐ ๋ฐ๋ */
}
.container > .item {
border: 1px solid #3bc9db;
border-radius: 3px;
background-color: #99e9f2;
font-size:250%;
color: #fff;
display: flex;
align-items: center;
justify-content: center;
}
.item:nth-child(1) { grid-row-end: span 2; }
.item:nth-child(2) { grid-row-end: span 3; }
.item:nth-child(4) { grid-row-end: span 3; }
.item:nth-child(5) { grid-column-end: span 2; }
</style>
</head>
<body>
<h3>Grid Layout</h3>
<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>
</body>
</html>