<!doctype html><html lang="zh"><head> <meta charset="UTF-8"> <title>test</title> <style> div{ width: 220px; height: 60px; line-height: 60px; background-color: transparent; } </style></head><body> <!--背景颜色 background-color: 颜色;//transparent(默认) --> <div> 我是div </div></body></html>
<!doctype html><html lang="zh"><head> <meta charset="UTF-8"> <title>test</title> <style> div{ width: 500px; height: 500px; background-image: url("./images/bg.jpg"); } </style></head><body> <!--背景图片 background-image: url("图片地址"); none:默认值 --> <div> 我是div </div></body></html>
<!doctype html><html lang="zh"><head> <meta charset="UTF-8"> <title>test</title> <style> body{ background-image: url("./images/bg.jpg"); background-repeat:repeat-x; } </style></head><body> <!--背景重复 background-repeat:值; 值: repeat 重复(默认) no-repeat 不重复 repeat-x 横向重复 repeat-y 纵向重复 --></body></html>
<!doctype html><html lang="zh"><head> <meta charset="UTF-8"> <title>test</title> <style> div{ width: 500px; height: 200px; background-color: skyblue; background-image: url("./images/bg.jpg"); background-repeat: no-repeat; background-size: 400px 100px; background-size: contain; } </style></head><body> <!--背景尺寸 background-size: 宽度 高度; contain:把背景图片扩展到最大尺寸,使宽度或高度完全适应内容区域 cover:把背景图片扩展至足够大,使背景图片完全覆盖区域,背景图片的某些区域会被覆盖 当只设置一个值时,另一个值会赋值为auto --> <div></div></body></html>
<!doctype html><html lang="zh"><head> <meta charset="UTF-8"> <title>test</title> <style> div{ width: 800px; height: 600px; background-color: skyblue; background-image: url("./images/bg.jpg"); background-repeat: no-repeat; background-position: right bottom; } </style></head><body> <!--背景定位 background-position: 值1(水平方向) 值2(垂直方向); 1.方向定位:top bottom right left center 只设置一个值时,另一个值是center 2.尺寸定位:参考位置是背景图片左上角与元素左上角水平垂直距离,设置一个值时,这个值一定是水平位置 3.百分比定位:0% 0% 图片在左上角 50% 50% 图片水平垂直居中 100% 100% 图片在右上角 4.背景定位可以使用负值 --> <div></div></body></html>
<!doctype html><html lang="zh"><head> <meta charset="UTF-8"> <title>test</title> <style> div{ width: 700px; height: 500px; background-image: url("./images/01.jpg"); background-repeat: no-repeat; background-attachment: scroll; } </style></head><body> <!--背景固定 background-attachment: 值; scroll:默认值,背景图片会随页面的滚动而滚动 fixed:背景图片不会随页面的滚动而滚动 --> <div></div></body></html>
<!doctype html><html lang="zh"><head> <meta charset="UTF-8"> <title>test</title> <style> div{ width: 700px; height: 500px; background-image: url("./images/01.jpg"); background-repeat: no-repeat; background-attachment: scroll; } </style></head><body> <!-- background-image: url("./images/01.jpg"); background-repeat: no-repeat; background-attachment: scroll; 可以简化为: background:颜色值 背景图片 背景重复 背景定位/背景尺寸 背景固定; --> <div></div></body></html>