CSS : Grid

daymoon_ยท2022๋…„ 2์›” 1์ผ
0

CSS

๋ชฉ๋ก ๋ณด๊ธฐ
7/18
post-thumbnail
post-custom-banner

Grid

๐Ÿ“Œ ์ฐธ๊ณ ์ž๋ฃŒ
1๋ถ„์ฝ”๋”ฉ Grid
Heropy CSS Grid ์™„๋ฒฝ ๊ฐ€์ด๋“œ

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

โœ… Grid ์†์„ฑ

  • ๊ทธ๋ฆฌ๋“œ ์ปจํ…Œ์ด๋„ˆ : ์ปจํ…Œ์ด๋„ˆ์— ์ ์šฉํ•˜๋Š” ์†์„ฑ โ–ถ ๊ทธ๋ฆฌ๋“œ ๋ฐฉ์‹์œผ๋กœ ๋ ˆ์ด์•„์›ƒ ๊ฒฐ์ •
  • ๊ทธ๋ฆฌ๋“œ ์•„์ดํ…œ : ์•„์ดํ…œ์— ์ ์šฉํ•˜๋Š” ์†์„ฑ โ–ถ ๊ทธ๋ฆฌ๋“œ ์ปจํ…Œ์ด๋„ˆ ๋‚ด๋ถ€์—์„œ ๊ทธ๋ฆฌ๋“œ ๋ฐฉ์‹์œผ๋กœ ๋ฐฐ์น˜

โœ… Grid ์ง€์ •

  • grid๋กœ ๋ ˆ์ด์•„์›ƒ์„ ์ง€์ •ํ•  ์š”์†Œ์— ์†์„ฑ๊ฐ’์„ ์ง€์ •
/* ๊ธฐ๋ณธ : ๋ธ”๋ก ๋ ˆ๋ฒจ ์š”์†Œ */
display: grid;

/* ์ธ๋ผ์ธ ๋ ˆ๋ฒจ ์š”์†Œ */
/* inline-block`๊ณผ ๋น„์Šทํ•จ */
display: inline-flex;

โœ… Grid ๊ตฌ์กฐ

  • Grid Container(๊ทธ๋ฆฌ๋“œ ์ปจํ…Œ์ด๋„ˆ)
    โ–ถ display: grid๋ฅผ ์ ์šฉํ•˜๋Š” Grid ์ „์ฒด ์˜์—ญ
    โ–ถ Grid ์ปจํ…Œ์ด๋„ˆ ์•ˆ์˜ ์š”์†Œ๋“ค์ด Grid ๊ทœ์น™์˜ ์˜ํ–ฅ์„ ๋ฐ›์•„ ์ •๋ ฌ

  • Grid Item(๊ทธ๋ฆฌ๋“œ ์•„์ดํ…œ)
    โ–ถ Grid ์ปจํ…Œ์ด๋„ˆ์˜ ์ž์‹ ์š”์†Œ๋“ค
    โ–ถ Grid ๊ทœ์น™์— ์˜ํ•ด ๋ฐฐ์น˜

  • Grid Trakc(๊ทธ๋ฆฌ๋“œ ํŠธ๋ž™)
    โ–ถ Grid ํ–‰ ๋˜๋Š” ์—ด(Column)

  • Grid Cell(๊ทธ๋ฆฌ๋“œ ์…€)
    โ–ถ Grid์˜ ํ•œ ์นธ

  • Grid Line(๊ทธ๋ฆฌ๋“œ ๋ผ์ธ)
    โ–ถ Grid ์…€์„ ๊ตฌ๋ถ„ํ•˜๋Š” ์„ 

  • Grid Number(๊ทธ๋ฆฌ๋“œ ๋„˜๋ฒ„)
    โ–ถ Grid์˜ ๋ผ์ธ์˜ ๋„˜๋ฒ„

  • Grid Gap(๊ทธ๋ฆฌ๋“œ ๊ฐญ)
    โ–ถ Grid ์…€ ์‚ฌ์ด์˜ ๊ฐ„๊ฒฉ

  • Grid Area(๊ทธ๋ฆฌ๋“œ ์˜์—ญ)
    โ–ถ Grid ๋ผ์ธ์œผ๋กœ ๋‘˜๋Ÿฌ์‹ธ์ธ ์‚ฌ๊ฐํ˜• ์˜์—ญ์œผ๋กœ, ๊ทธ๋ฆฌ๋“œ ์…€์˜ ์ง‘ํ•ฉ


๊ทธ๋ฆฌ๋“œ ํ˜•ํƒœ ์ •์˜

โš™๏ธ grid-template-rows, grid-template-columns

  • ์ปจํ…Œ์ด๋„ˆ์— Grid ํŠธ๋ž™์˜ ํฌ๊ธฐ๋“ค์„ ์ง€์ •ํ•˜๋Š” ์†์„ฑ
  • ๋ช…์‹œ์  ํ–‰(track)/์—ด(track)์˜ ํฌ๊ธฐ๋ฅผ ์ •์˜
.container {
	/* ์—ด 3๊ฐœ : 200px 200px 500px */
	grid-template-columns: 200px 200px 500px;

	/* ํ–‰ 3๊ฐœ : 200px 200px 500px */
	grid-template-rows: 200px 200px 500px;
}

๐Ÿ”ธ repeat ํ•จ์ˆ˜

  • ๋ฐ˜๋ณต๋˜๋Š” ๊ฐ’์„ ์ž๋™์œผ๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜
.container {
	grid-template-columns: repeat(5, 1fr);
	/* grid-template-columns: 1fr 1fr 1fr 1fr 1fr */
}

๐Ÿ”ธ minmax ํ•จ์ˆ˜

  • ์ตœ์†Ÿ๊ฐ’๊ณผ ์ตœ๋Œ“๊ฐ’์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜
  • auto ๋˜๋Š” 1fr์„ ์ ์œผ๋ฉด ๋นˆ ๊ณต๊ฐ„์ด ์žˆ๋Š” ๋ถ€๋ถ„์„ ์ฑ„์›Œ์คŒ
.container {
	grid-template-columns: repeat(3, 1fr);
	grid-template-rows: repeat(3, minmax(100px, auto));
}

๐Ÿ”ธ auto-fill๊ณผ auto-fit

  • column์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฏธ๋ฆฌ ์ •ํ•˜์ง€ ์•Š๊ณ  ์„ค์ •๋œ ๋„ˆ๋น„๊ฐ€ ํ—ˆ์šฉํ•˜๋Š” ํ•œ ์ตœ๋Œ€ํ•œ ์…€์„ ์ฑ„์›€
  • auto-fill : ํŠธ๋ž™์˜ ์ตœ์†Œ๊ธธ์ด ํ•ฉ๋ณด๋‹ค ์ปจํ…Œ์ด๋„ˆ์˜ ๊ธธ์ด๊ฐ€ ๊ธธ์–ด์ง„ ๊ฒฝ์šฐ ๋นˆ ๊ณต๊ฐ„์„ ๋‚จ๊น€
  • auto-fit : ์•„์ดํ…œ๋“ค์ด ์ปจํ…Œ์ด๋„ˆ์— ๋งž๊ฒŒ ์ฑ„์›Œ์คŒ
.container {
	grid-template-columns: repeat(auto-fill, minmax(20%, auto));
}

๊ฐ„๊ฒฉ ๋งŒ๋“ค๊ธฐ

โš™๏ธ row-gap, column-gap, gap

  • ๊ทธ๋ฆฌ๋“œ ์…€ ์‚ฌ์ด์˜ ๊ฐ„๊ฒฉ์„ ์„ค์ •
  • row-gap : ํ–‰๊ณผ ์—ด ์‚ฌ์ด์˜ ๊ฐ„๊ฒฉ ์ •์˜
  • column-gap : ์—ด๊ณผ ์—ด ์‚ฌ์ด์˜ ๊ฐ„๊ฒฉ ์ •์˜
  • gap : ๊ฐ ํ–‰๊ณผ ํ–‰, ์—ด๊ณผ ์—ด ์‚ฌ์ด์˜ ๊ฐ„๊ฒฉ์„ ์ง€์ •
.container {
	row-gap: 10px;
	/* row์˜ ๊ฐ„๊ฒฉ์„ 10px๋กœ */
    
	column-gap: 20px;
	/* column์˜ ๊ฐ„๊ฒฉ์„ 20px๋กœ */
    
    gap: 20px;
	/* row-gap: 20px; column-gap: 20px; */
    
    gap: 10px 20px;
	/* row-gap: 10px; column-gap: 20px; */
}

๊ทธ๋ฆฌ๋“œ ํ˜•ํƒœ๋ฅผ ์ž๋™์œผ๋กœ ์ •์˜

โš™๏ธ grid-auto-columns, grid-auto-rows

  • ํ†ต์ œ๋ฅผ ๋ฒ—์–ด๋‚œ ์œ„์น˜์— ์žˆ๋Š” ํŠธ๋ž™์˜ ํฌ๊ธฐ๋ฅผ ์ง€์ •ํ•˜๋Š” ์†์„ฑ
  • ์•”์‹œ์ ์ธ ์—ด/ํ–‰์˜ ํฌ๊ธฐ๋ฅผ ์ •์˜

๊ฐ ์…€์˜ ์˜์—ญ ์ง€์ •

โš™๏ธ grid-column-start, grid-column-end, grid-column, grid-row-start, grid-row-end, grid-row

  • Grid ์•„์ดํ…œ์— ์ ์šฉํ•˜๋Š” ์†์„ฑ โ†’ ๊ฐ ์…€์˜ ์˜์—ญ์„ ์ง€์ •
  • grid-column๊ณผ grid-row๋Š” start์™€ end๋ฅผ ํ•œ๋ฒˆ์— ์“ฐ๋Š” ์ถ•์–‘ํ˜•
.item:nth-child(1) {
	grid-column-start: 1;
	grid-column-end: 3;
	grid-row-start: 1;
	grid-row-end: 2;
}

/* ์ถ•์•ฝ ์†์„ฑ */
.item:nth-child(1) {
	grid-column: 1 / 3;
	grid-row: 1 / 2;
}

์˜์—ญ ์ด๋ฆ„์œผ๋กœ ๊ทธ๋ฆฌ๋“œ ์ •์˜

โš™๏ธ grid-template-areas

  • ๊ฐ ์˜์—ญ(Grid Area)์— ์ด๋ฆ„์„ ๋ถ™์ด๊ณ , ๊ทธ ์ด๋ฆ„์„ ์ด์šฉํ•ด์„œ ๋ฐฐ์น˜ํ•˜๋Š” ๋ฐฉ๋ฒ• โ†’ ์ง๊ด€์ 
  • ํ•ด๋‹น ์•„์ดํ…œ ์š”์†Œ์— grid-area์†์„ฑ์œผ๋กœ ์ด๋ฆ„์„ ์ง€์ •ํ•˜์—ฌ ๋งค์นญ
.container {
	grid-template-areas:
		"header header header"
		"   a    main    b   "
		"   .     .      .   "
		"footer footer footer";
}

.header { grid-area: header; }
.sidebar-a { grid-area: a; }
.main-content { grid-area: main; }
.sidebar-b { grid-area: b; }
.footer { grid-area: footer; }
/* ์ด๋ฆ„ ๊ฐ’์— ๋”ฐ์˜ดํ‘œ๊ฐ€ ์—†๋Š” ๊ฒƒ์— ์ฃผ์˜ํ•˜์„ธ์š” */

์ž๋™ ๋ฐฐ์น˜

โš™๏ธ grid-auto-flow

  • ์•„์ดํ…œ์ด ์ž๋™ ๋ฐฐ์น˜๋˜๋Š” ํ๋ฆ„์„ ๊ฒฐ์ •ํ•˜๋Š” ์†์„ฑ

์„ธ๋กœ ๋ฐฉํ–ฅ ์ •๋ ฌ

โš™๏ธ align-items

  • ์•„์ดํ…œ๋“ค์„ ์„ธ๋กœ(column) ๋ฐฉํ–ฅ์œผ๋กœ ์ •๋ ฌ
  • ์ปจํ…Œ์ด๋„ˆ์— ์ ์šฉ
.container {
	align-items: stretch;
	/* align-items: start; */
	/* align-items: center; */
	/* align-items: end; */
}

๊ฐ€๋กœ ๋ฐฉํ–ฅ ์ •๋ ฌ

โš™๏ธ justify-items

  • ์•„์ดํ…œ ๊ฐ€๋กœ(row) ๋ฐฉํ–ฅ์œผ๋กœ ์ •๋ ฌ
.container {
	justify-items: stretch;
	/* justify-items: start; */
	/* justify-items: center; */
	/* justify-items: end; */
}

โš™๏ธ place-items

  • align-items์™€ justify-items๋ฅผ ๊ฐ™์ด ์“ธ ์ˆ˜ ์žˆ๋Š” ๋‹จ์ถ• ์†์„ฑ
  • align-items, justify-items์˜ ์ˆœ์„œ๋กœ ์ž‘์„ฑ
  • ํ•˜๋‚˜์˜ ๊ฐ’๋งŒ ์“ฐ๋ฉด ๋‘ ์†์„ฑ ๋ชจ๋‘์— ์ ์šฉ
.container {
	place-items: center start;
}

์•„์ดํ…œ ๊ทธ๋ฃน ์„ธ๋กœ ์ •๋ ฌ

โš™๏ธ align-content

  • Grid ์•„์ดํ…œ๋“ค์˜ ๋†’์ด๋ฅผ ๋ชจ๋‘ ํ•ฉํ•œ ๊ฐ’์ด Grid ์ปจํ…Œ์ด๋„ˆ์˜ ๋†’์ด๋ณด๋‹ค ์ž‘์„ ๋•Œ Grid ์•„์ดํ…œ๋“ค์„ ํ†ต์งธ๋กœ ์ •๋ ฌ
.container {
	align-content: stretch;
	/* align-content: start; */
	/* align-content: center; */
	/* align-content: end; */
	/* align-content: space-between; */
	/* align-content: space-around; */
	/* align-content: space-evenly; */
}

์•„์ดํ…œ ๊ทธ๋ฃน ๊ฐ€๋กœ ์ •๋ ฌ

โš™๏ธ justify-content

  • Grid ์•„์ดํ…œ๋“ค์˜ ๋„ˆ๋น„๋ฅผ ๋ชจ๋‘ ํ•ฉํ•œ ๊ฐ’์ด Grid ์ปจํ…Œ์ด๋„ˆ์˜ ๋„ˆ๋น„๋ณด๋‹ค ์ž‘์„ ๋•Œ Grid ์•„์ดํ…œ๋“ค์„ ํ†ต์งธ๋กœ ์ •๋ ฌ
.container {
	justify-content: stretch;
	/* justify-content: start; */
	/* justify-content: center; */
	/* justify-content: end; */
	/* justify-content: space-between; */
	/* justify-content: space-around; */
	/* justify-content: space-evenly; */
}

โš™๏ธ place-content

  • align-content์™€ justify-content๋ฅผ ๊ฐ™์ด ์“ธ ์ˆ˜ ์žˆ๋Š” ๋‹จ์ถ• ์†์„ฑ
  • align-content, justify-content์˜ ์ˆœ์„œ๋กœ ์ž‘์„ฑ
  • ํ•˜๋‚˜์˜ ๊ฐ’๋งŒ ์“ฐ๋ฉด ๋‘ ์†์„ฑ ๋ชจ๋‘์— ์ ์šฉ
.container {
	place-content: space-between center;
}

๊ฐœ๋ณ„ ์•„์ดํ…œ ์„ธ๋กœ ์ •๋ ฌ

โš™๏ธ align-self

  • ํ•ด๋‹น ์•„์ดํ…œ์„ ์„ธ๋กœ(column)๋ฐฉํ–ฅ์œผ๋กœ ์ •๋ ฌ
  • ์•„์ดํ…œ์— ์ ์šฉ
.item {
	align-self: stretch;
	/* align-self: start; */
	/* align-self: center; */
	/* align-self: end; */
}

๊ฐœ๋ณ„ ์•„์ดํ…œ ๊ฐ€๋กœ ์ •๋ ฌ

โš™๏ธ justify-self

  • ํ•ด๋‹น ์•„์ดํ…œ์„ ๊ฐ€๋กœ(row)๋ฐฉํ–ฅ์œผ๋กœ ์ •๋ ฌ
  • ์•„์ดํ…œ์— ์ ์šฉ
.item {
	justify-self: stretch;
	/* justify-self: start; */
	/* justify-self: center; */
	/* justify-self: end; */
}

โš™๏ธ palce-self

  • align-self์™€ justify-self๋ฅผ ๊ฐ™์ด ์“ธ ์ˆ˜ ์žˆ๋Š” ๋‹จ์ถ• ์†์„ฑ
  • align-self, justify-self์˜ ์ˆœ์„œ๋กœ ์ž‘์„ฑ
  • ํ•˜๋‚˜์˜ ๊ฐ’๋งŒ ์“ฐ๋ฉด ๋‘ ์†์„ฑ ๋ชจ๋‘์— ์ ์šฉ
.item {
	place-self: start center;
}

๋ฐฐ์น˜ ์ˆœ์„œ

โš™๏ธ order

  • ๊ฐ ์•„์ดํ…œ๋“ค์˜ ์‹œ๊ฐ์  ๋‚˜์˜ ์ˆœ์„œ๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ์†์„ฑ
.item:nth-child(1) { order: 3; } /* A */
.item:nth-child(2) { order: 1; } /* B */
.item:nth-child(3) { order: 2; } /* C */

โš™๏ธ z-index

  • z-index๋กœ Z์ถ• ์ •๋ ฌ์„ ํ•  ์ˆ˜ ์žˆ์Œ
  • ์ˆซ์ž๊ฐ€ ํด ์ˆ˜๋ก ์œ„๋กœ ์˜ฌ๋ผ๊ฐ
  • position์—์„œ z-index๋ž‘ ๊ฐ™์Œ
.item:nth-child(5) {
	z-index: 1;
	transform: scale(2);
}
/* z-index๋ฅผ ์„ค์ • ์•ˆํ•˜๋ฉด 0์ด๋ฏ€๋กœ, 1๋งŒ ์„ค์ •ํ•ด๋„ ๋‚˜๋จธ์ง€ ์•„์ดํ…œ์„ ๋ณด๋‹ค ์œ„๋กœ ์˜ฌ๋ผ์˜จ๋‹ค */

๐Ÿ—“๏ธ ์ˆ˜์ • ๋ฐ ์ถ”๊ฐ€

โœ… 2022.02.26

  • ์ธ๋„ค์ผ ๋ณ€๊ฒฝ

โœ… 2022.05.18

  • ๋ณธ๋ฌธ ๋‚ด์šฉ ์ „์ฒด ์ˆ˜์ • ๋ฐ ์žฌ์ •๋ฆฌ
profile
๋ฏธ์ง€์˜ ๊ณต๊ฐ„๐ŸŒ™
post-custom-banner

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