CSS中如何使用背景样式属性,看这篇文章就够用了

时间:2019-12-01 10:06:18   收藏:0   阅读:93
原文:CSS中如何使用背景样式属性,看这篇文章就够用了

css背景样式属性介绍#

属性名 属性值 描述
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使用方式#

Copy
<!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>

技术图片

Copy
<!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>

技术图片

Copy
<!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>

技术图片

属性为background-image使用方式#

Copy
<!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>

技术图片


属性为background-repeat使用方式#

属性值 描述
repeat background-repeat属性的默认值,作用表示背景图片平铺。
repeat-x 作用:将背景图片设置为水平方向平铺。
repeat-y 作用:将背景图片设置为垂直方向平铺。
no-repeat 作用:将背景图片设置为不平铺。

属性值为repeat实践#

Copy
<!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>

技术图片


属性值为repeat-x实践#

Copy
<!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>

技术图片

属性值为repeat-y实践#

Copy
<!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>

技术图片

属性值为no-repeat实践#

Copy
<!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使用方式#

Copy
<!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>

技术图片

Copy
<!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>

技术图片

Copy
<!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>

技术图片

Copy
<!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>

技术图片

Copy
<!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>

技术图片

Copy
<!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使用方式#

属性值 描述
scroll 设置背景图片滚动。
fixed 设置背景图片固定。
Copy
<!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>

技术图片

Copy
<!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使用方式#

Copy
<!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>

技术图片

评论(0)
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!