๐Ÿ’ป/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>