๐Ÿผ Grid-box

KHWยท2021๋…„ 9์›” 6์ผ
0

CSS

๋ชฉ๋ก ๋ณด๊ธฐ
11/16
post-custom-banner

๐ŸŒŽ Grid

  • ๋ผ์ธ(gutter) : ์•„์ดํ…œ ์‚ฌ์ด์˜ ๊ฐ„๊ฒฉ => ๋ฐ–๊ณผ ์•„์ดํ…œ ์‚ฌ์ด๋„ ํฌํ•จ
    (1๋ถ€ํ„ฐ ์‹œ์ž‘)

  • item : ๊ฐ๊ฐ์˜ item์— ๋Œ€ํ•ด์„œ ์ฒ˜๋ฆฌ

  • content : ๊ฐ๊ฐ์˜ item์„ ํฌํ•จํ•œ ์ „์ฒด๋ฅผ ๋‚˜ํƒ€๋ƒ„

  • grid๋Š” ๊ฐ€๋กœ๋Š” ์ตœ๋Œ€๋กœ ๋Š˜์–ด๋‚˜๋ ค๊ณ  ์„ธ๋กœ๋Š” ์ตœ๋Œ€ํ•œ ์ค„์–ด๋“ค๋ ค๊ณ ํ•œ๋‹ค
    (px์ด ์กด์žฌํ•˜๋ฉด ํ•ด๋‹น px๋งŒํผ ๊ฐ€๋กœ๋Š” ์ตœ๋Œ€ํ•œ ๋Š˜๊ณ  ์„ธ๋กœ๋Š” ์ตœ๋Œ€ํ•œ ์ค„์–ด๋“ ๋‹ค.)

  • ๋ช…์‹œํ•˜์ง€ ์•Š์œผ๋ฉด span 1์ด ๊ธฐ๋ณธ๊ฐ’์ž…๋‹ˆ๋‹ค.
    row-start๊ฐ€ 2์ด๊ณ  row-end๊ฐ€ span 2๋ผ๋ฉด ํŽผ์ณ์ง€๋Š” ์˜์—ญ์€ 2 3 4๊นŒ์ง€์˜ ๋„ค๋ชจ 2๊ฐœ์˜ ์˜์—ญ์ด ํŽผ์ณ์ง„๋‹ค.
    (๊ธฐ๋ณธ๊ฐ’์ด๋ฉด ๋‹น์—ฐํžˆ 2,3๊นŒ์ง€์˜ ์˜์—ญ๋งŒ ํŽผ์ณ์ง€๋Š” 1๊ฐœ์˜ ์˜์—ญ)

๊ฐœ๋…์ •๋ฆฌ

justify -> ์ˆ˜ํ‰(ํ–‰)
align -> ์ˆ˜์ง(์—ด)

  • stretch๋กœ ๋ณด๋ฉด ์–ด๋””๋กœ ๋Š˜์–ด๋‚˜๋Š”์ง€๋ฅผ ํ™•์ธ ๊ฐ€๋Šฅํ•˜๋‹ค
    justify๋Š” ์ˆ˜ํ‰์œผ๋กœ ๋Š˜์–ด๋‚˜๊ณ  align์€ ์ˆ˜์ง์œผ๋กœ ๋Š˜์–ด๋‚œ๋‹ค.

๊ฐ€๋ณ€๊ธธ์ด : ๋ธŒ๋ผ์šฐ์ €์˜ ์ถ•์†Œํ™•๋Œ€์— ๋”ฐ๋ผ ์˜ํ–ฅ์„ ๋ฐ›๋Š” ๊ฒƒ 1fr
๊ณ ์ •๊ธธ์ด : ๋ธŒ๋ผ์šฐ์ €์˜ ์ถ•์†Œํ™•๋Œ€์— ๋”ฐ๋ผ ์˜ํ–ฅ์„ ๋ฐ›์ง€์•Š๋Š” ๊ฒƒ ~px

๐ŸŒ• Grid ์ปจํ…Œ์ด๋„ˆ ์†์„ฑ

  1. display
  2. grid-template-rows
  3. grid-template-columns
  4. grid-auto-rows
  5. grid-auto-columns
  6. grid-auto-flow
  7. justify-content : ์ „์ฒด ํฐ container ๊ฐœ๋…์œผ๋กœ ๋ณด๋Š” ์ˆ˜ํ‰(ํ–‰)์ •๋ ฌ
  8. align-content : ์ „์ฒด ํฐ container ๊ฐœ๋…์œผ๋กœ ๋ณด๋Š” ์ˆ˜์ง(์—ด)์ •๋ ฌ
  9. justify-items : ์•„์ดํ…œ์— ๋”ฐ๋ฅด๋Š” ๊ฐœ๋…์œผ๋กœ ๋ณด๋Š” ์ˆ˜ํ‰(ํ–‰)์ •๋ ฌ
  10. align-items : ์•„์ดํ…œ์— ๋”ฐ๋ฅด๋Š” ๊ฐœ๋…์œผ๋กœ ๋ณด๋Š” ์ˆ˜์ง(ํ–‰)์ •๋ ฌ
  11. grid-template-areas
  12. grid-gap

1. display

  1. grid
  2. inline-grid
  • ๊ฐ๊ฐ์˜ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ํ–‰์œผ๋กœ ๋‚˜๋ˆ„์–ด์กŒ๋ƒ / ์—ด๋กœ ๋‚˜๋ˆ„์–ด์กŒ๋ƒ

2. grid-template-rows

3. grid-template-columns

๋ช…์‹œ์  ์ง€์ • ๋Œ€์ƒ

1px ์ด๋‚˜ 1fr ์‚ฌ์šฉ

fr (fraction)

๊ณต๊ฐ„๋น„์œจ : ์ „์ฒด๋ฅผ ์ฑ„์šฐ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.
ํ•ด๋‹น ๋‚ด์šฉ์„ 1fr 1fr 1fr ... ๋Œ€์‹  repeat(ํšŸ์ˆ˜ , 1fr) ํ˜•ํƒœ๋กœ ์“ด๋‹ค.

4. grid-auto-rows

5. grid-auto-columns

์•”์‹œ์  ์ง€์ • ๋Œ€์ƒ

๋ช…์‹œ์ ์œผ๋กœ ์ง€์ •ํ•œ ๋ถ€๋ถ„ ์ด์ƒ์˜ ๋Œ€์ƒ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ์—ญํ• 
9๊ฐœ๊ฐ€ ์žˆ๋Š”๋ฐ template-rows๋กœ 2๊ฐœ template-columns๋กœ 2๊ฐœ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋ฉด
5๊ฐœ์˜ ์•”์‹œ์  ์ฒ˜๋ฆฌํ•  ๋Œ€์ƒ์ด ๋‚จ์•„์žˆ๋‹ค.

  • ๋ช…์‹œ์  ํ–‰ ์™ธ๋ถ€์— ๋ฐฐ์น˜๋˜๋Š” ๊ฒฝ์šฐ ์•”์‹œ์  ํ–‰์˜ ํฌ๊ธฐ๊ฐ€ ์ ์šฉ
    ( grid-row๋ฅผ ํ†ตํ•ด์„œ๋„ ๋ช…์‹œ์ ์—์„œ ์•”์‹œ์ ์œผ๋กœ ๋ฐ”๋€”์ˆ˜ ์žˆ๋‹ค. )

6. grid-auto-flow

  1. dense : ๋นˆ๊ณต๊ฐ„์„ ๋ฉ”์›Œ๊ฐ€๋ฉฐ ์ •๋ ฌ
  2. row(๊ธฐ๋ณธ๊ฐ’) : ํ–‰์ถ•์„ ๊ธฐ์ค€์œผ๋กœ ์Œ“์ธ๋‹ค
  3. column : ์—ด์ถ•์„ ๊ธฐ์ค€์œผ๋กœ ์Œ“์ธ๋‹ค
  4. column dense

7. justify-content

  1. normal
  2. start
  3. end
  4. center
  5. space-between
  6. space-around
  7. space-evenly
  • ์ฃผ์ถ•(ํ–‰์ถ•)์„ ๊ธฐ์ค€์œผ๋กœ ์ฒ˜๋ฆฌ

8. align-content

  1. normal
  2. start
  3. end
  4. center
  5. space-between
  6. space-around
  7. space-evenly
  • ๊ต์ฐจ์ถ•(์—ด์ถ•)์„ ๊ธฐ์ค€์œผ๋กœ ์ฒ˜๋ฆฌ

justify-content / align-content ํŠน์ง•

container๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋นˆ๊ณต๊ฐ„์ด ์กด์žฌํ•  ๋•Œ ์ ์šฉ๋œ๋‹ค.

9. justify-items

  1. normal
  2. start
  3. end
  4. center
  • ํ–‰์„ ๊ธฐ์ค€์œผ๋กœ ๊ฐ๊ฐ์˜ item์˜ ๋นˆ๊ณต๊ฐ„์— ๋Œ€ํ•ด ์ •๋ ฌ

10. align-items

  1. normal
  2. start
  3. end
  4. center
  • ์—ด์„ ๊ธฐ์ค€์œผ๋กœ ๊ฐ๊ฐ์˜ item์˜ ๋นˆ๊ณต๊ฐ„์— ๋Œ€ํ•ด ์ •๋ ฌ

content / items (์ค‘์š”)

justify-content , align-content : container ํฌ๊ธฐ ๋„“์ด๋ฅผ ๊ธฐ์ค€์œผ๋กœ item๋“ค์„ ์ฒ˜๋ฆฌ

justify-items , align-items : ๊ฐ๊ฐ์˜ items ํฌ๊ธฐ ๋„“์ด๋ฅผ ๊ธฐ์ค€์œผ๋กœ item๋“ค์„ ์ฒ˜๋ฆฌ

justify-items / align-items ํŠน์ง•

๊ทธ๋ฆฌ๋“œ ์˜์—ญ์— ์—ฌ๋Ÿฌ ์…€๋“ค์ด ์žˆ๋Š”๋ฐ ํ•œ ์…€์•ˆ์— item์ด ์…€๋ณด๋‹ค ์ž‘์•„ ๋นˆ๊ณต๊ฐ„์ด ์žˆ๋‹ค๋ฉด ์ด๋ฅผ ์ •๋ ฌํ•  ๋•Œ ์‚ฌ์šฉ
container๊ฐ€ 1fr๋กœ ์ „๋ถ€ ์ปค์ง€๊ณ  item๋“ค์ด px๋กœ ์„œ๋กœ๊ฐ„์˜ item๊ฐ„์˜ ๊ณต๋ฐฑ์ด ์žˆ์„๋•Œ ํ•ด๋‹น ๋ถ€๋ถ„์„ justify-items์™€ align-items๋กœ ํ•ด๋‹น ๊ณต๋ฐฑ์„ ์ •๋ ฌํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ชจ๋‘ ์ค‘๊ฐ„์— ์ ์šฉํ•œ ์˜ˆ์‹œ

11. grid-template-areas + grid-area

" a b c "
" d e f "
" . . f "
" a b d " ํ˜•ํƒœ

  • ""์— ์˜ํ•ด ํ–‰์„ ๊ตฌ๋ถ„ํ•œ๋‹ค. .์— ์˜ํ•ด ์ˆ˜ํ–‰ ์•ˆํ•˜๋Š” ๊ณณ์„ ์ฒ˜๋ฆฌํ•ด์ค€๋‹ค

  • ํ•ด๋‹น ์ฝ”๋“œ ์‹คํ–‰์ „์— ์ ์šฉ๋  ๋Œ€์ƒ ํƒœ๊ทธ์— grid-area๋ฅผ ์ ์šฉ์‹œํ‚จ๋‹ค.
    (.container header { grid-area : a; })

11. 12์ ์šฉ์˜ˆ์‹œ

12. grid-gap

ํ•˜๋‚˜์ด๋ฉด ์ „๋ถ€ ํ–‰์—ด ์ ์šฉ
๋‘๊ฐœ์ด๋ฉด ๊ฐ๊ฐ ํ–‰์—ด ์ ์šฉ
์ด๋ฅผ ๊ธธ๊ฒŒ์“ฐ๋ฉด
grid-row-gap
grid-column-gap ๋กœ ๊ฐ๊ฐ ์ฒ˜๋ฆฌ๊ฐ€๋Šฅ

๐ŸŒ• Grid ์•„์ดํ…œ ์†์„ฑ

  1. grid-row-start
  2. grid-row-end
  3. grid-column-start
  4. grid-column-end
  5. grid-row / grid-column
  6. justify-self
  7. align-self
  8. order
  9. z-index

1. grid-row-start

1) ์ˆซ์ž ํ•œ๊ฐœ : ์‹œ์ž‘๋ผ์ธ์„ ๊ฒฐ์ •ํ•˜๋ฉฐ end์™€ ๊ฐ™์ด ์“ฐ์ธ๋‹ค.
2) span + ์ˆซ์ž ํ•œ๊ฐœ : ๊ธฐ์กด ์œ„์น˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ–‰์œผ๋กœ ์ˆซ์ž๋งŒํผ์„ ๋‚˜๋ˆˆ ๋ถ€๋ถ„์„ ๋ฉ”์šด๋‹ค.

2. grid-row-end

1) ์ˆซ์ž ํ•œ๊ฐœ : start ๋ผ์ธ์„ ๊ธฐ์ค€์œผ๋กœ end๋ฅผ ์ฑ„์šด๋‹ค
2) span + ์ˆซ์ž ํ•œ๊ฐœ : ๊ธฐ์กด ์œ„์น˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ–‰์œผ๋กœ ์ˆซ์ž๋งŒํผ์„ ๋‚˜๋ˆˆ ๋ถ€๋ถ„์„ ๋ฉ”์šด๋‹ค.

3. grid-column-start

1) ์ˆซ์ž ํ•œ๊ฐœ : ์‹œ์ž‘๋ผ์ธ์„ ๊ฒฐ์ •ํ•˜๋ฉฐ end์™€ ๊ฐ™์ด ์“ฐ์ธ๋‹ค.
2) span + ์ˆซ์ž ํ•œ๊ฐœ : ๊ธฐ์กด ์œ„์น˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์—ด์œผ๋กœ ์ˆซ์ž๋งŒํผ์„ ๋‚˜๋ˆˆ ๋ถ€๋ถ„์„ ๋ฉ”์šด๋‹ค.

4. grid-column-end

1) ์ˆซ์ž ํ•œ๊ฐœ : start ๋ผ์ธ์„ ๊ธฐ์ค€์œผ๋กœ end๋ฅผ ์ฑ„์šด๋‹ค
2) span + ์ˆซ์ž ํ•œ๊ฐœ : ๊ธฐ์กด ์œ„์น˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์—ด์œผ๋กœ ์ˆซ์ž๋งŒํผ์„ ๋‚˜๋ˆˆ ๋ถ€๋ถ„์„ ๋ฉ”์šด๋‹ค.

5. grid-row / grid-column

start์™€ end๋ฅผ ํ•ฉ์ณ์„œ ์‚ฌ์šฉํ•˜๋Š” ์—ญํ• ๋กœ
์ฃผ์˜ํ•  ์ ์€ ์„œ๋กœ๊ฐ„์— /๋กœ ๊ตฌ๋ถ„ํ•ด์•ผํ•œ๋‹ค .

ex)

  grid-row : 3 / 4;
  grid-column : 2 / 4;

1~5 ์˜ˆ์‹œ

6. justify-self

  1. normal
  2. start
  3. end
  4. center
  • ํ–‰์„ ๊ธฐ์ค€์œผ๋กœ ํ•˜๋‚˜์˜ item์˜ ๋นˆ๊ณต๊ฐ„์— ๋Œ€ํ•ด ์ •๋ ฌ

7. align-self

  1. normal
  2. start
  3. end
  4. center
  • ์—ด์„ ๊ธฐ์ค€์œผ๋กœ ํ•˜๋‚˜์˜ item์˜ ๋นˆ๊ณต๊ฐ„์— ๋Œ€ํ•ด ์ •๋ ฌ

8. order

๊ธฐ๋ณธ์€ 0 ์ด๊ณ 
์ˆซ์ž๊ฐ€ ์ž‘์„์ˆ˜๋ก ์•ž์ชฝ์ •๋ ฌ
์ˆซ์ž๊ฐ€ ํด์ˆ˜๋ก ๋’ค์ชฝ์ •๋ ฌ

9. z-index

  • ๊ฒน์น ๊ฒฝ์šฐ ์Œ“์ž„์ˆœ์„œ๋ณ€๊ฒฝ

scroll๋„ ์ ์šฉํ•˜๊ธฐ

container์— scroll์„ ์ ์šฉ์‹œํ‚ค๊ณ  ํ•ด๋‹น ํ•„์ˆ˜์ ์œผ๋กœ height๋ฅผ ์ง€์ •ํ•ด์•ผ scroll์ด ๋™์ž‘๋œ๋‹ค.
์ด๋•Œ grid-template-columns๋„ ๊ผญ ์ฒ˜๋ฆฌํ•ด์•ผํ•œ๋‹ค.
์• ๋งคํ•˜๊ฒŒ 10๊ฐœ๋ฅผ 5*2ํ–‰๋ ฌ๋กœ ํ•˜๋Š”๋ฐ
์œ„์ชฝ 6๊ฐœ๋งŒ ํ•ด๋‹น ๊ฒƒ์„ ์ ์šฉ์‹œํ‚ค๋ฉด ๋†’๋‚ฎ์ด๊ฐ€ ๋‹ค๋ฅด๊ฒŒ ์ ์šฉ๋˜๋ฏ€๋กœ
์ „๋ถ€ ์ž˜ ์ ์šฉ๋˜๊ฒŒ ํ•ด์•ผํ•œ๋‹ค.
grid-template-rows: repeat(10, 1fr); ํ•˜๋‹ค๋ชปํ•ด ์ด๋ ‡๊ฒŒ๋ผ๋„ 10์ค„๋‹ค ์ ์šฉ์‹œํ‚ค๊ฒŒ

์ถ”๊ฐ€๋‚ด์šฉ

grid-template-areas ์‚ฌ์šฉ

grid-area ์‚ฌ์šฉ

grid-template-areas ๊ฐ™์€ ๊ฒฝ์šฐ ์ „์ฒด Grid ๊ตฌ์กฐ๋ฅผ ํ•œ๋ˆˆ์— CSS ์ฝ”๋“œ๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ์ง€๋งŒ,
๊ฐ Grid Item์— ์ด๋ฆ„์„ ๋ถ€์—ฌํ•ด์•ผ ํ•œ๋‹ค๋Š” ๋ถˆํŽธํ•จ๋„ ์žˆ์Šต๋‹ˆ๋‹ค.
grid-template-areas ์†์„ฑ์„ ์„ ํ˜ธํ•˜์‹œ๋Š” ๋ถ„๋“ค๋„ ๋งŽ์ด ์žˆ์Šต๋‹ˆ๋‹ค๋งŒ,
์ €๋Š” ๊ฐœ์ธ์ ์œผ๋กœ grid-row์™€ grid-column ์‚ฌ์šฉ์„ ๋” ์„ ํ˜ธํ•ฉ๋‹ˆ๋‹ค.
span ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ํ™•์žฅ์˜ ๊ฐœ๋…์„ ์‚ฌ์šฉํ•˜๋ฉด ์ผ๋ถ€ Grid Item์˜ ๊ฐ’๋งŒ ์ง€์ •ํ•ด๋„ ๊ตฌ์กฐ๊ฐ€ ๋งŒ๋“ค์–ด์งˆ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํŽธ๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
ํ•˜์ง€๋งŒ ๊ทธ ๊ฐ’์„ ๋ช…ํ™•ํ•˜๊ฒŒ ์ดํ•ดํ•˜์ง€ ๋ชปํ•œ ํ˜‘์—…์ž๊ฐ€ ์žˆ๋‹ค๋ฉด, ๋ช…์‹œ์ ์ด์ง€ ์•Š์€ ๋ถ€๋ถ„์œผ๋กœ ๋ฌธ์ œ๊ฐ€ ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
๊ทธ๋ž˜์„œ ์–ด๋””๊นŒ์ง€๋‚˜ ์ƒํ™ฉ๊ณผ ์ทจํ–ฅ์— ๋งž๊ฒŒ ์„ ํƒํ•ด์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.

  • ๋ผ๋Š” ์˜๊ฒฌ์„ ๋ฐ›์•˜๋‹ค.

๋‹ค๋ฅธ ์ •๋ณด

๊ธฐ๋ณธ์ ์œผ๋กœ grid๋Š” block๊ณผ ์œ ์‚ฌํ•œ๋ฐ container์—์„œ row๋ฅผ 1fr๋กœ ์ง€์ •ํ•˜๋ฉด
item๋‚ด์—์„œ css๋กœ ์ ์šฉํ•œ height:auto ๋Š” ๊ธฐ์กด์— ์ตœ์†Œํ•œ์˜ ๋†’์ด๋ฅผ ์œ ์ง€ํ•˜๋ ค๋Š” ๊ฒƒ๊ณผ ๋‹ค๋ฅด๊ฒŒ container์˜ 1fr์— ์˜ํ•ด ์ตœ๋Œ€ํ•œ ๋†’์•„์ง€๋ ค๊ณ  ํ•œ๋‹ค.

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

<style>
  .container {
    width:400px;
    height:400px;
    border:4px solid;
    display:grid;
    grid-template-rows: repeat(2,1fr);
    grid-template-columns: repeat(2,1fr);
  }

  .container .item{
    border:2px solid;
    width:100px;
    height:100px;
    background-color : orange;
  }

  .container .item:nth-child(1){
    justify-self: center;
    align-self: start;
  }
  .container .item:nth-child(2){
    width:auto;
    height:auto;	
  //  height:auto;	 
  // ์—†์„ ๊ฒฝ์šฐ height๊ฐ€ ์ตœ์†Œ ๋†’์ด๊ฐ€ ๋˜๊ณ  ์กด์žฌํ•˜๋ฉด 
  // 1fr์— ์˜ํ•ด ์ตœ๋Œ€๋กœ ๋†’์•„์ง€๋ ค๊ณ ํ•œ๋‹ค.
    justify-self: normal;
    align-self: normal;
  }
</style>

์ฐธ๊ณ ์ž๋ฃŒ

grid

profile
๋‚˜์˜ ํ•˜๋ฃจ๋ฅผ ๊ฐ€๋Šฅํ•œ ๊ธฐ์–ตํ•˜๊ณ  ์ฆ๊ธฐ๊ณ  ํ›„ํšŒํ•˜์ง€๋ง์ž
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€