分開設定背景相關屬性雖清晰,但較冗長:
background-image: url("logo.png");
background-position: top center;
background-repeat: no-repeat;
使用 background 縮寫屬性可合併為單一行:
background: url(logo.png) no-repeat top center;
完整順序為:background-color background-image background-repeat background-attachment background-position。省略的屬性會套用預設值(如 repeat、scroll、0% 0%),請留意覆蓋行為。
現代瀏覽器支援多背景堆疊,以逗號分隔:
background:
url(icon.png) no-repeat 10px 10px,
linear-gradient(to right, #fff, #eee);
先寫的圖層在最上層。