顏色是網頁設計中的關鍵要素之一。CSS 提供了多種方式來定義顏色,包括以下幾種常見方式:
red, blue, green 等。#ff0000(紅色)、#00ff00(綠色)、#0000ff(藍色)。rgb(255, 0, 0) 表示紅色。rgba(255, 0, 0, 0.5) 表示半透明紅色。hsl(0, 100%, 50%) 表示紅色。hsla(0, 100%, 50%, 0.5)。color: 設置文本的顏色。background-color: 設置元素的背景顏色。background-color: 設置背景顏色。background-image: 設置背景圖片。background-image: url('image.jpg');
background-repeat: 控制背景圖片是否重複。
repeat, no-repeat, repeat-x, repeat-y。background-position: 設置背景圖片的位置。
center, top, bottom, left, right 或具體的像素值、百分比。background-size: 控制背景圖片的大小。
auto, cover, contain 或具體的尺寸。background-attachment: 設置背景是否隨滾動而移動。
scroll, fixed, local。將多個背景屬性縮為一行
body {
background: url('background.jpg') no-repeat center/cover #f4f4f4;
}
*透過使用 rgba 或 hsla 可以設置背景顏色的透明度。
*背景圖片本身的透明度可以通過使用 opacity 或圖像處理來控制。
<div class="header">
<h1>歡迎來到我的網站</h1>
</div>
.header {
background-image: url('background.jpg');
background-size: cover;
background-position: center;
background-color: rgba(0, 0, 0, 0.5); /* 半透明的背景色 */
color: white; /* 設置文本顏色 */
padding: 50px;
text-align: center;
}
網頁呈下圖: