HTML
標籤的背景顏色或背景圖像。屬性名 | 屬性值 | 描述 |
---|---|---|
background-color | #f00、red、rgb(255,0,0) | 設置背景顏色 |
background-image | url(背景圖片路徑) | 設置背景圖像 |
background-repeat | repeat、repeat-x、repeat-y、no-repeat | 設置背景圖片是否平鋪和平鋪方向。 |
background-position | left、center、right、top、bottom、固定值、百分比 | 設置背景圖片位置 |
background-attachment | scroll、fixed | 設置背景圖片位置是不是固定或滾動。 |
background | 屬性值就是以上的全部值 | 設置背景的縮寫形式。 |
background-color
實踐,實踐內容如:將HTML
頁面中的div
背景設置爲紅色。代碼塊css
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>background-color屬性使用</title> <style> div{ background-color: red; } </style> </head> <body> <div></div> </body> </html>
結果圖html
div
標籤設置了background-color
屬性,還有屬性值爲red
,div
標籤背景沒有發生任何變化呢?div
標籤裏面沒有任何內容、 div
標籤沒有設置寬高度。div
標籤放置一些內容。<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>background-color屬性使用</title> <style> div{ background-color: red; } </style> </head> <body> <div>成功不是戰勝別人,而是改變本身。</div> </body> </html>
background-color
和屬性值爲red
才真正的被渲染出來。div
內容消除掉,而後咱們給div
設置寬高度爲200px
像素,看看屬性爲background-color
和屬性值爲red
,可否被渲染出來呢?代碼塊ui
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>background-color屬性使用</title> <style> div{ width: 200px; height: 200px; background-color: red; } </style> </head> <body> <div></div> </body> </html>
結果圖url
注意:如今你們應該明白了屬性爲
background-color
,只有設置了寬高度的元素或者元素裏面有內容,才能被渲染出來。3d
background-image
用於給元素設置背景圖片,將圖片路徑放在url()
括號當中纔會被渲染。background-image
和屬性爲background-color
是一致的,都必需要有寬高度和內容纔會被渲染。讓咱們進入屬性爲background-image
實踐,實踐內容如:給div標籤設置背景圖片,div
標籤寬高度設置爲400px
像素。code
代碼塊htm
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>background-image屬性使用</title> <style> div{ width: 400px; height: 400px; background-image: url(./img/001.png); } </style> </head> <body> <div></div> </body> </html>
結果圖blog
注意:屬性爲
background-image
默認圖片是平鋪的,因此這個結果圖並不奇怪哈。圖片
background-repeat
有2種做用如:background-repeat
的屬性值有4種如: repeat
、repeat-x
、repeat-y
、no-repeat
。background-repeat
屬性值說明表:屬性值 | 描述 |
---|---|
repeat | background-repeat屬性的默認值,做用表示背景圖片平鋪。 |
repeat-x | 做用:將背景圖片設置爲水平方向平鋪。 |
repeat-y | 做用:將背景圖片設置爲垂直方向平鋪。 |
no-repeat | 做用:將背景圖片設置爲不平鋪。 |
background-repeat
而且屬性值爲repeat
實踐,實踐內容如:將div
標籤背景圖片設置爲平鋪。代碼塊it
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>background-repeat屬性使用</title> <style> div{ width: 400px; height: 400px; background-image: url(./img/001.png); background-repeat: repeat; } </style> </head> <body> <div></div> </body> </html>
結果圖
注意:假設咱們不設置屬性爲
background-repeat
而且屬性值爲repeat
,也沒有關係的默認就是平鋪。
background-repeat
而且屬性值爲repeat-x
實踐,實踐內容如:將div
標籤背景圖片設置爲水平方向平鋪,爲了給初學者一個直觀的印象,筆者將div
標籤添加了一個邊框樣式。代碼塊
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>background-repeat屬性使用</title> <style> div{ width: 400px; height: 400px; border: 1px solid red; background-image: url(./img/001.png); background-repeat:repeat-x; } </style> </head> <body> <div></div> </body> </html>
結果圖
background-repeat
而且屬性值爲repeat-y
實踐,實踐內容如:將div
標籤背景圖片設置爲垂直方向平鋪,爲了給初學者一個直觀的印象,筆者將div
標籤添加了一個邊框樣式。代碼塊
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>background-repeat屬性使用</title> <style> div{ width: 400px; height: 400px; border: 1px solid red; background-image: url(./img/001.png); background-repeat:repeat-y; } </style> </head> <body> <div></div> </body> </html>
結果圖
background-repeat
而且屬性值no-repeat
實踐,實踐內容如:將div
標籤背景圖片設置爲不平鋪,爲了給初學者一個直觀的印象,筆者將div
標籤添加了一個邊框樣式。代碼塊
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>background-repeat屬性使用</title> <style> div{ width: 400px; height: 400px; border: 1px solid red; background-image: url(./img/001.png); background-repeat:no-repeat; } </style> </head> <body> <div></div> </body> </html>
結果圖
background-position
做用:設置背景圖片的位置在哪。background-position
的屬性值分爲3種使用方式如:英文單詞、固定值、百分比。left
(居左)、right
(居右)、top
(居上)、bottom
(居下)、center
(居中)background-position
使用英文單詞設置背景的位置實踐。代碼塊
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>background-position屬性使用</title> <style> div{ width: 400px; height: 400px; border: 1px solid red; background-image: url(./img/001.png); background-repeat:no-repeat; background-position:center; background-position: top right; } </style> </head> <body> <div></div> </body> </html>
結果圖
代碼塊
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>background-position屬性使用</title> <style> div{ width: 400px; height: 400px; border: 1px solid red; background-image: url(./img/001.png); background-repeat:no-repeat; background-position:center; background-position: bottom left; } </style> </head> <body> <div></div> </body> </html>
結果圖
代碼塊
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>background-position屬性使用</title> <style> div{ width: 400px; height: 400px; border: 1px solid red; background-image: url(./img/001.png); background-repeat:no-repeat; background-position:center; background-position: bottom right; } </style> </head> <body> <div></div> </body> </html>
結果圖
代碼塊
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>background-position屬性使用</title> <style> div{ width: 400px; height: 400px; border: 1px solid red; background-image: url(./img/001.png); background-repeat:no-repeat; background-position:center; background-position: center center; } </style> </head> <body> <div></div> </body> </html>
結果圖
div
標籤背景圖片的位置實踐。代碼塊
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>background-position屬性使用</title> <style> div{ width: 400px; height: 400px; border: 1px solid red; background-image: url(./img/001.png); background-repeat:no-repeat; background-position:center; background-position: 100px; } </style> </head> <body> <div></div> </body> </html>
結果圖
px
單位換成%
百分號就是按照元素的寬高度進行百分比計算背景圖片的位置。20px
像素。代碼塊
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>background-position屬性使用</title> <style> div{ width: 400px; height: 400px; border: 1px solid red; background-image: url(./img/001.png); background-repeat:no-repeat; background-position:center; background-position: 20px bottom; } </style> </head> <body> <div></div> </body> </html>
結果圖
background-attachment
做用:就是設置背景圖片位置是不是固定或者是滾動的。background-attachment
屬性值說明表:屬性值 | 描述 |
---|---|
scroll | 設置背景圖片滾動。 |
fixed | 設置背景圖片固定。 |
background-attachment
實踐,實踐內容將div
標籤背景圖片位置滾動和固定位置,方便你們理解滾動和固定筆者將在div
標籤中放置一些內容。background-attachment
默認屬性值就是scroll
滾動的。代碼塊
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>background-position屬性使用</title> <style> div{ width: 400px; height: 400px; border: 1px solid red; background-image: url(./img/001.png); background-repeat:no-repeat; background-attachment:scroll; } </style> </head> <body> <div> 微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰, 微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰, 微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰, 微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰, 微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰, 微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰, 微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰, 微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰, 微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰, 微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰, 微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰, 微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰, 微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰。 </div> </body> </html>
結果圖
代碼塊
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>background-position屬性使用</title> <style> div{ width: 400px; height: 400px; border: 1px solid red; background-image: url(./img/001.png); background-repeat:no-repeat; background-attachment:fixed; } </style> </head> <body> <div> 微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰, 微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰, 微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰, 微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰, 微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰, 微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰, 微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰, 微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰, 微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰, 微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰, 微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰, 微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰, 微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰。 </div> </body> </html>
結果圖
background
就是設置背景的一個縮寫。本章內容你們都掌握了這個就如小菜一點不值一提哈,廢話就很少說了直接上代碼塊咯。代碼塊
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>background屬性使用</title> <style> div{ width: 400px; height: 400px; border: 1px solid red; background: url(./img/001.png) no-repeat top right scroll; } </style> </head> <body> <div> 微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰, 微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰, 微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰, 微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰, 微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰, 微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰, 微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰, 微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰, 微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰, 微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰, 微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰, 微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰, 微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰。 </div> </body> </html>
結果圖