📕
HTML & CSS
  • 網頁設計 - HTML & CSS
  • 1 簡介
    • 1.1 講者簡介
    • 1.2 課程須知
    • 1.3 網站前端簡介
    • 1.4 開發工具簡介
    • 1.5 第一個網頁
  • 2 HTML
    • 2.1 html 文件基本結構
    • 2.2 宣告、標籤、屬性、元素
    • 2.3 保留文字格式
    • 2.4 表示程式的標籤
    • 2.5 註解
    • 2.6 網頁標題
    • 2.7 中介資料(Meta Data)
    • 2.8 縮寫
    • 2.9 斷行與水平分隔線
    • 2.10 段落
    • 2.11 內容標題
    • 2.12 判別區塊與行內
    • 2.13 列表
    • 2.14 清單
    • 2.15 連結
    • 2.16 圖片
    • 2.17 獨立內容
    • 2.18 表格
    • 2.19 嵌入外站
    • 2.20 span 和 div
    • 2.21 引用
    • 2.22 有結構的語意元素
    • 2.23 結構驗證
    • 2.24 HTML Entity(實體)
    • 2.25 影片
    • 2.26 音訊
    • 2.27 其它標籤
    • 2.28 表單
    • 2.29 HTML 所有標籤
  • 3 CSS
    • 3.1 套用 CSS
    • 3.2 註解
    • 3.3 Selectors - 屬性及值
    • 3.4 Selectors - 符號
    • 3.5 Selectors - class 和 id
    • 3.6 關於優先權
    • 3.7 排版 - 關於 display 屬性
    • 3.8 排版 - 區塊模型(Box Model)
    • 3.9 排版 - vertical-align 對齊
    • 3.10 練習 - 固定式版型
    • 3.11 關於 Pseudo Element
    • 3.12 關於 Pseudo Class
    • 3.13 文字樣式
    • 3.14 關於溢載
    • 3.15 連結樣式
    • 3.16 列表樣式
    • 3.17 表格樣式
    • 3.18 背景樣式
    • 3.19 排版 - 關於定位(position)
    • 3.20 排版 - 關於浮動(float)
    • 3.21 關於不透明度 opacity
    • 3.22 關於 visibility、pointer-events
    • 3.23 關於 outline
    • 3.24 表單樣式
    • 3.25 效果 - 游標
    • 3.26 效果 - 圓角
    • 3.27 效果 - 區塊陰影
    • 3.28 效果 - 文字陰影
    • 3.29 效果 - 轉換 transform
    • 3.30 Flexbox 排版 - Container
    • 3.31 Flexbox 排版 - Items
    • 3.32 Column 多欄排版模式
    • 3.33 其它補充
  • 4 練習
  • 5 作業:網頁切版
  • 6 參考資料
Powered by GitBook
On this page
  • background-color 背景單色
  • background-image 背景圖片
  • background-repeat 設定圖片的重覆狀態
  • 背景圖片相關屬性
  • background-size 設定背景圖大小
  • background-position 設定背景圖位置
  • background-attachment 固定背景圖片
  • background-origin 設定背景圖顯示的區域
  • background-clip 設定背景圖裁切的區域
  • background 背景圖片線性漸層色
  • 一個區域可以疊多張背景圖
  • 其它資源
  1. 3 CSS

3.18 背景樣式

Previous3.17 表格樣式Next3.19 排版 - 關於定位(position)

Last updated 1 year ago

background-color 背景單色

可以使用各種顏色格式:16 進位代碼、rgba、hsla 等。

div{
  background-color: red;
  /* 或 */
  background-color: #aabbcc;
  /* 或 */
  background-color: rgba(50, 50, 50, 1);
  /* 或 */
  background-color: hsla(120deg, 50%, 50%, 1);
}

background-image 背景圖片

有一張圖片:

然後針對整個網頁(或任何元素區塊皆可),撰寫以下 CSS:

body{
  background-image: url("https://alldata.sgp1.digitaloceanspaces.com/images/christmas-dark.png");
}

結果呈現:會發現此圖在 <body> 區域內,不斷地水平、垂直反覆出現圖片。

background-repeat 設定圖片的重覆狀態

body{
  background-image: url("https://alldata.sgp1.digitaloceanspaces.com/images/christmas-dark.png");
  background-repeat: no-repeat; /* repeat-x repeat-y */
}

可選的值有:

  • repeat:預設,指的是圖片向 x 軸、y 軸反覆出現。

  • repeat-x:圖片向 x 軸反覆出現。

  • repeat-y:圖片向 y 軸反覆出現。

  • no-repeat:不反覆出現,只出現一次。

範例:

背景圖片相關屬性

有一張圖片,大小為 寬 640px、高 428px:

撰寫如下:

<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <title>這是網頁標題</title>
    <style>
      div.place_img{
        width: 300px;
        height: 300px;
        border:1px solid red;
        background-image: url("https://alldata.sgp1.digitaloceanspaces.com/images/lion.jpg");
        background-repeat: no-repeat;
      }
    </style>
  </head>
  <body>
    <div class="place_img">這是 div 的內容</div>
  </body>
</html>

結果如下:背景圖超過 div 的區域,會被遮掉看不到。

background-size 設定背景圖大小

div.place_img{
  width: 300px;
  height: 300px;
  border:1px solid red;
  background-image: url("./images/lion.jpg");
  background-repeat: no-repeat;
  
  /* 背景圖的寬度會變成佔 div 區域寬度的 80%。 */
  background-size: 80%;
  
  /*
  例:背景圖的寬度及高度會變成佔 div 區域的寬高都 50%,圖片會變形。
  background-size: 50% 50%;
  
  例:背景圖寬高顯示固定單位,寬為 150px,高為 100px。
  background-size: 150px 100px;
  */
}

結果呈現:

特殊關鍵字(contain):(背景圖片 「寬」或「高」 其中一個會變成 100%,讓圖片在區域內能夠全部出現,不能被遮到。)

div.place_img{
  width: 300px;
  height: 300px;
  border:1px solid red;
  background-image: url("./images/lion.jpg");
  background-repeat: no-repeat;
  
  /* 背景圖在該區域內要完整出現(寬或高其中一個會是 100%) */
  background-size: contain;
}

結果呈現:

如果將 div 的高度改成 100px 的話,則結果會變成:

特殊關鍵字(cover):(背景圖片 「寬」或「高」 其中一個會變成 100%,讓圖片能夠佔滿整個區域,圖片會有部份被遮到。)

div.place_img{
  width: 300px;
  height: 300px;
  border:1px solid red;
  background-image: url("./images/lion.jpg");
  background-repeat: no-repeat;
  
  /* 背景圖要完整覆蓋該區域(寬或高其中一個會是 100%),背景圖超出去的部份會被遮掉。 */
  background-size: cover;
}

結果如圖:

background-position 設定背景圖位置

div.place_img{
  width: 300px;
  height: 300px;
  border:1px solid red;
  background-image: url("./images/lion.jpg");
  background-repeat: no-repeat;
  background-size: 80%;
  
  /* 水平方向:置中;垂直方向:置底 */
  background-position: center bottom;
}

結果如圖:

所以該圖在區域內,搭配關鍵字的話,共有以下幾種組合:

background-position: left top; /* 水平方向:置左;垂直方向:置頂 */
background-position: center top; /* 水平方向:置中;垂直方向:置頂 */
background-position: right top;  /* 水平方向:置右;垂直方向:置頂 */
background-position: left center;    /* 水平方向:置左;垂直方向:置中 */
background-position: center center;  /* 水平方向:置中;垂直方向:置中 */
background-position: right center;   /* 水平方向:置右;垂直方向:置中 */
background-position: left bottom;    /* 水平方向:置左;垂直方向:置底 */
background-position: center bottom;  /* 水平方向:置中;垂直方向:置底 */
background-position: right bottom;   /* 水平方向:置右;垂直方向:置底 */

也可以指定水平方向、固定方向的距離,單位可以是 px 或 %:

/* 水平方向:離左側 50px 的距離;垂直方向:離上側 10px 的距離 */
background-position: 50px 10px;

結果如圖:

也可以指定以下,表示距離右側 10px;距離下方 20px:

background-position: right 10px bottom 20px;

background-attachment 固定背景圖片

有以下三種模式:

  • scroll:(預設),背景圖會隨著頁面滑動而跟著滑動,元素內則不會。

  • local:元素區域內滑動則會跟著滑動。

  • fixed:背景圖會固定在網頁的可視區域(viewport)。不管自己元素內、或頁面滑動,該背景圖皆不會滑動。

scroll(預設):

背景圖會隨著頁面滑動而跟著滑動;元素區域內滑動則不會跟著滑動。

local:

元素區域內滑動則會跟著滑動。

fixed(行動版瀏覽器不一定支援):

背景圖會固定在網頁的可視區域。

background-origin 設定背景圖顯示的區域

  • padding-box:(預設值)。設定圖片顯示區域是從 padding 區域開始。

  • border-box:設定圖片顯示區域是從 border 區域開始。

  • content-box:設定圖片顯示區域是從 content 區域開始。

範例:

background-clip 設定背景圖裁切的區域

  • padding-box:設定圖片顯示區域到 padding 區域,其它裁切。

  • border-box:(預設值)。設定圖片顯示區域是到 border 區域,其它裁切。

  • content-box:設定圖片顯示區域是到 content 區域,其它裁切。

範例:

background 背景圖片線性漸層色

background: linear-gradient(方向, 顏色停止點1, 顏色停止點2, ...);

方向可選的有:

  • to bottom:這是預設值。

  • to top

  • to right

  • to left

  • to bottom right

  • to bottom left

  • to top right

  • to top left

  • 40deg (代表 to 40 度 的方向)

範例:

關於顏色停駐點:

1、紅色在 30px 的位置,綠色也在 30px 的位置,沒有任何漸層效果發生;0px ~ 30px 由紅色填滿;30px到最後,由綠色填滿:

background: linear-gradient(red 30px, green 30px);

示意圖:

2、紅色在 30px 的位置,綠色在 100px 的位置,30px ~ 100px 這個區域,紅色到綠色漸層;0px ~ 30px 由紅色填滿,100px 到最後由綠色填滿:

background: linear-gradient(red 30px, green 100px);

示意圖:

3、30px 到 100px 由紅色變到綠色;100px 到 250px 由綠色變到黃色:(推理方式同上)

background: linear-gradient(red 30px, green 100px, yellow 250px);

示意圖:

範例:

一個區域可以疊多張背景圖

其它資源

可以直接參考,即各屬性值以半形逗號做區隔。

這個例子
uiGradients
Gradient CSS Generator