CSS 背景速記
CSS background - 簡寫屬性
為了縮短程式碼,也可以將所有背景屬性指定在一個單獨的屬性中。這被稱為簡寫屬性。
不必編寫
body {
background-color: #ffffff;
background-image: url("img_tree.png");
background-repeat: no-repeat;
background-position: right top;
}
您可以使用簡寫屬性 background
示例
使用簡寫屬性在一個宣告中設定背景屬性
body {
background: #ffffff url("img_tree.png") no-repeat right top;
}
自己動手試一試 »
當使用簡寫屬性時,屬性值的順序為
background-color
background-image
background-repeat
background-attachment
background-position
如果缺少其中一個屬性值,只要其他屬性值按此順序排列,就沒有關係。請注意,在上面的示例中,我們沒有使用 background-attachment 屬性,因為它沒有值。
所有 CSS 背景屬性
屬性 | 描述 |
---|---|
background | 在一個宣告中設定所有背景屬性 |
background-attachment | 設定背景影像是固定還是隨頁面其餘部分滾動 |
background-clip | 指定背景的繪製區域 |
background-color | 設定元素的背景顏色 |
background-image | 設定元素的背景影像 |
background-origin | 指定背景影像的位置 |
background-position | 設定背景影像的起始位置 |
background-repeat | 設定背景影像的重複方式 |
background-size | 指定背景影像的大小 |
影片:CSS 背景速記

