Grid

grid ๋˜ํ•œ flex์ฒ˜๋Ÿผ ๋ ˆ์ด์•„์›ƒ์˜ ๊ตฌ์กฐ๋ฅผ ์„ค์ •ํ•  ๋•Œ ์‚ฌ์šฉ๋˜๋Š” css ํ‚ค์›Œ๋“œ์ด๋‹ค.

์ด์ „์˜ flex๊ฐ€ ํ•œ๋ฒˆ์— ํ•˜๋‚˜์˜ ๋ฐฉํ–ฅ, ์ฆ‰ 1์ฐจ์›์ ์ธ ๋ ˆ์ด์•„์›ƒ์„ ์„ค์ •ํ–ˆ์—ˆ์ง€๋งŒ grid๋Š” ํ•œ๋ฒˆ์— ์ˆ˜์ง, ์ˆ˜ํ‰์˜ ๋ ˆ์ด์•„์›ƒ์„ ์กฐ์ •ํ• ์ˆ˜ ์žˆ๋Š” 2์ฐจ์›์ ์ธ ๋ ˆ์ด์•„์›ƒ์˜ ์„ค์ •์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ๋”ฐ๋ผ์„œ ๊ตฌ์กฐ์ ์ธ ๋ ˆ์ด์•„์›ƒ์„ ๋งŒ๋“ค๊ณ  ์‹ถ๋‹ค๋ฉด flex๋ณด๋‹ค๋Š” grid์˜ ์‚ฌ์šฉ์ด ๋” ๋‚˜์€ ์„ ํƒ์ด๋‹ค.

<div className='grid-container'>
</div>
.grid-container {
    display: grid;
    grid-template-rows: 1fr 1fr 1fr;
    grid-template-columns: 50px 50px 50px;
    // ์œ„์™€ ๊ฐ™์ด ๋ฐ˜๋ณต๋˜๋Š” css๋ฅผ repeat๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ„์†Œํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค.
    // grid-template-columns: repeat(3, 50px);
}

flex์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ grid๋ฅผ ์„ค์ •ํ•œ ์š”์†Œ์˜ ์ž์‹๋“ค์€ grid-item์œผ๋กœ ์„ค์ •๋˜๋ฉฐ, ๊ฐ ์ž์‹๋“ค์˜ ํฌ๊ธฐ๋ฅผ grid-template-rows/columns๋กœ ์กฐ์ •์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ๊ธฐ์กด์— ์‚ฌ์šฉํ•˜๋˜ px, em, ๋˜๋Š” ๋ฐฑ๋ถ„์œจ๋กœ๋„ ํ‘œํ˜„์ด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, grid container๋‚ด์—์„œ์˜ ํฌ๊ธฐ๋ฅผ ์ •์˜ํ•˜๋Š” fr(fraction)์ด๋ผ๋Š” ๋‹จ์œ„๋กœ๋„ ์„ค์ •์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

์กฐ๊ธˆ ํŠน์ดํ•œ์ ์œผ๋กœ, css๋‚ด์—์„œ grid container์˜ ๊ตฌ์กฐ ์„ค์ •์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

<section>
    <h1>Grid</h1>
    <section className='grid-container'>
        <header>Header</header>
        <aside>Sidebar</aside>
        <main>Main content</main>
        <footer>Footer</footer>
    </section>
</section>
.grid-container {
  height: 300px;
  display: grid;
  grid-template-areas:
    'header header header'
    'aside main .'
    'footer footer footer';
}

header {
  background-color: green;
  grid-area: header;
}
aside {
  background-color: red;
  grid-area: aside;
}

footer {
  background-color: yellow;
  grid-area: footer;
}
main {
  background-color: pink;
  grid-area: main;
}

์œ„ ์˜ˆ์‹œ์ฒ˜๋Ÿผ grid-template-areas๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌ์กฐ๋ฅผ ์ •์˜ํ• ์ˆ˜ ์žˆ๋Š”๋ฐ, ๋ฌธ์ž์—ด์˜ ํ•œ ํ–‰์ด grid-conatiner์˜ ํ–‰์„, ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„๋œ ๊ฐ๊ฐ์˜ ๋ฌธ์ž์—ด์ด ์—ด์„ ๋‚˜ํƒ€๋‚ธ๋‹ค. ์•ˆ์— ์ž‘์„ฑ๋œ ๋ฌธ์ž์—ด์€ ์–ด๋–ค ํƒœ๊ทธ๋“ค์ด grid-container๋ฅผ ๊ตฌ์„ฑํ•˜๋Š”์ง€ ์•Œ๋ ค์ฃผ๊ณ , ํ•ด๋‹น ํƒœ๊ทธ์˜ css ์†์„ฑ์— grid-area๋ฅผ ํ†ตํ•˜์—ฌ ์—ฐ๊ฒฐ์‹œ์ผœ์ฃผ๋ฉด ๋œ๋‹ค. ๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

Last updated