CSS 背景

學習如何使用 CSS 更改元素背景 可以使用多個 CSS 屬性來更改元素的背景: background-color 背景顏色 background-image 背景圖像 background-clip 背景區域 background-position 背景位置 background-origin 背景原點 background-repeat 背景重複 background-attachment 背景附著 background-size 背景大小 還有一個縮寫屬性 background,可以縮短定義並將它們分組在一行。 background-color 接受顏色值,可以是顏色關鍵字,也可以是 rgb 或 hsl 值: p { background-color: yellow; } div { background-color: #333; } 除了使用顏色,你還可以使用圖像作為元素的背景,指定圖像位置的 URL: div { background-image: url(image.png); } background-clip 可讓你決定背景圖片或顏色的區域。默認值為 border-box,該值延伸到邊框外緣。 其他值包括: padding-box:將背景延伸到填充邊緣,不包括邊框。 content-box:將背景延伸到內容邊緣,不包括填充。 inherit:應用父元素的值。 當使用圖像作為背景時,你需要使用 background-position 屬性來設置圖像的放置位置。left、right、center 都是 X 軸的有效值,top、bottom 是 Y 軸的有效值: div { background-position: top right; } 如果圖像比背景小,則需要使用 background-repeat 屬性設置行為。它應該在所有軸上重複 repeat-x、repeat-y,或者在所有軸上上重複 repeat?後者是默認值。還有一個值 no-repeat。...