CSS背景
CSS?背景
CSS 背景屬性用于定義HTML元素的背景。
CSS 屬性定義背景效果:
background-color
background-image
background-repeat
background-attachment
background-position
背景顏色
background-color 屬性定義了元素的背景顏色.
CSS中,顏色值通常以以下方式定義:
十六進(jìn)制 - 如:"#ff0000"
RGB - 如:"rgb(255,0,0)"
顏色名稱 - 如:"red"
以下實(shí)例中, h1, p, 和 div 元素?fù)碛胁煌谋尘邦伾?
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <style> h1 { background-color:#6495ed; } p { background-color:#e0ffff; } div { background-color:#b0c4de; } </style> </head> <body> <h1>靜夜思</h1> <div> 床前明月光, <p>疑是地上霜。</p> 舉頭望明月, <p>低頭思故鄉(xiāng)。</p> </div> </body> </html>
背景圖像
background-image 屬性描述了元素的背景圖像.
默認(rèn)情況下,背景圖像進(jìn)行平鋪重復(fù)顯示,以覆蓋整個(gè)元素實(shí)體.
頁(yè)面背景圖片設(shè)置實(shí)例:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>圖片背景測(cè)試</title> <style> body { background-image:url('http://pic.58pic.com/58pic/14/94/21/80U58PICPJM_1024.jpg'); background-color:#cccccc; } </style> </head> <body> <h1>明天你好??!</h1> </body> </html>
背景圖像 - 水平或垂直平鋪
默認(rèn)情況下 background-image 屬性會(huì)在頁(yè)面的水平或者垂直方向平鋪。
一些圖像如果在水平方向與垂直方向平鋪,這樣看起來(lái)很不協(xié)調(diào),如下所示:?
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>圖片背景測(cè)試</title> <style> body { background-image:url('http://img01.taopic.com/141002/240423-14100210124112.jpg'); } </style> </head> <body> <h1>明天你好!!</h1> </body> </html>
如果圖像只在水平方向平鋪 (repeat-x),?如果圖像只在水平方向平鋪 (repeat-y)。
背景圖像- 設(shè)置定位與不平鋪
?讓背景圖像不影響文本的排版
如果你不想讓圖像平鋪,你可以使用 background-repeat 屬性:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>圖片背景測(cè)試</title> <style> body { background-image:url('http://img01.taopic.com/141002/240423-14100210124112.jpg'); background-repeat:no-repeat } </style> </head> <body> <div> <h1>明天你好??!</h1> </div> </body> </html>
以上實(shí)例中,背景圖像與文本顯示在同一個(gè)位置,為了讓頁(yè)面排版更加合理,不影響文本的閱讀,我們可以改變圖像的位置。
可以利用 background-position 屬性改變圖像在背景中的位置
background-position:right top;
背景- 簡(jiǎn)寫(xiě)屬性
在以上實(shí)例中我們可以看到頁(yè)面的背景顏色通過(guò)了很多的屬性來(lái)控制。
為了簡(jiǎn)化這些屬性的代碼,我們可以將這些屬性合并在同一個(gè)屬性中.
背景顏色的簡(jiǎn)寫(xiě)屬性為 "background":
當(dāng)使用簡(jiǎn)寫(xiě)屬性時(shí),屬性值的順序?yàn)椋?
background-color
background-image
background-repeat
background-attachment
background-position
以上屬性無(wú)需全部使用,你可以按照頁(yè)面的實(shí)際需要使用.