[JAYโŒจ๏ธCSS] ๋„ˆ๋ฌด ์‰ฝ๊ณ  ์œ ์šฉํ•œ CSS grid ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•ต์‹ฌ ์ •๋ฆฌ

2023. 3. 13. 23:19ใ†html&css

๋ฐ˜์‘ํ˜•

 

 

๋„ˆ๋ฌด ์‰ฝ๊ณ  ์œ ์šฉํ•œ CSS grid ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•ต์‹ฌ ์ •๋ฆฌ

 1๏ธโƒฃ Grid ๋ž€?
  • display:grid ๋ฅผ ์„ ์–ธํ•˜๋ฉด ์ž์‹์—๊ฒŒ ์ ์šฉ.
  • ๋ถ€๋ชจ์š”์†Œ๋Š” grid-container ์ž์‹์š”์†Œ๋Š” grid-item
  • ์›นํŽ˜์ด์ง€๋ฅผ ์œ„ํ•œ 2์ฐจ์› ๋ ˆ์ด์•„์›ƒ(x์ถ•, y์ถ•) ์‹œ์Šคํ…œ.
  • Gap ์„ ํ†ตํ•ด ์…€๊ณผ ์…€์‚ฌ์ด์˜ ๊ฐ„๊ฒฉ์„ ์„ค์ •.

 

Grid์˜ ๊ตฌ์กฐ (ํ•ด๋‹น ์ด๋ฏธ์ง€๋Š” ์œ„๋‹ˆ๋ธŒ ์ˆ˜์—… ์ž๋ฃŒ์ž…๋‹ˆ๋‹ค.)
Grid์˜ ๊ตฌ์กฐ (ํ•ด๋‹น ์ด๋ฏธ์ง€ ์ถœ์ฒ˜๋Š” ์œ„๋‹ˆ๋ธŒ ์ˆ˜์—… ์ž๋ฃŒ์ž…๋‹ˆ๋‹ค.)

 

 

 

2๏ธโƒฃ grid-container
  • ↔๏ธ grid-template-columns ์—ด๋ฐฉํ–ฅ ๊ทธ๋ฆฌ๋“œ ํŠธ๋ž™์˜ ์‚ฌ์ด์ฆˆ๋ฅผ ์„ค์ •.
  • โ†•๏ธ grid-template-rows ํ–‰๋ฐฉํ–ฅ ๊ทธ๋ฆฌ๋“œ ํŠธ๋ž™์˜ ์‚ฌ์ด์ฆˆ๋ฅผ ์„ค์ •.

 

.container{
	display:grid;
	grid-template-columns: 100px 100px 100px;
	grid-template-rows: 200px 100px;
}

.container{
	display:grid;
	grid-template-columns: 1fr 1fr 1fr;
	grid-template-rows: 2fr 1fr;
}

/* fr์ด๋ž€? */
/* fraction = ๋ถ„์ˆ˜ : ๋น„์œจ๋กœ ์ปจํ…Œ์ด๋„ˆ ๋ถ„ํ•  (ex 1fr 1fr = 1:1) */

 

 

 

 

3๏ธโƒฃ Grid Function
  • repeat(๋ฐ˜๋ณต ํšŸ์ˆ˜ , ๋ฐ˜๋ณตํ•  ๊ฐ’)
/* grid-template-columns: 1fr 2fr 1fr 2fr; */
grid-template-columns: repeat(2, 1fr 2fr);

 

  • minmax(์ตœ์†Œํญ , ์ตœ๋Œ€ํญ or auto)
  • auto-fill auto-fit ์„ ํ™œ์šฉํ•ด์„œ ๋ฐ˜์‘ํ˜• ์›น์— ์‚ฌ์šฉํ•˜์ž! ๋‘ ์ฝ”๋“œ์˜ ์ฐจ์ด๋Š” ์•„๋ž˜ ์˜ˆ์ œ ์ด๋ฏธ์ง€ ์ฐธ๊ณ .

 

/*  auto-fill = ๊ฐ€๋Šฅํ•œ ๋งŽ์€ ์…€๋“ค์„ ์ •๋ ฌ. ๋‚จ์œผ๋ฉด ๋นˆ๊ณต๊ฐ„ ์ƒ์„ฑ */
grid-template-columns: repeat(auto-fill, minmax(80px, auto));

/* auto-fit = ๊ณต๊ฐ„์ด ๋‚จ์„ ๊ฒฝ์šฐ, ๊ทธ ๊ณต๊ฐ„์„ ๊ฐ ์…€๋“ค์ด ๋‚˜๋ˆ  ๊ฐ–์Šต๋‹ˆ๋‹ค. */
grid-template-columns: repeat(auto-fit, minmax(80px, auto));

auto-fill ๊ณผ auto-fit์˜ ์ฐจ์ด
auto-fill ๊ณผ auto-fit์˜ ์ฐจ์ด

 

 

 

 

 

4๏ธโƒฃ Grid-item
  • grid-column-start grid-column-end grid-row-start grid-row-end
  /* ์ขŒํ‘œ ์ž…๋ ฅ */
  grid-row-start: 1; 
  grid-row-end: 2; 
  grid-column-start: 1; 
  grid-column-end: 4; 


  /* ์ขŒํ‘œ ์ž…๋ ฅ ์ค„์—ฌ์„œ ์ž‘์„ฑ */
  grid-row: 1/2;
  grid-column: 1/4;
  
  
  /* grid-area๋ฅผ ํ†ตํ•ด ๋‹จ์ถ• ํ‘œ๊ธฐ */
  /* row-start / column-start(span ๋ณ‘ํ•ฉ ์ˆ˜) / row-end / column-end (span ๋ณ‘ํ•ฉ ์ˆ˜)  */
  grid-area: 1 / 1 / span 2 / span 2;

grid-item ์ขŒํ‘œ์„ค์ • ์˜ˆ์ œ
grid-item ์ขŒํ‘œ์„ค์ • ์˜ˆ์ œ

 

 

 

 

 

5๏ธโƒฃ grid-template-areas
  • ํ…œํ”Œ๋ฆฟ์„ ์ง์ ‘ ์„ ์–ธํ•ด์„œ grid ์ขŒํ‘œ์— ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ๋ฌธ์ž์—ด๋กœ ๋ฐฐ์น˜ํ•˜๋Š” ๋ฐฉ๋ฒ•.

 

.template-Grid {
  grid-template-rows: 2fr 1fr;
  grid-template-rows: 1fr 2fr 1fr;
  grid-template-areas:
    "header header header"
    "section section aside"
    "footer footer footer";
}

grid-template-area ํ™œ์šฉ ์˜ˆ์ œ
grid-template-area ํ™œ์šฉ ์˜ˆ์ œ

 

 

 

 

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป ์š”์•ฝ!
  • ๋ถ€๋ชจ์š”์†Œ๋Š” grid-container ์ž์‹์š”์†Œ๋Š” grid-item ํ–‰๋ ฌ์„ ์˜ค๊ฐ€๋ฉฐ css ๋ ˆ์ด์•„์›ƒ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.
  • ↔๏ธ grid-template-columns ์—ด๋ฐฉํ–ฅ ๊ทธ๋ฆฌ๋“œ ํŠธ๋ž™์˜ ์‚ฌ์ด์ฆˆ๋ฅผ ์„ค์ •.
  • โ†•๏ธ grid-template-rows ํ–‰๋ฐฉํ–ฅ ๊ทธ๋ฆฌ๋“œ ํŠธ๋ž™์˜ ์‚ฌ์ด์ฆˆ๋ฅผ ์„ค์ •.
  • grid-item์€ ์ขŒํ‘œ๋ฅผ ์ฐ์–ด์„œ ์œ„์น˜ ์ž…๋ ฅ grid-column-start grid-column-end grid-row-start grid-row-end
  • repeat() ๋ฐ˜๋ณต ํ•จ์ˆ˜, min() max() ๋น„์œจ ์กฐ์ • ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜๋ฉด ๋ฐ˜์‘ํ˜• ์ œ์ž‘ํ•  ๋•Œ Good!
  • ๊ณผ๊ฑฐ์˜ ๋‚˜... ์™œ grid๊ฐ€ ์–ด๋ ต๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ์„๊นŒ? ์ •๋ง ํŽธํ•˜๊ณ  ๊ฐ„๋‹จํ•˜๋‹ค.
    Grid์— ๋Œ€ํ•œ ์˜ˆ์ œ๊ฐ€ ๊ถ๊ธˆํ•˜๋‹ค๋ฉด ๐Ÿ”— git_pages ์—์„œ ํ™•์ธ ๊ฐ€๋Šฅ ๐Ÿ˜

 

 

 

 

 

๋ฐ˜์‘ํ˜•