
- CSS 教程
- CSS - 首頁
- CSS - 路線圖
- CSS - 簡介
- CSS - 語法
- CSS - 選擇器
- CSS - 包含
- CSS - 測量單位
- CSS - 顏色
- CSS - 背景
- CSS - 字型
- CSS - 文字
- CSS - 圖片
- CSS - 連結
- CSS - 表格
- CSS - 邊框
- CSS - 塊級邊框
- CSS - 行內邊框
- CSS - 外邊距
- CSS - 列表
- CSS - 內邊距
- CSS - 游標
- CSS - 輪廓
- CSS - 尺寸
- CSS - 捲軸
- CSS - 行內塊
- CSS - 下拉選單
- CSS - 可見性
- CSS - 溢位
- CSS - 清除浮動
- CSS - 浮動
- CSS - 箭頭
- CSS - 調整大小
- CSS - 引號
- CSS - 順序
- CSS - 位置
- CSS - 連字元
- CSS - 懸停
- CSS - 顯示
- CSS - 聚焦
- CSS - 縮放
- CSS - 位移
- CSS - 高度
- CSS - 連字元字元
- CSS - 寬度
- CSS - 不透明度
- CSS - Z-index
- CSS - 底部
- CSS - 導航欄
- CSS - 覆蓋層
- CSS - 表單
- CSS - 對齊
- CSS - 圖示
- CSS - 圖片庫
- CSS - 註釋
- CSS - 載入器
- CSS - 屬性選擇器
- CSS - 組合器
- CSS - 根元素
- CSS - 盒模型
- CSS - 計數器
- CSS - 剪裁
- CSS - 書寫模式
- CSS - Unicode-bidi
- CSS - min-content
- CSS - 全部
- CSS - 內嵌
- CSS - 隔離
- CSS - 溢位滾動
- CSS - Justify Items
- CSS - Justify Self
- CSS - 製表符大小
- CSS - 指標事件
- CSS - Place Content
- CSS - Place Items
- CSS - Place Self
- CSS - 最大塊級大小
- CSS - 最小塊級大小
- CSS - 混合模式
- CSS - 最大內聯大小
- CSS - 最小內聯大小
- CSS - 偏移
- CSS - 強調色
- CSS - 使用者選擇
- CSS 高階
- CSS - 網格
- CSS - 網格佈局
- CSS - Flexbox
- CSS - 可見性
- CSS - 定位
- CSS - 圖層
- CSS - 偽類
- CSS - 偽元素
- CSS - @規則
- CSS - 文字效果
- CSS - 分頁媒體
- CSS - 列印
- CSS - 佈局
- CSS - 驗證
- CSS - 圖片精靈
- CSS - !important
- CSS - 資料型別
- CSS3 教程
- CSS3 - 教程
- CSS - 圓角
- CSS - 邊框圖片
- CSS - 多重背景
- CSS - 顏色
- CSS - 漸變
- CSS - 盒陰影
- CSS - 盒裝飾中斷
- CSS - 游標顏色
- CSS - 文字陰影
- CSS - 文字
- CSS - 2D 變換
- CSS - 3D 變換
- CSS - 過渡
- CSS - 動畫
- CSS - 多列
- CSS - 盒尺寸
- CSS - 工具提示
- CSS - 按鈕
- CSS - 分頁
- CSS - 變數
- CSS - 媒體查詢
- CSS - 函式
- CSS - 數學函式
- CSS - 遮罩
- CSS - 形狀
- CSS - 樣式圖片
- CSS - 特效性
- CSS - 自定義屬性
- CSS 響應式
- CSS RWD - 簡介
- CSS RWD - 視口
- CSS RWD - 網格檢視
- CSS RWD - 媒體查詢
- CSS RWD - 圖片
- CSS RWD - 影片
- CSS RWD - 框架
- CSS 工具
- CSS - PX 到 EM 轉換器
- CSS - 顏色選擇器和動畫
- CSS 資源
- CSS - 有用資源
- CSS - 討論
CSS - 背景
CSS 背景屬性用於在 HTML 內容後面設定顏色、漸變或影像。有幾個與背景相關的 CSS 屬性用於設定顏色、大小、影像、重複行為和位置。在本節中,我們將討論所有與 CSS 背景相關的屬性及其用例。
CSS 背景示例
以下部分顯示了為 HTML 元素設定顏色、影像和漸變的示例。
目錄
背景簡寫屬性
background 是以下屬性的簡寫
- background-color:設定元素的背景顏色。
- background-image:在一個元素上設定一個或多個背景影像。
- background-position:設定背景中每個影像的初始位置。
- background-size:控制背景影像的大小。
- background-repeat:控制背景中影像的重複。
- background-origin:設定背景的原點。
- background-attachment:指定背景相對於視口的位置,固定或可滾動。
- background-clip:控制背景影像延伸到元素的填充或內容框之外的程度。
background: [bg-color] [bg-image] [bg-position]/[bg-size] [bg-repeat] [bg-origin] [bg-clip] [bg-attachment]; /* Example */ background: green url('image.jpg') top/20% no-repeat border-box content-box fixed;
如果要新增background-size,則必須將其緊跟在background-position之後,並用“/”分隔。例如:“left/50%”。
背景顏色屬性
在 CSS 中,我們可以使用background-color 屬性為 div、span、body、段落等元素設定背景顏色。
示例
這裡我們為 body 和 div 標籤使用了三種不同的顏色值,有關 CSS 中顏色的完整參考,請檢視CSS 顏色章節。
<!DOCTYPE html> <html> <head> <style> body { background-color: lightgray; } div{ padding: 25px; } .firstDiv{ background-color: rgb(255, 215, 0); } .secondDiv{ background-color: #f0f0f0; } .thirdDiv{ background-color: hsl(120, 100%, 75%); } </style> </head> <body> <h2>CSS Background Colors</h2> Body Color: lightgray; <br> <br> <div class="firstDiv"> Color: rgb(255, 215, 0) </div> <br> <div class="secondDiv"> Color: #f0f0f0</div> <br> <div class="thirdDiv"> Color: hsl(120, 100%, 75%)</div> </body> </html>
背景影像屬性
CSS 允許將影像設定為另一個元素(如 div、span、body、段落等)的背景。
background-image 屬性用於設定一個或多個影像作為背景。
注意:您可以新增多個影像作為背景。您只需要用逗號分隔影像。
示例
在這個示例中,我們為 body 元素設定了一個背景影像。
<!DOCTYPE html> <html lang="en"> <head> <style> div{ background-color: rgba(255, 255, 255); opacity: 70%; padding: 20px; } body { background-image: url(/css/images/logo.png); height: 350px; } </style> </head> <body> <div> <h1>Welcome to My Website</h1> <p> This is an example of setting a background image using CSS </p> </div> </body> </html>
背景位置屬性
background-position 屬性設定元素背景影像的初始位置。影像的位置相對於background-origin屬性設定的值。
示例
<!DOCTYPE html> <html> <head> <style> .position-right { background-image: url('/css/images/tutimg.png'); background-position: right; background-repeat: no-repeat; width: 100%; height: 300px; border: 3px solid black; position: relative; } </style> </head> <body> <div class="position-right"></div> </body> </html>
背景大小屬性
CSS background-size 屬性用於設定元素背景影像的大小。背景影像可以拉伸、約束或保持其正常大小。
示例
<!DOCTYPE html> <html> <head> <style> .size-contain { background-image: url('/css/images/pink-flower.jpg'); background-size: contain; /* Render Whole image */ width: 300px; height: 300px; } </style> </head> <body> <h2>CSS background-size property</h2> <div class="size-contain"></div> </body> </html>
背景重複屬性
CSS background-repeat 屬性控制背景上影像的重複。影像可以在水平和垂直軸上重複,也可以不重複。
示例
<!DOCTYPE html> <html> <head> <style> .repeat { background-image: url('/css/images/tutimg.png'); background-repeat: repeat; width: 800px; height: 400px; position: relative; } </style> </head> <body> <h2> CSS background-repeat property </h2> <div class="repeat"></div> </body> </html>
背景原點屬性
CSS background-origin 屬性用於設定背景的原點,可以是從邊框的開始、邊框內或填充內。
示例
<!DOCTYPE html> <html> <head> <style> div { border: 10px rgb(13, 7, 190); border-style: dashed; margin: 5px; padding: 1cm; font: 700 1em sans-serif; color: aliceblue; display: inline-block; background-image: url('/css/images/yellow-flower.jpg'); height: 200px; width: 200px; background-size: contain; } .content-box { background-origin: content-box; } </style> </head> <body> <div class="content-box"> </div> <p> This image background start from content box of div element. </p> </body> </html>
背景附件屬性
CSS background-attachment 屬性決定背景上影像的位置是在視口中固定還是在其容器內滾動。
示例
<!DOCTYPE html> <html> <head> <style> .fixed { background-image: url('images/logo.png'); background-repeat: no-repeat; background-attachment: fixed; background-position: left top; background-color: lightblue; background-size: 40% 30%; padding: 5rem; width: 250px; height: 500px; } </style> </head> <body> <h2>CSS background-attachment Property</h2> <div class="fixed"> <p> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularized in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. </p> </div> </body> </html>
背景剪裁屬性
CSS background-clip 屬性用於指定如何在元素的填充框、邊框框或內容框內顯示背景影像或顏色。它決定將背景應用於元素的哪個區域。
示例
<!DOCTYPE html> <html> <head> <style> p { border: 10px dotted black; padding: 15px; background: green; color: white; } .border-area { background-clip: border-box; } .padding-area { background-clip: padding-box; } </style> </head> <body> <h2>CSS background-clip property</h2> <p class="border-area"> Background applied to the entire element. </p> <p class="padding-area"> Background applied to the content & padding area. </p> </body> </html>
CSS 背景所有屬性
與background相關的屬性都列在下表中
屬性 | 描述 | 示例 |
---|---|---|
background | 背景相關屬性的簡寫。 | |
background-attachment | 指定背景相對於視口的位置,固定或可滾動。 | |
background-clip | 控制背景影像延伸到元素的填充或內容框之外的程度。 | |
background-color | 設定元素的背景顏色。 | |
background-image | 在一個元素上設定一個或多個背景影像。 | |
background-origin | 設定背景的原點。 | |
background-position | 設定背景中每個影像的初始位置。 | |
background-position-x | 設定背景中每個影像的初始水平位置。 | |
background-position-y | 設定背景中每個影像的初始垂直位置。 | |
background-repeat | 控制背景中影像的重複。 | |
background-size | 控制背景影像的大小。 | |
background-blend-mode | 確定元素的背景影像如何相互混合。 |