Css-background屬性
Background-color
背景顏色
background-color: color|transparent|initial|inherit;
Specifies that the background color should be
transparent. This is default
Background-image 背景圖片
background-image: url|none|initial|inherit;
可用url(); 裡面放連結或是檔案相對位置
linear-gradient (start color , finish color);
一種屬性可以使用來做背景顏色的轉換,所以至少要有兩種顏色(可以更多,用逗號隔開),開始顏色和結束顏色,
語法
background-image: linear-gradient(direction, color-stop1, color-stop2, ...);
direction定義他的顏色的方向和角度
方向 to(bottom center top)(left center right)
角度 90deg、180deg、270deg、360deg(可以玩玩看)
Usage
我常使用的地方是在圖片上覆蓋一層透明的黑色至灰色背景,讓他有層次感。
background-image:
radial-gradient(shape size at position,
start-color, ..., last-color);
一個類似從中心往外漸變的屬性(自己理解)
shape 有ellipse預設 、 circle 兩種
size 定義漸變的規模 有farthest-corner預設、closest-side、…
position:開始的地方 預設為center
repeating-linear-gradient() 重複 a linear gradient
repeating-radial-gradient() 重複 a radial gradien
(就是漸變的地方重複,可以看成是背景重複,都差不多)
Background-repeat 背景是否重複
重複有
水平重複repeat-x
圖片往右堆疊
垂直重複repeat-y
圖片往下堆疊
不重複設定 no-repeat
Round屬性:背景圖片會延伸填滿整個瀏覽器,簡單說就是沒有間距
Space: 盡可能重複背景圖像而不剪裁。 第一個和最後一個圖像固定在元素的任一側,並且空白在圖像之間均勻分佈(有間距但均勻分布,會盡量填滿圖片)
Background-attachment 背景固定或捲動
背景圖片要固定或捲動
固定使用fixed
捲動scroll 預設
Local
The background image will scroll with the
element's contents
目前沒遇到,改天補充
Background-position 背景位置
若要改變圖片位置,可以設定
Background-position: (left、center、right ) (top、center、bottom)
以上各個屬性可整合在一個background裡
Ex:
Background: #fff url(“xxx.jpg”) no-repeat right top;
屬性值的順序為:
- background-color
- background-image
- background-repeat
- background-attachment
- background-position
位置設定也可用 x% y%表示 xà左上角開始 yà到右下角
Background-size背景大小
一般: auto自動 cover覆蓋(因為放大圖片容意失真)、contain(只包含圖片原始模樣,所以放背景時無法填滿)
自己能夠設定
1.
width、height(此為固定順序)
2.
百分比
多重背景background: url(),url();
Background-size: contain,cover;
額外屬性
Background-clip可以設定背景是否在border裡面
background-clip:
border-box(為預設,所以border邊框會在clip裡面)
padding-box(border圍繞在背景外)
content-box(背景延伸到內容邊緣 類似文字或內容等等)
initial 跟預設一樣
inherit 設定完可以讓相同區塊的地方繼承樣式
background-origin 可以設定背景在border或內容裡的位置
background-origin:
padding-box|border-box|content-box|initial|inherit;