An interactive study guide covering CSS basics, selectors, style properties, specificity, and stylesheets. 涵蓋 CSS 基礎、選擇器、樣式屬性、優先級及樣式表的互動式學習指南。
CSS (Cascading Style Sheet) is a stylesheet language used to control the appearance and presentation of HTML elements. It allows you to apply styles (such as colours, fonts, spacing, and layout) to HTML elements, separating the presentation from the content. CSS(層疊樣式表)是一種樣式表語言,用於控制 HTML 元素的外觀和呈現方式。它讓你可以為 HTML 元素套用樣式(如顏色、字型、間距和版面),將呈現與內容分離。
Inline styles place style code directly inside an HTML element's style attribute. While convenient, they have major drawbacks:
內嵌樣式是將樣式代碼直接放在 HTML 元素的 style 屬性中。雖然方便,但有主要缺點:
Every HTML element is rendered as an invisible "box" on the web page. The box has four key regions that can be styled: 每個 HTML 元素都以不可見的「盒子」形式呈現在網頁上。盒子有四個可樣式化的關鍵區域:
A CSS rule consists of a selector and a declaration block.
CSS 規則由選擇器和聲明區塊組成。
Targets every element of that type in the HTML document. No special character prefix needed.
針對 HTML 文件中所有該類型的元素。無需特殊前綴字符。
p, h1, ul, li, table, tr, img
有效範例:p、h1、ul、li、table、tr、img
Targets a specific single element with a unique id attribute. Uses a # prefix.
針對具有唯一 id 屬性的特定單一元素,使用 # 前綴。
id attribute must be unique within the same HTML document. No two elements can share the same ID.
⚠️ 同一 HTML 文件中,id 屬性必須是唯一的。不能有兩個元素共用相同的 ID。
Targets any element with that class name. Uses a . (dot) prefix. Multiple classes can be added to one element.
針對所有具有該類別名稱的元素,使用 .(點)前綴。一個元素可以添加多個類別。
| Selector Type選擇器類型 | Prefix前綴 | Targets目標 |
|---|---|---|
| p, h1, li… | None無 | All elements of that type所有該類型的元素 |
| #myID | # | One specific unique element一個特定唯一元素 |
| .myClass | . | Any element with that class所有具有該類別的元素 |
| Property屬性 | What it controls控制內容 | Example values範例值 |
|---|---|---|
| colour | Text colour文字顏色 | red, #ff8c42, rgb(144,3,20) |
| font-family | Font face字體類型 | Arial, Georgia, "Courier New" |
| font-weight | Boldness粗體程度 | bold, normal, 400, 700 |
| font-size | Text size文字大小 | 12px, smaller, larger |
| text-decoration | Decorative lines裝飾線條 | underline, none, line-through |
| Property屬性 | What it controls控制內容 | Example values範例值 |
|---|---|---|
| text-align | Horizontal text alignment水平文字對齊 | left, right, centre, justify |
| background-colour | Background colour背景顏色 | coral, #ffb804, rgb(80,135,6) |
| border | Element border appearance元素邊框外觀 | solid, dashed, solid red 2px |
| padding | Space between content and border內容與邊框之間的空間 | padding-top: 20px |
| margin | Space between elements (or page edge)元素之間(或頁面邊緣)的空間 | margin-left: 10px |
| height / width | Element dimensions元素尺寸 | height: 50px, width: 150px |
Adjust the controls to see CSS properties in action: 調整控制項以查看 CSS 屬性的效果:
Specificity determines which CSS declaration wins when two selectors both target the same element and try to set the same style property with different values. 優先級決定當兩個選擇器都針對同一元素,並試圖用不同值設置相同樣式屬性時,哪個 CSS 聲明勝出。
Higher = wins when there's a conflict 越高 = 衝突時獲勝
Click the card to reveal the definition. Use the arrows to navigate. 點擊卡片查看定義,使用箭頭切換卡片。