本章整理 CSS 中與顏色、背景相關的屬性。
功能:設定前景或元素的顏色。數值可使用顏色關鍵字或 RGB 值。例如 color:purple、color:#00ff80、color:rgb(255,200,100)、color:rgb(75%,0%,0%)。
功能:設定頁面或元素的背景顏色。數值可用顏色名或 RGB 值,也可設為 transparent(透明,使背景為預設)。
功能:定義背景圖形。數值為 none(不用圖形)或 url(圖形網址)。也可搭配 background-color,以便在找不到圖形時替代顯示。
功能:控制背景圖形是否重複排列。
數值:repeat(水平與垂直重複,預設值)、repeat-x(水平重複)、repeat-y(垂直重複)、no-repeat(不重複)。此屬性通常與 background-image 和 background-position 一起使用。
功能:指定背景是隨元素一起滾動,還是固定在頁面某位置上。
數值:scroll(隨元素一起滾動,預設值)、fixed(固定)。此屬性與 background-image 一起使用。
功能:在空間中定位元素背景。
數值:長度(相對或絕對單位)、百分比、垂直位置關鍵字(top、center、bottom)、水平位置關鍵字(left、center、right)。例如 top left、left top 與 0% 0% 都表示圖形從元素框左上角開始。範例:Body {background-image:url(logo.gif); background-position:50% 50%} 可讓背景圖形在頁面上居中顯示。
功能:簡寫屬性,可一次設定所有背景屬性,包括 background-attachment、background-color、background-image、background-position、background-repeat。