πŸ‘ŠπŸ» CSS : Grid에 λŒ€ν•΄ μ•Œμ•„λ³΄μž

경이·2022λ…„ 2μ›” 11일
post-thumbnail

🧐 Gridλž€ ?

Flex(ν•œ λ°©ν–₯ λ ˆμ΄μ•„μ›ƒ)와 μœ μ‚¬ν•œ GridλŠ” 두 λ°©ν–₯(κ°€λ‘œ-μ„Έλ‘œ) λ ˆμ΄μ•„μ›ƒ λ©”μ„œλ“œμ΄λ‹€. λ”°λΌμ„œ Flex보닀 더 볡합적인 λ ˆμ΄μ•„μ›ƒ ν‘œν˜„μ΄ κ°€λŠ₯ν•˜λ‹€! GridλŠ” Flex와 λ§ˆμ°¬κ°€μ§€λ‘œ container와 item만 있으면 되며 container에 μ μš©ν•˜λŠ”μ†μ„±, item에 μ μš©ν•˜λŠ” μ†μ„±μœΌλ‘œ λ‚˜λˆ„μ–΄μ§„λ‹€.. λ˜ν•œ GridλŠ” container에 display: grid;λ₯Ό μ„€μ •ν•˜λŠ” κ²ƒμœΌλ‘œλΆ€ν„° μ‹œμž‘λœλ‹€.

.container {
	display: grid;
}

πŸ“Œ μš©μ–΄ 정리

κ·Έλ¦¬λ“œ μ»¨ν…Œμ΄λ„ˆ (Grid Container)

  • display: gridλ₯Ό μ μš©ν•˜λŠ” Grid의 전체 μ˜μ—­

κ·Έλ¦¬λ“œ μ•„μ΄ν…œ (GridΒ Item)

  • Gridμ»¨ν…Œμ΄λ„ˆμ˜ μžμ‹ μš”μ†Œ. 이 item듀이 Grid κ·œμΉ™μ— μ˜ν•΄ λ°°μΉ˜λœλ‹€.

κ·Έλ¦¬λ“œ νŠΈλž™ (Grid Track)

  • Grid의 ν–‰(Row) λ˜λŠ” μ—΄(Column)

κ·Έλ¦¬λ“œ μ…€ (Grid Cell)

  • Grid의 ν•œ μΉΈ. <div>같은 μ‹€μ œ htmlμš”μ†ŒλŠ” κ·Έλ¦¬λ“œ μ•„μ΄ν…œμ΄κ³ , 이런 Grid μ•„μ΄ν…œ ν•˜λ‚˜κ°€ λ“€μ–΄κ°€λŠ” β€œκ°€μƒμ˜ μΉΈ(ν‹€)”이라고 μƒκ°ν•˜λ©΄ λœλ‹€.

κ·Έλ¦¬λ“œ 라인(Grid Line)

  • Grid셀을 κ΅¬λΆ„ν•˜λŠ” μ„ 

κ·Έλ¦¬λ“œ 번호(Grid Number)

  • Grid라인의 각 번호

κ·Έλ¦¬λ“œ κ°­(Grid Gap)

  • Gridμ…€ μ‚¬μ΄μ˜ 간격

κ·Έλ¦¬λ“œ μ˜μ—­(Grid Area)

  • Grid라인으둜 λ‘˜λŸ¬μ‹ΈμΈ μ‚¬κ°ν˜• μ˜μ—­, κ·Έλ¦¬λ“œ μ…€μ˜ μ§‘ν•©

πŸ“ Grid container의 속성

βœ” display: grid;

  • ν•΄λ‹Ή μ»¨ν…Œμ΄λ„ˆλ₯Ό grid둜 μ‚¬μš©ν•˜κ² λ‹€κ³  μ„€μ •
.container {
	display: grid;
	/* display: inline-grid; */
}
/* 그리고 아무 일도 μΌμ–΄λ‚˜μ§€ μ•Šμ•˜λ‹€ */

βœ” grid-template-rows / grid-template-columns κ·Έλ¦¬λ“œ ν˜•νƒœ μ •μ˜

  • grid νŠΈλž™μ˜ 크기듀을 μ§€μ •ν•΄μ£ΌλŠ” 속성
  • repeatν•¨μˆ˜λŠ” repeat(반볡횟수, λ°˜λ³΅κ°’)으둜 λ°˜λ³΅λ˜λŠ” 값을 μžλ™μœΌλ‘œ μ²˜λ¦¬ν•œλ‹€.
  • minimaxν•¨μˆ˜λŠ” μ΅œμ†Ÿκ°’κ³Ό μ΅œλŒ“κ°’μ„ μ§€μ •ν•  수 μžˆλŠ” ν•¨μˆ˜μ΄λ‹€.
  • auto-fill은 column의 개수λ₯Ό 미리 μ •ν•˜μ§€ μ•Šκ³ , μ„€μ •λœ λ„ˆλΉ„κ°€ ν—ˆμš©ν•˜λŠ” ν•œ μ΅œλŒ€ν•œ 셀을 μ±„μš΄λ‹€. auto-fit은 λ‚¨λŠ” 곡간을 μ±„μš΄λ‹€.
.container {
	grid-template-columns: 200px 200px 500px;
	/* grid-template-columns: 1fr 1fr 1fr */
	/* grid-template-columns: repeat(3, 1fr) */
	/* grid-template-columns: 200px 1fr */
	/* grid-template-columns: 100px 200px auto */

	grid-template-rows: 200px 200px 500px;
	/* grid-template-rows: 1fr 1fr 1fr */
	/* grid-template-rows: repeat(3, 1fr) */
	/* grid-template-rows: 200px 1fr */
	/* grid-template-rows: 100px 200px auto */
}

βœ” row-gap/column-gap/ gap 간격 λ§Œλ“€κΈ°

  • κ·Έλ¦¬λ“œ μ…€ μ‚¬μ΄μ˜ 간격을 μ„€μ •ν•œλ‹€.
.container {
	row-gap: 10px;
	/* row의 간격을 10px둜 */
	column-gap: 20px;
	/* column의 간격을 20px둜 */
}

.container {
	gap: 10px 20px;
	/* row-gap: 10px; column-gap: 20px; */
}

.container {
	gap: 20px;
	/* row-gap: 20px; column-gap: 20px; */
}

βœ” grid-auto-column/grid-auto-rows κ·Έλ¦¬λ“œμ˜ ν˜•νƒœλ₯Ό μžλ™μœΌλ‘œ μ •μ˜

  • grid-template-columns(λ˜λŠ” grid-template-rows)의 ν†΅μ œλ₯Ό λ²—μ–΄λ‚œ μœ„μΉ˜μ— μžˆλŠ” νŠΈλž™μ˜ 크기λ₯Ό μ§€μ •ν•˜λŠ” 속성
.container {
	grid-template-rows: repeat(3, minmax(100px, auto));
}

βœ” 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λ₯Ό 같이 μ“Έ 수 μžˆλŠ” 단좕 속성이닀.
.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λ₯Ό 같이 μ“Έ 수 μžˆλŠ” 단좕 속성이닀.
.container {
	place-content: space-between center;
}

πŸ“ Grid item의 속성

βœ” grid-column/grid-row 각 μ…€μ˜ μ˜μ—­ μ§€μ •

  • μœ„μ˜ 그림은 grid λΌμΈλ²ˆν˜Έκ°€ 맀겨져 μžˆλŠ”λ° 이 번호λ₯Ό μ΄μš©ν•΄μ„œ columnκ³Ό row의 λ²”μœ„λ₯Ό κ²°μ •ν•œλ‹€. 빨간색 μ˜μ—­μ„ μ½”λ“œλ‘œ μ“°λ©΄ μ•„λž˜μ™€ κ°™λ‹€.
.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;
}

  • λͺ‡ 개의 셀을 μ°¨μ§€ν•˜κ²Œ ν•  것인지도 μ§€μ •ν•  수 μžˆλ‹€.
.item:nth-child(1) {
	/* 1번 λΌμΈμ—μ„œ 2μΉΈ */
	grid-column: 1 / span 2;
	/* 1번 λΌμΈμ—μ„œ 3μΉΈ */
	grid-row: 1 / span 3;
}

βœ” grid-template-areas μ˜μ–΄ μ΄λ¦„μœΌλ‘œ κ·Έλ¦¬λ“œ μ •μ˜

  • μ§κ΄€μ μœΌλ‘œ 각 μ˜μ—­μ— 이름을 뢙인뒀 각자 μ°¨μ§€ν•˜λŠ” μ…€μ˜ 개수만큼 ν•΄λ‹Ή μœ„μΉ˜μ— 이름을 μ¨μ£Όλ©΄λœλ‹€. 각 셀은 곡백으둜 κ΅¬λΆ„λ˜λ©°, λΉˆμΉΈμ€ λ§ˆμΉ¨ν‘œλ‚˜ noneμ„μ‚¬μš©ν•œλ‹€.
.container {
	grid-template-areas:
		"header header header"
		"   a    main    b   "
		"   .     .      .   "
		"footer footer footer";
}

βœ” grid-areas μ˜μ—­ 이름 λ§€μΉ­

  • grid-area 속성을 μ‚¬μš©ν•΄ ν•΄λ‹Ή itemμš”μ†Œμ— 이름을 μ§€μ •ν•΄μ£Όλ©΄ λœλ‹€.
.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 μžλ™ 배치

  • μ•„μ΄ν…œμ΄ μžλ™ λ°°μΉ˜λ˜λŠ” 흐름을 κ²°μ •ν•˜λŠ” 속성
.container {
	display: grid;
	grid-template-columns: repeat(auto-fill, minmax(25%, auto));
	grid-template-rows: repeat(5, minmax(50px,auto));
	grid-auto-flow: dense;
}
item:nth-child(2) { grid-column: auto / span 3; }
item:nth-child(5) { grid-column: auto / span 3; }
item:nth-child(7) { grid-column: auto / span 2; }

βœ” 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; */
}

βœ” place-self

  • align-self와 justify-selfλ₯Ό 같이 μ“Έ 수 μžˆλŠ” 단좕 속성이닀.
.item {
	place-self: start center;
}

βœ” order 배치 μˆœμ„œ

  • 각 itemλ“€μ˜ μ‹œκ°μ  λ‚˜μ—΄ μˆœμ„œλ₯Ό κ²°μ •ν•œλ‹€. html자체의 ꡬ쑰λ₯Ό λ°”κΎΈλŠ”κ²ƒμ΄ μ•„λ‹ˆλ―€λ‘œ, μ‹œκ°μž₯애인뢄듀이 슀크린 λ¦¬λ”λ‘œ 화면을 μ½μ„λ•ŒλŠ” μ˜λ―Έκ°€ μ—†μ–΄μ§€λŠ” μ†μ„±μ΄λ―€λ‘œ μ£Όμ˜ν•œλ‹€.
.item:nth-child(1) { order: 3; } /* A */
.item:nth-child(2) { order: 1; } /* B */
.item:nth-child(3) { order: 2; } /* C */

βœ” z-index

  • μˆ«μžκ°€ 클수둝 μœ„λ‘œ μ˜¬λΌμ˜¨λ‹€.
.item:nth-child(5) {
	z-index: 1;
	transform: scale(2);
}
/* z-indexλ₯Ό μ„€μ • μ•ˆν•˜λ©΄ 0μ΄λ―€λ‘œ, 1만 섀정해도 λ‚˜λ¨Έμ§€ μ•„μ΄ν…œμ„ 보닀 μœ„λ‘œ μ˜¬λΌμ˜¨λ‹€ */

πŸ“š Ref

https://studiomeal.com/archives/533

profile
μ΄μ‚¬μ€‘μž…λ‹ˆλ‹€!🌟https://velog.io/@devkyoung2

0개의 λŒ“κΈ€