清華大佬耗費(fèi)三個(gè)月吐血整理的幾百G的資源,免費(fèi)分享!....>>>
CSS 背景
CSS 背景屬性用于定義HTML元素的背景。
CSS 屬性定義背影效果:
- background-color
- background-image
- background-repeat
- background-attachment
- background-position
背景顏色
background-color 屬性定義了元素的背景顏色.
頁面的背景顏色使用在body的選擇器中:
實(shí)例
CSS中,顏色值通常以以下方式定義:
- 十六進(jìn)制 - 如:"#ff0000"
- RGB - 如:"rgb(255,0,0)"
- 顏色名稱 - 如:"red"
以下實(shí)例中, h1, p, 和 div 元素?fù)碛胁煌谋尘邦伾?
實(shí)例
p {background-color:#e0ffff;}
div {background-color:#b0c4de;}
背景圖像
background-image 屬性描述了元素的背景圖像.
默認(rèn)情況下,背景圖像進(jìn)行平鋪重復(fù)顯示,以覆蓋整個(gè)元素實(shí)體.
頁面背景圖片設(shè)置實(shí)例:
實(shí)例
下面是一個(gè)例子是一個(gè)糟糕的文字和背景圖像組合。文本可讀性差:
實(shí)例
背景圖像 - 水平或垂直平鋪
默認(rèn)情況下 background-image 屬性會(huì)在頁面的水平或者垂直方向平鋪。
一些圖像如果在水平方向與垂直方向平鋪,這樣看起來很不協(xié)調(diào),如下所示:?
實(shí)例
{
background-image:url('gradient2.png');
}
如果圖像只在水平方向平鋪 (repeat-x), 頁面背景會(huì)更好些:
實(shí)例
{
background-image:url('gradient2.png');
background-repeat:repeat-x;
}
背景圖像- 設(shè)置定位與不平鋪
讓背景圖像不影響文本的排版
如果你不想讓圖像平鋪,你可以使用 background-repeat 屬性:
實(shí)例
{
background-image:url('img_tree.png');
background-repeat:no-repeat;
}
以上實(shí)例中,背景圖像與文本顯示在同一個(gè)位置,為了讓頁面排版更加合理,不允許文本的閱讀,我們可以改變圖像的位置。
可以利用 background-position 屬性改變圖像在背景中的位置:
實(shí)例
{
background-image:url('img_tree.png');
background-repeat:no-repeat;
background-position:right top;
}
背景- 簡寫屬性
在以上實(shí)例中我們可以看到頁面的背景顏色通過了很多的屬性來控制。
為了簡化這些屬性的代碼,我們可以將這些屬性合并在同一個(gè)屬性中.
背景顏色的簡寫屬性為 "background":
實(shí)例
當(dāng)使用簡寫屬性時(shí),屬性值得順序?yàn)椋?
- background-color
- background-image
- background-repeat
- background-attachment
- background-position
以上屬性無需全部使用,你可以按照頁面的實(shí)際需要使用.
這個(gè)實(shí)例使用了先前介紹的CSS,你可以查看相應(yīng)實(shí)例: CSS 實(shí)例

更多實(shí)例
如何設(shè)置固定的背景圖像
本例演示如何設(shè)置固定的背景圖像。圖像不會(huì)隨著頁面的其他部分滾動(dòng)。
CSS 背景屬性
Property | 描述 |
---|---|
background | 簡寫屬性,作用是將背景屬性設(shè)置在一個(gè)聲明中。 |
background-attachment | 背景圖像是否固定或者隨著頁面的其余部分滾動(dòng)。 |
background-color | 設(shè)置元素的背景顏色。 |
background-image | 把圖像設(shè)置為背景。 |
background-position | 設(shè)置背景圖像的起始位置。 |
background-repeat | 設(shè)置背景圖像是否及如何重復(fù)。 |
掃碼二維碼 獲取免費(fèi)視頻學(xué)習(xí)資料
- 本文固定鏈接: http://www.wangchenghua.com/j/css/1000171/
- 免費(fèi): Python視頻資料獲取