學習 CSS 的第一步,是先搞清楚它能控制哪些層面的樣式。以下把可設定的屬性依類別整理出來,並補充盒模型與布局的關鍵概念。
包含:字體屬性、文本屬性、顏色及背景屬性、邊框屬性、容器屬性(邊距與填充)、分類屬性、游標屬性。
字體屬性:font-family、font-style、font-variant、font-weight、font-size
文本屬性:word-spacing、letter-spacing、text-decoration、vertical-align、text-transform、text-align、text-indent、line-height
顏色背景屬性:color、background(background-color、background-image、background-repeat、background-attachment、background-position)
邊框屬性:border(border-top、border-right、border-bottom、border-left;border-width、border-style、border-color)
容器屬性:margin、padding、width、height、float、clear
分類屬性:display、white-space、list-style-type、list-style-image、list-style-position、list-style
游標屬性:auto、crosshair、default、hand、move、e-resize、ne-resize、nw-resize、n-resize、se-resize、sw-resize、s-resize、w-resize、text、wait、help
理解 CSS 盒模型對布局至關重要——每個元素都有一個對應的方塊盒。HTML 布局通常採用 div + CSS 的方式,將網頁切分成多個方塊,各自用 div 承載,再用 CSS 進行排版。常用布局屬性包括:margin、padding、float、clear、top、right、bottom、left、z-index、position。
把這些屬性摸熟,你就能從容應對絕大多數常見的頁面排版需求。