๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Stylesheet/CSS

[CSS3]CSS Grid ์™„์ „ ์ •๋ณต: ์›น ๊ฐœ๋ฐœ์ž๋ฅผ ์œ„ํ•œ ์ตœ๊ณ ์˜ ๋ ˆ์ด์•„์›ƒ ์‹œ์Šคํ…œ

by YJ Dev 2024. 6. 4.
728x90
๋ฐ˜์‘ํ˜•
SMALL

CSS Grid๋Š” 2์ฐจ์› ๋ ˆ์ด์•„์›ƒ์„ ์‰ฝ๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ฃผ์–ด ๋ณต์žกํ•œ ๋””์ž์ธ๋„ ๊ฐ„๋‹จํžˆ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด ๊ธ€์˜ ๋ชฉ์ ์€ CSS Grid๋ฅผ ์ฒ˜์Œ ์ ‘ํ•˜๋Š” ์ดˆ๋ณด์ž๋ถ€ํ„ฐ ๊ณ ๊ธ‰ ์‚ฌ์šฉ์ž๊นŒ์ง€ ๋ชจ๋‘๊ฐ€ ์ดํ•ดํ•˜๊ณ  ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๋•๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ธฐ๋ณธ ๊ฐœ๋…, ํ•ต์‹ฌ ์†์„ฑ, ์‹ค์ „ ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด CSS Grid๋ฅผ ์™„๋ฒฝํžˆ ๋งˆ์Šคํ„ฐํ•ด ๋ณด์„ธ์š”.

css3 grid

ํšจ๊ณผ์ ์ธ ๋ ˆ์ด์•„์›ƒ์„ ์œ„ํ•œ CSS ์†์„ฑ ์ค‘์—๋Š” Flexbox์™€ Grid๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. CSS Grid์™€ Flexbox์˜ ์ฐจ์ด์ ์€ Flexbox๋Š” 1์ฐจ์› ๋ ˆ์ด์•„์›ƒ์„ ๋‹ค๋ฃจ๋Š” ๋ฐ˜๋ฉด, CSS Grid๋Š” 2์ฐจ์› ๋ ˆ์ด์•„์›ƒ์„ ๋‹ค๋ฃน๋‹ˆ๋‹ค. ๊ฐ๊ฐ์˜ ์žฅ์ ์„ ํ™œ์šฉํ•˜์—ฌ ๋‘ ๊ธฐ์ˆ ์„ ํ˜ผํ•ฉํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Flexbox์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ์•„๋ž˜ ํฌ์ŠคํŒ…์„ ํ™•์ธํ•ด ์ฃผ์„ธ์š”! ๐Ÿ˜

" "

[CSS3]Flexbox ๋งˆ์Šคํ„ฐํ•˜๊ธฐ: CSS3๋กœ ๋ฐ˜์‘ํ˜• ์›น์‚ฌ์ดํŠธ ๋งŒ๋“ค๊ธฐ

CSS3 FLEXBOX๋Š” ์›น ํŽ˜์ด์ง€ ๋ ˆ์ด์•„์›ƒ์„ ์‰ฝ๊ฒŒ ์กฐ์ •ํ•˜๊ณ  ๋‹ค์–‘ํ•œ ๋””๋ฐ”์ด์Šค์— ๋งž์ถฐ ๋ฐ˜์‘ํ˜• ์›น์‚ฌ์ดํŠธ๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” FLEXBOX์˜ ๊ธฐ๋ณธ ๊ฐœ๋…๋ถ€ํ„ฐ ๊ณ ๊ธ‰ ํ™œ์šฉ ๋ฐฉ๋ฒ•๊นŒ์ง€ ์ฒด๊ณ„์ ์œผ๋กœ

creativevista.tistory.com

CSS Grid๋ž€ ๋ฌด์—‡์ธ๊ฐ€?๐ŸŒŸ

CSS Grid์˜ ๊ฐœ์š”์™€ ์ค‘์š”์„ฑ

CSS Grid๋Š” ์›น๋””์ž์ธ์„ ์œ„ํ•œ 2์ฐจ์› ๋ ˆ์ด์•„์›ƒ ์‹œ์Šคํ…œ์ž…๋‹ˆ๋‹ค. ํ”Œ๋ ‰์Šค๋ฐ•์Šค(Flexbox)์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋ฉด ๋ณต์žกํ•œ ๋ ˆ์ด์•„์›ƒ๋„ ์‰ฝ๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. CSS Grid๋Š” ํ–‰(row)๊ณผ ์—ด(column)์„ ๋™์‹œ์— ์ œ์–ดํ•  ์ˆ˜ ์žˆ์–ด ๋ฐ˜์‘ํ˜• ๋””์ž์ธ์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐ ํƒ์›”ํ•ฉ๋‹ˆ๋‹ค.

CSS Grid์˜ ์žฅ์ 

  • 2์ฐจ์› ๋ ˆ์ด์•„์›ƒ์„ ์†์‰ฝ๊ฒŒ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๊ฐ„๊ฒฐํ•˜๊ณ  ์ง๊ด€์ ์ธ ์ฝ”๋“œ๋กœ ๋ณต์žกํ•œ ๋ ˆ์ด์•„์›ƒ์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๋ฐ˜์‘ํ˜• ๋””์ž์ธ์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐ ์œ ๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

CSS Grid์˜ ๊ธฐ๋ณธ ๊ฐœ๋…๐Ÿ“š

  • Grid Container: CSS Grid๋ฅผ ์ ์šฉํ•˜๋Š” ์ปจํ…Œ์ด๋„ˆ ์š”์†Œ์ž…๋‹ˆ๋‹ค.
  • Grid Item: Grid Container ๋‚ด์— ์žˆ๋Š” ์ž์‹ ์š”์†Œ๋“ค์ž…๋‹ˆ๋‹ค.
  • Row: CSS Grid์—์„œ ์ˆ˜์ง ๋ฐฉํ–ฅ์œผ๋กœ ๋ ˆ์ด์•„์›ƒ์„ ๋‚˜๋ˆ„๋Š” ์š”์†Œ๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. ํ–‰์€ ๊ทธ๋ฆฌ๋“œ ์ปจํ…Œ์ด๋„ˆ์˜ grid-template-rows ์†์„ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ •์˜๋ฉ๋‹ˆ๋‹ค.
  • Column: CSS Grid์—์„œ ์ˆ˜ํ‰ ๋ฐฉํ–ฅ์œผ๋กœ ๋ ˆ์ด์•„์›ƒ์„ ๋‚˜๋ˆ„๋Š” ์š”์†Œ๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. ์—ด์€ ๊ทธ๋ฆฌ๋“œ ์ปจํ…Œ์ด๋„ˆ์˜ grid-template-columns ์†์„ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ •์˜๋ฉ๋‹ˆ๋‹ค.
  • Grid Cell: ๊ทธ๋ฆฌ๋“œ์˜ ํ•œ ์นธ์„ ๊ฐ€๋ฆฌํ‚ค๋ฉฐ, ํŠน์ • ํ–‰๊ณผ ์—ด์ด ๋งŒ๋‚˜๋Š” ์ง€์ ์ž…๋‹ˆ๋‹ค.
  • Grid Gap: ๊ทธ๋ฆฌ๋“œ ์…€ ์‚ฌ์ด์˜ ๊ฐ„๊ฒฉ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ํ–‰๊ณผ ์—ด ์‚ฌ์ด์˜ ๊ฐ„๊ฒฉ์„ ์„ค์ •ํ•  ๋•Œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
  • Grid Item: ๊ทธ๋ฆฌ๋“œ ์•ˆ์— ๋“ค์–ด๊ฐ€๋Š” ๊ฐ๊ฐ์˜ ์š”์†Œ๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๋“œ ์•„์ดํ…œ์€ ๊ทธ๋ฆฌ๋“œ ์ปจํ…Œ์ด๋„ˆ์— ์˜ํ•ด ๋ฐฐ์น˜๋ฉ๋‹ˆ๋‹ค.
  • Grid Line: ๊ทธ๋ฆฌ๋“œ ์…€์„ ๊ฐ€๋กœ์ง€๋ฅด๋Š” ์ˆ˜์ง ๋˜๋Š” ์„ธ๋กœ ์„ ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๋“œ ๋ผ์ธ์€ ๊ฐ ์—ด๊ณผ ํ–‰ ์‚ฌ์ด์˜ ๊ฒฝ๊ณ„๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.
  • Grid Number: ๊ทธ๋ฆฌ๋“œ ์•„์ดํ…œ์ด๋‚˜ ์…€์„ ์‹๋ณ„ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ๋ฒˆํ˜ธ๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. ์ฃผ๋กœ ํ–‰๊ณผ ์—ด์˜ ์ธ๋ฑ์Šค๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

css3 grid

CSS Grid์˜ ๊ธฐ๋ณธ ์†์„ฑ๐Ÿ”ฎ

display: grid

  • Grid ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
  • ์ด ์†์„ฑ์„ ์‚ฌ์šฉํ•˜๋ฉด ์ปจํ…Œ์ด๋„ˆ์˜ ์ž์‹ ์š”์†Œ๋“ค์ด ๊ทธ๋ฆฌ๋“œ ์•„์ดํ…œ์œผ๋กœ ๋ฐฐ์น˜๋ฉ๋‹ˆ๋‹ค.
.container {
  display: grid;
}

grid-template-column๊ณผ grid-template-rows

  • grid-template-columns: ์—ด์˜ ์ˆ˜์™€ ๋„ˆ๋น„๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
  • grid-template-rows: ํ–‰์˜ ์ˆ˜์™€ ๋†’์ด๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
  • ํ”ฝ์…€, ๋ฐฑ๋ถ„์œจ, fr ๋‹จ์œ„ ๋“ฑ์œผ๋กœ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
.container {
  grid-template-columns: 100px 200px 1fr;
  grid-template-rows: 50px auto;
}

โญfr ๋‹จ์œ„โญ

fr ๋‹จ์œ„๋Š” CSS Grid์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์ƒ๋Œ€์ ์ธ ๋‹จ์œ„ ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. ์ด๋Š” "fraction"์˜ ์•ฝ์ž๋กœ, ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๊ณต๊ฐ„์„ ๊ทธ๋ฆฌ๋“œ ์ปจํ…Œ์ด๋„ˆ ๋‚ด์—์„œ ๋น„์œจ์— ๋”ฐ๋ผ ๋ถ„๋ฐฐํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

fr ๋‹จ์œ„๋Š” ๊ทธ๋ฆฌ๋“œ ์ปจํ…Œ์ด๋„ˆ์˜ ๋‚จ์€ ๊ณต๊ฐ„์„ ๋ถ„ํ• ํ•˜์—ฌ ์„ค์ •๋œ ๋น„์œจ์— ๋”ฐ๋ผ ๊ฐ ์—ด ๋˜๋Š” ํ–‰์— ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋ฐ˜์‘ํ˜• ๋””์ž์ธ์„ ์œ„ํ•ด ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

grid-gap

  • ๊ทธ๋ฆฌ๋“œ ์•„์ดํ…œ ์‚ฌ์ด์˜ ๊ฐ„๊ฒฉ์„ ์ง€์ •ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ๋‹จ์ถ• ์†์„ฑ์ž…๋‹ˆ๋‹ค. ์ด ๋‹จ์ถ• ์†์„ฑ์€ row-gap๊ณผ column-gap์˜ ์ถ•์•ฝํ˜•์ž…๋‹ˆ๋‹ค.
  • grid-gap: ๊ทธ๋ฆฌ๋“œ ์•„์ดํ…œ ์‚ฌ์ด์˜ ๊ฐ„๊ฒฉ์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. row-gap ๋ฐ column-gap ๊ฐ’์„ ๋™์‹œ์— ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • column-gap: ๊ทธ๋ฆฌ๋“œ ์•„์ดํ…œ ์‚ฌ์ด์˜ ์ˆ˜ํ‰ ๊ฐ„๊ฒฉ์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
  • row-gap: ๊ทธ๋ฆฌ๋“œ ์•„์ดํ…œ ์‚ฌ์ด์˜ ์ˆ˜์ง ๊ฐ„๊ฒฉ์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
.container {
  grid-gap: 10px;
}

grid-template-areas

  • ์ด๋ฆ„์„ ์ง€์ •ํ•œ ๊ทธ๋ฆฌ๋“œ ์˜์—ญ์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
.container {
  grid-template-areas:
    "header header header"
    "sidebar main main"
    "footer footer footer";
}

CSS Grid์˜ ์ •๋ ฌ ์†์„ฑ↔

justify-items

  • ๊ทธ๋ฆฌ๋“œ ์ปจํ…Œ์ด๋„ˆ ๋‚ด์˜ ๋ชจ๋“  ๊ทธ๋ฆฌ๋“œ ์•„์ดํ…œ์˜ ์ˆ˜ํ‰ ์ •๋ ฌ์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
  • ๊ฐ’: start, end, center, stretch (๊ธฐ๋ณธ๊ฐ’)
.container {
  display: grid;
  justify-items: center; /* ๋ชจ๋“  ์•„์ดํ…œ์„ ์ˆ˜ํ‰ ์ค‘์•™์— ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค. */
}

css3 grid
css3 grid
css3 grid
css3 grid

align-items

  • ๊ทธ๋ฆฌ๋“œ ์ปจํ…Œ์ด๋„ˆ ๋‚ด์˜ ๋ชจ๋“  ๊ทธ๋ฆฌ๋“œ ์•„์ดํ…œ์˜ ์ˆ˜์ง ์ •๋ ฌ์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
  • ๊ฐ’: start, end, center, stretch (๊ธฐ๋ณธ๊ฐ’)
.container {
  display: grid;
  align-items: center; /* ๋ชจ๋“  ์•„์ดํ…œ์„ ์ˆ˜์ง ์ค‘์•™์— ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค. */
}

css3 grid
css3 grid
css3 grid
css3 grid

justify-content

  • ๊ทธ๋ฆฌ๋“œ ์ปจํ…Œ์ด๋„ˆ ๋‚ด์˜ ๊ทธ๋ฆฌ๋“œ ํŠธ๋ž™(์ „์ฒด ๊ทธ๋ฆฌ๋“œ)์„ ์ˆ˜ํ‰ ๋ฐฉํ–ฅ์œผ๋กœ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.
  • ๊ฐ’: start, end, center, stretch, space-between, space-around, space-evenly
.container {
  display: grid;
  justify-content: space-between; /* ์ „์ฒด ๊ทธ๋ฆฌ๋“œ๋ฅผ ์ˆ˜ํ‰ ๋ฐฉํ–ฅ์œผ๋กœ ๊ท ๋“ฑํ•˜๊ฒŒ ๋ถ„๋ฐฐํ•ฉ๋‹ˆ๋‹ค. */
}

align-content

  • ๊ทธ๋ฆฌ๋“œ ์ปจํ…Œ์ด๋„ˆ ๋‚ด์˜ ๊ทธ๋ฆฌ๋“œ ํŠธ๋ž™(์ „์ฒด ๊ทธ๋ฆฌ๋“œ)์„ ์ˆ˜์ง ๋ฐฉํ–ฅ์œผ๋กœ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๋“œ ์ปจํ…Œ์ด๋„ˆ์— ์—ฌ๋ถ„์˜ ๊ณต๊ฐ„์ด ์žˆ์„ ๋•Œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
  • ๊ฐ’: start, end, center, stretch, space-between, space-around, space-evenly
.container {
  display: grid;
  align-content: center; /* ์ „์ฒด ๊ทธ๋ฆฌ๋“œ๋ฅผ ์ˆ˜์ง ๋ฐฉํ–ฅ์œผ๋กœ ์ค‘์•™์— ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค. */
}

justify-self

  • ๊ฐœ๋ณ„ ๊ทธ๋ฆฌ๋“œ ์•„์ดํ…œ์˜ ์ˆ˜ํ‰ ์ •๋ ฌ์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. justify-items๋ณด๋‹ค ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’์Šต๋‹ˆ๋‹ค.
  • ๊ฐ’: start, end, center, stretch
.item {
  justify-self: end; /* ๊ฐœ๋ณ„ ์•„์ดํ…œ์„ ์ˆ˜ํ‰ ๋ฐฉํ–ฅ ๋์— ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค. */
}

align-self

  • ๊ฐœ๋ณ„ ๊ทธ๋ฆฌ๋“œ ์•„์ดํ…œ์˜ ์ˆ˜์ง ์ •๋ ฌ์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. align-items๋ณด๋‹ค ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’์Šต๋‹ˆ๋‹ค.
  • ๊ฐ’: start, end, center, stretch
.item {
  align-self: end; /* ๊ฐœ๋ณ„ ์•„์ดํ…œ์„ ์ˆ˜์ง ๋ฐฉํ–ฅ ๋์— ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค. */
}

CSS Grid ๋ ˆ์ด์•„์›ƒ ๋ฐฐ์น˜ ์†์„ฑ๐Ÿงฉ

grid-column-start

  • ๊ทธ๋ฆฌ๋“œ ์•„์ดํ…œ์ด ์‹œ์ž‘ํ•  ์—ด ๋ผ์ธ์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
  • ๊ฐ’: ๊ทธ๋ฆฌ๋“œ ๋ผ์ธ ๋ฒˆํ˜ธ, ๊ทธ๋ฆฌ๋“œ ๋ผ์ธ ์ด๋ฆ„, span <number> (์ง€์ •๋œ ์ˆ˜์˜ ํŠธ๋ž™์„ ์ฐจ์ง€), auto (๊ธฐ๋ณธ๊ฐ’)
.item {
  grid-column-start: 2; /* ๋‘ ๋ฒˆ์งธ ์—ด ๋ผ์ธ์—์„œ ์‹œ์ž‘ */
}

grid-column-end

  • ๊ทธ๋ฆฌ๋“œ ์•„์ดํ…œ์ด ๋๋‚˜๋Š” ์—ด ๋ผ์ธ์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
  • ๊ฐ’: ๊ทธ๋ฆฌ๋“œ ๋ผ์ธ ๋ฒˆํ˜ธ, ๊ทธ๋ฆฌ๋“œ ๋ผ์ธ ์ด๋ฆ„, span <number> (์ง€์ •๋œ ์ˆ˜์˜ ํŠธ๋ž™์„ ์ฐจ์ง€), auto (๊ธฐ๋ณธ๊ฐ’)
.item {
  grid-column-end: 4; /* ๋„ค ๋ฒˆ์งธ ์—ด ๋ผ์ธ์—์„œ ๋ */
}

grid-row-start

  • ๊ทธ๋ฆฌ๋“œ ์•„์ดํ…œ์ด ์‹œ์ž‘ํ•  ํ–‰ ๋ผ์ธ์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
  • ๊ฐ’: ๊ทธ๋ฆฌ๋“œ ๋ผ์ธ ๋ฒˆํ˜ธ, ๊ทธ๋ฆฌ๋“œ ๋ผ์ธ ์ด๋ฆ„, span <number> (์ง€์ •๋œ ์ˆ˜์˜ ํŠธ๋ž™์„ ์ฐจ์ง€), auto (๊ธฐ๋ณธ๊ฐ’)
.item {
  grid-row-start: 1; /* ์ฒซ ๋ฒˆ์งธ ํ–‰ ๋ผ์ธ์—์„œ ์‹œ์ž‘ */
}

grid-row-end

  • ๊ทธ๋ฆฌ๋“œ ์•„์ดํ…œ์ด ๋๋‚˜๋Š” ํ–‰ ๋ผ์ธ์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
  • ๊ฐ’: ๊ทธ๋ฆฌ๋“œ ๋ผ์ธ ๋ฒˆํ˜ธ, ๊ทธ๋ฆฌ๋“œ ๋ผ์ธ ์ด๋ฆ„, span <number> (์ง€์ •๋œ ์ˆ˜์˜ ํŠธ๋ž™์„ ์ฐจ์ง€), auto (๊ธฐ๋ณธ๊ฐ’)
.item {
  grid-row-end: 3; /* ์„ธ ๋ฒˆ์งธ ํ–‰ ๋ผ์ธ์—์„œ ๋ */
}

CSS Grid์˜ ๊ณ ๊ธ‰ ๊ธฐ๋Šฅ๐Ÿš€

grid line, gridarea

  • grid line: ๊ทธ๋ฆฌ๋“œ ์•„์ดํ…œ์„ ๋ฐฐ์น˜ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๊ฐ€์ƒ์˜ ์„ ์ž…๋‹ˆ๋‹ค.
  • grid area: ์—ฌ๋Ÿฌ ์…€์„ ํ•˜๋‚˜์˜ ์˜์—ญ์œผ๋กœ ๋ฌถ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
.container {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    grid-template-rows: 100px 100px;
    gap: 10px;
}

.item1 {
    grid-column: 1 / 3; /* 1๋ฒˆ์งธ ์—ด๋ถ€ํ„ฐ 2๋ฒˆ์งธ ์—ด๊นŒ์ง€ */
    grid-row: 1 / 2; /* 1๋ฒˆ์งธ ํ–‰ */
}

.item2 {
    grid-area: 1 / 3 / 2 / 5; /* grid-row-start / grid-column-start / grid-row-end / grid-column-end */
}

grid-auto-flow

  • ๊ทธ๋ฆฌ๋“œ ์•„์ดํ…œ์ด ์ž๋™์œผ๋กœ ๋ฐฐ์น˜๋˜๋Š” ๋ฐฉํ–ฅ์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ์ˆ˜ํ‰ ๋ฐฉํ–ฅ์ด๋“  ์ˆ˜์ง ๋ฐฉํ–ฅ์ด๋“ , ๋˜๋Š” ์ˆ˜ํ‰-์ˆ˜์ง ๋ฐฉํ–ฅ์œผ๋กœ ๋ฐฐ์น˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. row, column, dense ์ค‘ ํ•˜๋‚˜์˜ ๊ฐ’์œผ๋กœ ๊ทธ๋ฆฌ๋“œ ์•„์ดํ…œ์˜ ๋ฐฐ์น˜ ๋ฐฉํ–ฅ์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
  • row: ๊ทธ๋ฆฌ๋“œ ์•„์ดํ…œ์„ ํ–‰ ๋‹จ์œ„๋กœ ๋ฐฐ์น˜ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๋“œ ์ปจํ…Œ์ด๋„ˆ์˜ ํฌ๊ธฐ๊ฐ€ ์ถฉ๋ถ„ํžˆ ํฌ๋‹ค๋ฉด, ๊ทธ๋ฆฌ๋“œ ์•„์ดํ…œ์ด ๊ฐ€๋กœ๋กœ ๋จผ์ € ์ฑ„์›Œ์ง‘๋‹ˆ๋‹ค. ๊ทธ ํ›„์— ๋‹ค์Œ ํ–‰์œผ๋กœ ๋„˜์–ด๊ฐ€๋ฉด์„œ ์ฑ„์›Œ์ง‘๋‹ˆ๋‹ค.
  • column: ๊ทธ๋ฆฌ๋“œ ์•„์ดํ…œ์„ ์—ด ๋‹จ์œ„๋กœ ๋ฐฐ์น˜ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๋“œ ์ปจํ…Œ์ด๋„ˆ์˜ ํฌ๊ธฐ๊ฐ€ ์ถฉ๋ถ„ํžˆ ํฌ๋‹ค๋ฉด, ๊ทธ๋ฆฌ๋“œ ์•„์ดํ…œ์ด ์„ธ๋กœ๋กœ ๋จผ์ € ์ฑ„์›Œ์ง‘๋‹ˆ๋‹ค. ๊ทธ ํ›„์— ๋‹ค์Œ ์—ด๋กœ ๋„˜์–ด๊ฐ€๋ฉด์„œ ์ฑ„์›Œ์ง‘๋‹ˆ๋‹ค.
  • dense: ๊ฐ€๋Šฅํ•œ ํ•œ ๋งŽ์€ ๊ณต๊ฐ„์„ ์ฑ„์šฐ๋„๋ก ๊ทธ๋ฆฌ๋“œ ์•„์ดํ…œ์„ ๋ฐ€์–ด ๋„ฃ์Šต๋‹ˆ๋‹ค.๋งŒ์•ฝ ๊ทธ๋ฆฌ๋“œ ์•„์ดํ…œ ์‚ฌ์ด์— ๊ณต๊ฐ„์ด ๋น„์–ด ์žˆ๋‹ค๋ฉด, ๊ทธ๋ฆฌ๋“œ ๋ผ์ธ์— ๋”ฐ๋ผ ๋” ์ฑ„์›Œ์ง€๋„๋ก ๋ฐ€์–ด ๋„ฃ์Šต๋‹ˆ๋‹ค.

์ด ์ฝ”๋“œ๋Š” dense ๊ฐ’์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ทธ๋ฆฌ๋“œ ์•„์ดํ…œ์„ ๋ฐฐ์น˜ํ•˜๋Š” ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๋“œ ์ปจํ…Œ์ด๋„ˆ๋Š” 4๊ฐœ์˜ ์—ด๋กœ ๊ตฌ์„ฑ๋˜๋ฉฐ, ๊ทธ๋ฆฌ๋“œ ์•„์ดํ…œ์€ grid-auto-flow: dense; ์†์„ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ€๋Šฅํ•œ ๊ณต๊ฐ„์„ ์ตœ๋Œ€ํ•œ ํ™œ์šฉํ•˜์—ฌ ๋ฐฐ์น˜๋ฉ๋‹ˆ๋‹ค.

<style>
.container {
    background-color: wheat;
    padding: 10px;
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    grid-auto-rows: minmax(100px, auto);
    grid-auto-flow: dense; /* dense ๊ฐ’์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ทธ๋ฆฌ๋“œ ์•„์ดํ…œ์„ ๋ฐ€์–ด๋„ฃ์Šต๋‹ˆ๋‹ค. */
}

.item {
    background-color: violet;
    border: 3px solid #000;
}

.item:nth-child(1) {
    grid-column: 1 / span 3; /* ์ฒซ ๋ฒˆ์งธ ์•„์ดํ…œ์„ ๋„“์€ ์˜์—ญ์œผ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. */
}

.item:nth-child(2) {
    grid-row: 2 / span 3; /* ๋‘ ๋ฒˆ์งธ ์•„์ดํ…œ์„ ์—ฌ๋Ÿฌ ํ–‰์— ๊ฑธ์ณ ๋ฐฐ์น˜ํ•ฉ๋‹ˆ๋‹ค. */
}

.item:nth-child(3) {
    grid-column: 2 / span 3; /* ์„ธ ๋ฒˆ์งธ ์•„์ดํ…œ์„ ๋„“์€ ์˜์—ญ์œผ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. */
}
</style>


<body>
  <div class="container">
    <div class="item">grid-1</div>
    <div class="item">grid-2</div>
    <div class="item">grid-3</div>
    <div class="item">grid-4</div>
    <div class="item">grid-5</div>
    <div class="item">grid-6</div>
    <div class="item">grid-7</div>
    <div class="item">grid-8</div>
    <div class="item">grid-9</div>
    <div class="item">grid-10</div>
    <div class="item">grid-11</div>
    <div class="item">grid-12</div>
  </div>
</body>

css3 grid

auto-fill

  • ๊ทธ๋ฆฌ๋“œ ์ปจํ…Œ์ด๋„ˆ์— ์ •์˜๋œ ํฌ๊ธฐ์˜ ์—ฌ์œ  ๊ณต๊ฐ„์ด ์žˆ์„ ๊ฒฝ์šฐ, ๊ทธ ๊ณต๊ฐ„์„ ์ฑ„์šฐ๊ธฐ ์œ„ํ•ด ๊ทธ๋ฆฌ๋“œ ์•„์ดํ…œ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋Š˜๋ฆฝ๋‹ˆ๋‹ค.
  • ๊ทธ๋ฆฌ๋“œ ํŠธ๋ž™์˜ ๊ฐœ์ˆ˜๋ฅผ ์ง€์ •๋œ ํฌ๊ธฐ๋กœ ์ฑ„์šฐ๊ธฐ ์œ„ํ•ด ์ž๋™์œผ๋กœ ์กฐ์ •ํ•ฉ๋‹ˆ๋‹ค.
.container {
  grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
}

auto-fit

  • ๊ทธ๋ฆฌ๋“œ ์ปจํ…Œ์ด๋„ˆ์— ์ •์˜๋œ ํฌ๊ธฐ์˜ ์—ฌ์œ  ๊ณต๊ฐ„์ด ์žˆ์„ ๊ฒฝ์šฐ, ๊ทธ ๊ณต๊ฐ„์„ ์ฑ„์šฐ๊ธฐ ์œ„ํ•ด ๊ทธ๋ฆฌ๋“œ ์•„์ดํ…œ์˜ ํฌ๊ธฐ๋ฅผ ๋Š˜๋ฆฝ๋‹ˆ๋‹ค. ์ด๋Š” ๋ฏธ๋””์–ด ์ฟผ๋ฆฌ์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ˜์‘ํ˜• ๋ ˆ์ด์•„์›ƒ์„ ๊ตฌ์„ฑํ•˜๋Š” ๋ฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.
  • ๊ทธ๋ฆฌ๋“œ ํŠธ๋ž™์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ทธ๋ฆฌ๋“œ ์ปจํ…Œ์ด๋„ˆ์˜ ํฌ๊ธฐ์— ๋งž์ถฐ ์ž๋™์œผ๋กœ ์กฐ์ •ํ•ฉ๋‹ˆ๋‹ค.
.container {
  grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
}

minmax(์ตœ์†Œ ๊ฐ’, ์ตœ๋Œ€ ๊ฐ’)

  • ๊ทธ๋ฆฌ๋“œ ์•„์ดํ…œ์˜ ์ตœ์†Œ ํฌ๊ธฐ์™€ ์ตœ๋Œ€ ํฌ๊ธฐ๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ๊ทธ๋ฆฌ๋“œ ํŠธ๋ž™์˜ ํฌ๊ธฐ๋ฅผ ์ง€์ •ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
  • ์†์„ฑ ๊ฐ’์œผ๋กœ auto๊ฐ’์„ ์ง€์ •ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ๊ทธ๋ฆฌ๋“œ ์ปจํ…Œ์ด๋„ˆ์— ๋งž์ถฐ ์ž๋™์œผ๋กœ ํฌ๊ธฐ๊ฐ€ ์กฐ์ •๋ฉ๋‹ˆ๋‹ค.
.container {
  grid-template-columns: minmax(100px, auto) 1fr;
}

repeat(๋ฐ˜๋ณต ํšŸ์ˆ˜, ๋ฐ˜๋ณต ๊ฐ’)

  • ๊ทธ๋ฆฌ๋“œ ํ…œํ”Œ๋ฆฟ์—์„œ ๋ฐ˜๋ณต๋˜๋Š” ํŒจํ„ด์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ๋ฐ˜๋ณต๋˜๋Š” ํ–‰ ๋˜๋Š” ์—ด์˜ ํฌ๊ธฐ๋ฅผ ์ •์˜ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
.container {
  grid-template-columns: repeat(3, 100px);
}

์ค‘์ฒฉ ๊ทธ๋ฆฌ๋“œ

<div class="outer-container">
  <div class="inner-container">
    <div class="inner-item">A</div>
    <div class="inner-item">B</div>
  </div>
</div>

<style>
  .outer-container {
    display: grid;
    grid-template-columns: 1fr;
  }
  .inner-container {
    display: grid;
    grid-template-columns: 1fr 1fr;
  }
</style>

Grid์™€ Flexbox์˜ ํ˜ผํ•ฉ ์‚ฌ์šฉ

.container {
  display: grid;
  grid-template-columns: 2fr 1fr;
}
.flex-item {
  display: flex;
  justify-content: center;
  align-items: center;
}

๋ฐ˜์‘ํ˜• ๋ ˆ์ด์•„์›ƒ ๋งŒ๋“ค๊ธฐ๐Ÿ“ฑ

๋ฏธ๋””์–ด ์ฟผ๋ฆฌ๋ฅผ ํ™œ์šฉํ•œ ๊ทธ๋ฆฌ๋“œ ์กฐ์ •

.container {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
    gap: 10px;
}

@media (max-width: 600px) {
    .container {
        grid-template-columns: 1fr;
    }
}

๋‹ค์–‘ํ•œ ํ™”๋ฉด ํฌ๊ธฐ์— ๋Œ€์‘ํ•˜๋Š” ๋ฐฉ๋ฒ•:

  • auto-fill: ๊ฐ€๋Šฅํ•œ ๋งŽ์€ ๊ทธ๋ฆฌ๋“œ ์…€์„ ์ฑ„์›๋‹ˆ๋‹ค.
  • minmax(100px, 1fr): ๊ฐ ์…€์˜ ์ตœ์†Œ ํฌ๊ธฐ๋Š” 100px, ์ตœ๋Œ€ ํฌ๊ธฐ๋Š” 1fr์ž…๋‹ˆ๋‹ค.

๋‹ค์–‘ํ•œ ์˜ˆ์ œ๐Ÿ› ๏ธ

๊ธฐ๋ณธ ๊ทธ๋ฆฌ๋“œ ์„ค์ •

<div class="container">
  <div class="item">1</div>
  <div class="item">2</div>
  <div class="item">3</div>
</div>

<style>
  .container {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-gap: 10px;
  }
  .item {
    background-color: lightblue;
    padding: 20px;
    text-align: center;
  }
</style>

์ •๋ ฌ๊ณผ ๋ฐฐ์น˜

.container {
  justify-items: center;
  align-items: center;
}

๊ฒฉ์ž ๊ฐ„๊ฒฉ ์กฐ์ •

.container {
  grid-gap: 20px;
}

๋ธ”๋กœ๊ทธ ๋ ˆ์ด์•„์›ƒ

<div class="blog-layout">
  <header class="header">Header</header>
  <nav class="sidebar">Sidebar</nav>
  <main class="main-content">Main Content</main>
  <footer class="footer">Footer</footer>
</div>

<style>
  .blog-layout {
    display: grid;
    grid-template-areas: 
      "header header header"
      "sidebar main main"
      "footer footer footer";
    grid-gap: 10px;
  }
  .header { grid-area: header; }
  .sidebar { grid-area: sidebar; }
  .main-content { grid-area: main; }
  .footer { grid-area: footer; }
</style>

๋””๋ฒ„๊น…๊ณผ ์ตœ์ ํ™”๐Ÿ›ก๏ธ

CSS Grid ๋””๋ฒ„๊น… ๋„๊ตฌ ์†Œ๊ฐœ (๋ธŒ๋ผ์šฐ์ € ๊ฐœ๋ฐœ์ž ๋„๊ตฌ)

๋ธŒ๋ผ์šฐ์ € ๊ฐœ๋ฐœ์ž ๋„๊ตฌ์—์„œ๋Š” ๊ทธ๋ฆฌ๋“œ ๋ ˆ์ด์•„์›ƒ์„ ์‹œ๊ฐ์ ์œผ๋กœ ํ™•์ธํ•˜๊ณ  ๋””๋ฒ„๊น…ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Firefox์™€ Chrome์˜ ๊ฐœ๋ฐœ์ž ๋„๊ตฌ์—์„œ๋Š” ๊ทธ๋ฆฌ๋“œ ์˜ค๋ฒ„๋ ˆ์ด ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

์„ฑ๋Šฅ ์ตœ์ ํ™”๋ฅผ ์œ„ํ•œ ํŒ

  • ํ•„์š” ์—†๋Š” ์†์„ฑ์€ ์ œ๊ฑฐํ•˜์—ฌ ์ฝ”๋“œ ์ตœ์ ํ™”
  • ๋ฐ˜๋ณต๋˜๋Š” ํŒจํ„ด์€ repeat()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ„๊ฒฐํ•˜๊ฒŒ

ํ•ต์‹ฌ ๋‚ด์šฉ๐Ÿ‘€

css3 grid
css3 grid
css3 grid
css3 grid

728x90
๋ฐ˜์‘ํ˜•