CSS 的背景颜色样式语法 : 默认的背景颜色是 transparent 透明 ;
background-color:颜色值;
background-color 属性 可以 定义 文本颜色 , 其颜色值有三种表示方式 :
代码示例 :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>背景颜色</title>
<base target="_blank"/>
<style>
/* 设置背景颜色 */
.background {
width: 200px;
height: 200px;
background-color: pink;
}
</style>
</head>
<body>
<div class="background">
背景颜色测试
</div>
</body>
</html>
展示效果 :
CSS 的背景图片样式语法 : 背景图片的链接需要写在 url()
中 , 并且 url() 中的链接可以没有双引号 ;
background-image: url(相对路径);
代码示例 :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>背景图片</title>
<base target="_blank"/>
<style>
/* 设置背景图片 */
.background {
width: 200px;
height: 200px;
color: white;
/* 背景图片设置
1. 在 url() 中设置相对链接
2. url() 中的链接没有双引号
*/
background-image: url(images/image.jpg);
}
</style>
</head>
<body>
<div class="background">
背景图片测试
</div>
</body>
</html>
展示效果 :
上面示例中 , 使用的图片像素是 200x200 像素 , 设置到 200x200 的盒子中 , 正好充满整个背景 ;
但是如果设置到 400x400 像素的盒子中 , 就会出现如下样式 , 背景会重叠展示多个 ;
通过设置 background-repeat
属性 , 可以设置平铺效果 ;
background-repeat: repeat;
, 背景在 X 和 Y 轴方向上平铺 ;background-repeat: no-repeat;
, 背景放在盒子左上角 ;
background-repeat: repeat-x;
, 背景在 X 轴方向上平铺 ;
background-repeat: repeat-y;
, 背景在 Y 轴方向上平铺 ;
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有