搜尋

背景縮寫格式 background

透過 CSS 設定元素背景時,除了一個個的設定每種背景樣式,也可使用「縮寫格式」設定背景,這篇教學會介紹如何使用背景縮寫格式 background 設定元素背景。

快速導覽:

CSS 教學 - 背景縮寫格式 background

使用 background

background 可以設定元素的「背景顏色」和「背景圖」,沒有繼承特性,使用 background 相當方便,可以不按照順序使用背景圖的樣式,但也有下列的幾個注意事項:

  • 使用 background-size 一定要接在 background-position 之後,且使用「/」分隔,若只有長度數值則表示 background-position 屬性值
  • 出現 background-originbackground-clip 相關屬性值時,如果只有一個就是 background-origin,出現的第二個是 background-clip
  • 出現 background-color 屬性值時,背景色一定位於背景圖下層
  • 使用 background 可能會覆蓋掉對應的背景圖樣式,反之其他背景樣式也可能會覆蓋 background 裡對應的樣式
  • 不支援 background-blend-mode

下方列出一些符合規範的 background 寫法:

div {
  background: red;  
  /* 等同 */
  /* background-color: red */

  background: red url("圖片網址");
  /* 等同 */
  /* background-color: red; */
  /* background-image: url("圖片網址"); */

  background: red url("圖片網址") space;
  /* 等同 */
  /* background-color: red; */
  /* background-image: url("圖片網址"); */
  /* background-repeat: space; */

  background: red url("圖片網址") space padding-box;
  /* 等同 */
  /* background-color: red; */
  /* background-image: url("圖片網址"); */
  /* background-repeat: space; */
  /* background-origin: padding-box; */

  background: red url("圖片網址") space padding-box padding-box;
  /* 等同 */
  /* background-color: red; */
  /* background-image: url("圖片網址"); */
  /* background-repeat: space; */
  /* background-origin: padding-box; */
  /* background-clip: padding-box; */

  background: red url("圖片網址") space padding-box padding-box top right;
  /* 等同 */
  /* background-color: red; */
  /* background-image: url("圖片網址"); */
  /* background-repeat: space; */
  /* background-origin: padding-box; */
  /* background-clip: padding-box; */
  /* background-position: top right; */

  background: red url("圖片網址") space padding-box padding-box top right/50%;
  /* 等同 */
  /* background-color: red; */
  /* background-image: url("圖片網址"); */
  /* background-repeat: space; */
  /* background-origin: padding-box; */
  /* background-clip: padding-box; */
  /* background-position: top right; */
  /* background-size: 50%; */

  background: red url("圖片網址") space padding-box padding-box top right/50% fixed;
  /* 等同 */
  /* background-color: red; */
  /* background-image: url("圖片網址"); */
  /* background-repeat: space; */
  /* background-origin: padding-box; */
  /* background-clip: padding-box; */
  /* background-position: top right; */
  /* background-size: 50%; */
  /* backgroud-attachment: fixed; */
}

使用 background 會根據權重規則,覆蓋對應的樣式或被對應的樣式覆蓋,下方的範例會使用 background 樣式覆蓋 background-size 樣式 ( cover 變成 30% ),接著再透過 background-color 覆蓋 background 裡的背景色 ( 紅色變成黃色 )。

線上展示:https://codepen.io/oxxo/pen/xxoOGMr

<!-- HTML 程式碼 -->
<div></div>

<!-- CSS 程式碼 -->
<style>
  div {
    width: 300px;
    height: 300px;
    border: 1px solid #000;
    background-size: cover;
    background: red top left/30% round url("https://steam.oxxostudio.tw/download/css/function-filter-demo.png");
    background-color: yellow;
  }
</style>

CSS 教學 - 背景縮寫格式 background -  background 覆蓋對應的樣式或被對應的樣式覆蓋

background 設定多重背景

background 也可以設定「多重背景」,使用方式與背景圖的多重背景相同,使用「逗號 ,」分隔不同的背景,使用時需要注意,只要某一項背景寫錯,就會發生樣式錯誤而無法顯示背景。

特別注意,「背景顏色只能出現一次」,且必須出現在多重背景的「最後一項」

下方的範例會使用 background 呈現多重背景的效果。

線上展示:https://codepen.io/oxxo/pen/OJeXVdd

<!-- HTML 程式碼 -->
<div></div>

<!-- CSS 程式碼 -->
<style>
  div {
    width: 300px;
    height: 300px;
    border: 1px solid #000;
    background: url("https://steam.oxxostudio.tw/download/css/function-filter-demo.png") no-repeat center/60%, 
      top left/20% space url("https://steam.oxxostudio.tw/download/css/function-filter-demo.png"),
      red top left/5% space url("https://steam.oxxostudio.tw/download/css/function-filter-demo.png");
  }
</style>

CSS 教學 - 背景縮寫格式 background - background 設定多重背景

小結

背景縮寫格式 background 是相當方便好用的樣式,使用時只需要注意一些細節規則,就可以大幅減少背景圖樣式的程式碼。

意見回饋

如果有任何建議或問題,可傳送「意見表單」給我,謝謝~

CSS 教學

基本介紹

認識 CSS 開始使用 CSS CSS 語法規則 CSS 命名原則

CSS 選擇器

認識 CSS 選擇器 優先順序 ( 權重 ) 樣式繼承與聯集 元素選擇器 ID 和 Class 選擇器 屬性選擇器 文件結構選擇器 虛擬類別選擇器 ( 結構 ) 虛擬類別選擇器 ( 類型 ) 虛擬類別選擇器 ( 輸入 ) 虛擬類別選擇器 ( 行為 ) 虛擬類別選擇器 ( 超連結 ) 虛擬類別選擇器 ( 邏輯 ) 虛擬類別選擇器 ( 其他 ) 虛擬元素選擇器 群組與組合選擇器

數值與單位

關鍵字與文字數值 長度與角度單位 顏色單位 位置名稱與時間單位

變數與函式

變數 數學計算 文字與清單計數 生成內容與引號 色彩模型 漸層色 影像濾鏡

文字樣式

使用通用字型 使用外部字型 @font-face 定義字型 文字尺寸 常用文字樣式 文字換行 文字空白與 Tab 大小 文字行高與縮排 文字水平與垂直對齊 文字書寫方向 特殊文字樣式

元素容器

容器顯示類型 ( display ) 元素 display 對照表 盒子模型 ( Box Model ) 寬度與高度 內邊距 ( padding ) 外邊界 ( margin ) 邊框 ( border ) 邊框圓角 影像邊框 輪廓 ( outline ) 可見性與透明度 內容溢出與裁切

背景與陰影

背景顏色 背景圖 ( 定位、尺寸 ) 背景圖 ( 固定、重複 ) 背景圖 ( 多重背景、混合 ) 背景縮寫 ( background ) 容器陰影 ( box-shadow )

清單與表格

清單樣式 清單計數器 定義計數規則 表格基本樣式 表格邊框樣式 表格內容寬度與對齊

基本排版與定位

元素排版方式 浮動 ( float ) 浮動形狀 定位 ( position )

Flexbox 彈性排版

Flexbox 彈性盒子 Flexbox 對齊方式 Flexbox 彈性伸縮

Grid 網格排版

Grid 網格容器與格線 Grid 網格空間與命名 Grid 網格流向與間距 Grid 排列網格項目