Basic
Color & Background
1 | body { |
Font
- 用
font-family
這個 attribute 去設定字體集- 是一個 array, 因此可以指定多個 font
- 有先後順序, 前面的 font 找不到才會使用下一個 font
- 使用系統 Default 之 Native font :
1 | body { |
- 使用 Google 提供的免費 font
- Google font
- 利用 CDN 的概念
- 選擇想要的 font, 按右上角的(+), 就可以得到使用時要用的連結(CDN)
Selectors
- 在 CSS 中選擇元件依據
- id
- class
- … etc.
1 | h1 { |
Advanced selectors
- Composition - A or B
1 | #id1, .class1 { ... } /* 有 id1「或」有class1這個class的元件 */ |
- And - A and B
1 | li.class1 { ... } /* li「且」有class1這個class的元件 */ |
- Descendent - 在A之下的B (A中含有B的子元件)
1 | li a { ... } /* 在li內裡面的a元件*/ |
- Adjacent - 與A在同一層, 且B是在A的下一個元件
1 | .red + span { /* 與.red元件同一層, 鄰近的span元件 */ |
1 | <div class="red"></div> |
- Attribute selector - 選擇某個含有特定attribute的元件
1 | a[href="https://..."] { ... } /* 選擇含有href這個attr.且value是"https://..."的a元件 */ |
Pseudo class & element
選擇元件的「某個狀態」或「某個位置」
1 | /* pseudo class selector */ |
The 30 CSS Selectors You Must Memorize
Inheritance, Cascading, and Specificity
- Inheritance
- 有些父元件的屬性是會被子元件繼承的
- e.g. font, color, text-align … etc.
- Cascading
- 某個元件最終的 Style, 可能是由多個 CSS 組合而成
1 | h1 { |
1 | <!-- 最終的style會有上方三個selector指定的attr.組合而成 --> |
Specificity
當不同的Selector選到同個元件, 且設定的attr.有衝突時, 比較的順序 :
是否有宣告 !important
1
2
3body {
color : red ; /* 若有衝突, 都以宣告important的為主 */
}比較 specificity
- 誰大就以誰為主
- id : 百位數
- class : 十位數
- type : 個位數
- e.g.
- #id1 li.class1 => 111
- li ul.class1 => 012
- 誰大就以誰為主
- 比較撰寫順序, 後者覆蓋前者
Layout
Box model
Layout
- 遵循Normal flow
- 由上而下
- 由左而右
display
- 指定一個元件的layout
- 四種設定
- block : 元件佔滿一個橫向的block
- inline : 元件會由左而右排列, 直到佔滿螢幕寬才換行, 且
換行時會對元件進行切割
- inline-block : 一樣是由左而右排列, 不過換行時是以元件為單位(即不會對元件進行切割)
- none : 讓元件消失, 且不佔空間
hidden methon
visibility:hidden
- Still occupies space in normal flow
display:none
- Removed from normal flow
Centering content
- In block
text-align: center
- In inline
- 無法, 因為無法設定寬度, 沒有所謂的center
- In block’s block
- Add
margin-left:auto
andmargin-right:auto
in inner block- Tips :
- 當 margin 設為 auto 時, browser 會自動幫我們配置
最大的
margin
- 當 margin 設為 auto 時, browser 會自動幫我們配置
- Tips :
- Add
- In block
Margin collapse
- 相鄰的, 大吃小
Relative Metrics & Box Sizing
Relative to parent
width: 70%;
- 佔 parent 的70%
Relative to root
rem
1
2
3
4
5
6
7html {
font-size: 16px; /* root */
}
div {
max-width: 3rem; /* 3 * 16 px */
font-size: 1rem; /* 1 * 16 px */
}
Box sizing
- 一般來說, 在 CSS 中的 width 指的是
content
的 width- 也就是不包含 padding 和 border
- 不直覺
- 若希望一併考慮 padding 和 border
1
2
3
4
5/* 在一開始設定 */
* {
/* border & padding count into width & height */
box-sizing: border-box;
}
- 一般來說, 在 CSS 中的 width 指的是
Position
- Position
- Default(也就是當沒有去設定
position
的 value 時) : static- 遵循 normal flow
- 無法透過 top, left 等屬性去設定其上距左距…等
- position: relative
- 遵循 normal flow
- 可以去設定其 top, left … etc.
- position: absolute
- 不遵循 normal flow
- 位置是根據其
最接近且有設定 position 屬性的祖先
, 否則則根據 html 這個根元件
- position: fixed
- 不遵循 normal flow
- 位置是根據
window
(viewport) 元件- 也就是當我們 scroll 時, 不會影響其位置
- Default(也就是當沒有去設定
Float
- float
- 不遵循 normal flow
- 元件將向左/右堆疊
Stacking order
- z-index
- 設定 z-index attribute
- 值越高, 越上層
- 所有元件之 defalut 皆為 0
- 元件必須
被 positioned
才生效
- 設定 z-index attribute
1 | .higher { |
- Stacking context
- 當 B,C 元件為 A 元件的後代
- B, C 元件的 z-index 為在 A 下的
local index
- B, C 元件的 z-index 為在 A 下的
- 當 B,C 元件為 A 元件的後代