λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
[개발] Practice/HTML CSS

[HTML/CSS] Grid λ°˜μ‘ν˜• λ ˆμ΄μ•„μ›ƒ λ§Œλ“€κΈ°

by Connecting-the-dots 2022. 3. 2.
728x90
λ°˜μ‘ν˜•

πŸ’‘ μ‹€μŠ΅ 포인트!

PC 화면일 λ•Œ λ ˆμ΄μ•„μ›ƒ

 

λͺ¨λ°”일, νƒœλΈ”λ¦ΏμΌ λ•Œ λ ˆμ΄μ•„μ›ƒ

  • μ˜€λŠ˜μ€ display : grid λ₯Ό μ΄μš©ν•˜μ—¬ μ˜μ—­μ„ λ‚˜λˆ„λŠ” 방법을 λ°°μ›Œλ³΄μ•˜λŠ”λ°, 처음 λ³΄λŠ” 속성이라 ꡉμž₯히 μ‹ μ„ ν–ˆλ‹€. 일단 μœ„μ™€ 같은 μ˜μ—­μœΌλ‘œ λ‚˜λˆ„λŠ” 방법을 두 가지 λ°°μ›Œλ³΄κ³ , λ°˜μ‘ν˜•μœΌλ‘œλŠ” μ–΄λ–€ μ‹μœΌλ‘œ λ§Œλ“œλŠ”μ§€ κ³΅λΆ€ν–ˆλ‹€.

πŸ’œ PC ν™”λ©΄

PC 화면일 λ•Œ λ ˆμ΄μ•„μ›ƒ

🀍 1번째 방법

    <div class="grid-container">
        <div class="grid-nav"></div>
        <div class="grid-sidebar"></div>
        <div class="grid"></div>
    </div>

 

.grid-container {
  display: grid;
  grid-template-columns: 100px 100px 100px 100px; /* fr 은 전체 λ„ˆλΉ„μ— λŒ€ν•œ λΉ„μœ¨μ„ 의미 */
  grid-template-rows: 100px 100px 100px;
  /* grid-gap: 10px; */ /* grid-gap 은 각 grid μ‚¬μ΄μ˜ 간격을 의미 */
}

.grid-container div {
  border: 1px solid black;
}

.grid-nav {
  grid-column: 1 / 5; /* display: grid μžμ‹λ“€μ—λ§Œ μ‚¬μš©κ°€λŠ₯ν•œ 속성, 각 μˆ«μžλŠ” μ„Έλ‘œμ€„ μˆœλ²ˆμ„ 의미 */
  grid-row: 1 / 2; /* display: grid μžμ‹λ“€μ—λ§Œ μ‚¬μš©κ°€λŠ₯ν•œ 속성, 각 μˆ«μžλŠ” κ°€λ‘œμ€„ μˆœλ²ˆμ„ 의미 */
}

.grid-sidebar {
  grid-row: 2 / 4;
}

.grid {
  grid-row: 2 / 4;
  grid-column: 2 / 5;
}

🀍 2번째 방법

    <div class="grid-container2">
        <div class="grid-nav2"></div>
        <div class="grid-sidebar2"></div>
        <div class="grid2"></div>
    </div>

 

.grid-container2 {
  display: grid;
  grid-template-columns: 100px 100px 100px 100px;
  grid-template-rows: 100px 100px 100px;
  grid-template-areas:
    "헀더 헀더 헀더 헀더"
    "μ‚¬μ΄λ“œ κ·Έλ¦¬λ“œ κ·Έλ¦¬λ“œ κ·Έλ¦¬λ“œ"
    "μ‚¬μ΄λ“œ κ·Έλ¦¬λ“œ κ·Έλ¦¬λ“œ κ·Έλ¦¬λ“œ";
}

.grid-container2 div {
  border: 1px solid black;
}

.grid-nav2 {
  grid-area: 헀더;
}

.grid-sidebar2 {
  grid-area: μ‚¬μ΄λ“œ;
}

.grid2 {
  grid-area: κ·Έλ¦¬λ“œ;
}

 

πŸ’œ λͺ¨λ°”일 ν™”λ©΄

λͺ¨λ°”일, νƒœλΈ”λ¦ΏμΌ λ•Œ λ ˆμ΄μ•„μ›ƒ

  • 1번째 λ°©λ²•μ—μ„œ media query λ₯Ό 좔가해도 λ°˜μ‘ν˜• λ ˆμ΄μ•„μ›ƒμ„ λ§Œλ“€ 수 μžˆμ§€λ§Œ κ½€ 번거둜운 반면, 2번째 방법에 media query λ₯Ό μΆ”κ°€λ‘œ μ‚¬μš©ν•˜λ©΄ μ’€ 더 μ‰½κ²Œ λ°˜μ‘ν˜• λ ˆμ΄μ•„μ›ƒλ„ λ§Œλ“€ 수 μžˆλ‹€.
@media screen and (max-width: 768px) {
  .grid-container2 {
    grid-template-areas:
      "헀더 헀더 헀더 헀더"
      "μ‚¬μ΄λ“œ μ‚¬μ΄λ“œ μ‚¬μ΄λ“œ μ‚¬μ΄λ“œ"
      "κ·Έλ¦¬λ“œ κ·Έλ¦¬λ“œ κ·Έλ¦¬λ“œ κ·Έλ¦¬λ“œ";
  }
}

 

728x90
λ°˜μ‘ν˜•