---
# System prepended metadata

title: 2023-3-9 RWD
tags: [RWD, HTML, CSS]

---

# 2023-3-9 RWD
###### tags: `HTML` `CSS` `RWD`
## 網格系統擴充
針對.row class名稱的擴充進行控制
```css=
[class*="col-"],
[class*="row-cols"]>.col {
    padding-right: var(--col-gap);
    padding-left: var(--col-gap);
    box-sizing: border-box;
}
.row-cols-1>.col {
    width: 100%;
}

.row-cols-2>.col {
    width: 50%;
}

.row-cols-3>.col {
    width: 33.333333%;
}
...以下省略

.justify-content-md-center {
    justify-content: center;
}
.justify-content-md-flex-start {
    justify-content: flex-start;
}
.justify-content-md-flex-end {
    justify-content: flex-end;
}
...以下省略
```

## 選單
選單建議大寫大，小寫小 -> 避免code需要不斷覆寫
```css
.main-header {
  padding: 3px;
  background-color: #aaa;
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: relative;
}
.main-header .logo img {
  vertical-align: top;
}
.main-nav a {
  display: block;
  text-decoration: none;
  color: #fff;
  padding: 10px;
}
#nav-switch {
  display: none;
}
/* 手機控制區，可另外加上orientation選擇螢幕方向 */
@media screen and (width < 768px) and (orientation: portrait) {
  .main-header .hb {
    height: 40px;
    width: 40px;
    background-color: #fa0;
    display: flex;
    flex-direction: column;
    justify-content: space-evenly;
    align-items: center;
    position: relative;
  }
  .main-header .hb span {
    width: 80%;
    height: 2px;
    background-color: #000;
  }
  .main-nav {
    display: none;
    position: absolute;
    background-color: #fa0;
    top: 100%;
    left: 0;
    width: 90%;
  }
  .main-nav a + a {
    border-top: 1px solid #000;
  }
  /* 示範使用checkbox方式控制選單的顯示，但是較常用javascript*/ 
  #nav-switch:checked + .main-header .main-nav {
    display: block;
  }
  
}
/*平版以上控制區*/
@media screen and (width >= 768px) {
  .main-header .hb {
    display: none;
  }
  .main-nav a {
    display: inline;
  }
}
```


手機機版：
![](https://i.imgur.com/zn9Wz2a.png)

電腦版：
![](https://i.imgur.com/7XLP9Lm.png)

## transition動畫控制
transition加在不同區域有不同的控制效果
可加上自定義速度、延遲等控制效果
```css
/* transition */
  .main-header .hb span {
    position: absolute;
    left: 0;
    right: 0;
    margin: auto;
  }
  .main-header .hb span:nth-child(1) {
    top: 11px;
    transform: rotate(0deg);
    transition: top 0.5s 0.5s, transform 0.5s;
  }
  #nav-switch:checked + .main-header .hb span:nth-child(1) {
    top: 19px;
    transform: rotate(405deg);
    transition: top 0.5s,
      transform 0.5s 0.5s cubic-bezier(1, 0.77, 0.65, 1.52);
  }
  .main-header .hb span:nth-child(2) {
    top: 19px;
    opacity: 1;
    transition: opacity 0.5s;
  }
  #nav-switch:checked + .main-header .hb span:nth-child(2) {
    opacity: 0;
    transition: opacity 0.5s cubic-bezier(1, 0.77, 0.65, 1.52);
  }
  .main-header .hb span:nth-child(3) {
    top: 27px;
    transform: rotate(0deg);
    transition: top 0.5s 0.5s, transform 0.5s;
  }
  #nav-switch:checked + .main-header .hb span:nth-child(3) {
    top: 19px;
    transform: rotate(-405deg);
    transition: top 0.5s,
      transform 0.5s 0.5s cubic-bezier(1, 0.77, 0.65, 1.52);
  }
```
## 多層選單
多層選單需要考量不同裝置的使用情境，電腦版可以使用:hover，但手機版就不行，可透過追加子選單按鈕等方法應對，通常使用javascript控制選單的顯示會較為適合。
![](https://i.imgur.com/xpjQo1y.png)

