背景颜色(color)
- 语法:
background-color:颜色值; 默认的值是 transparent 透明的
背景图片(image)
- 语法:
background-image : none | url (url)
参数 | 作用 |
---|---|
none | 无背景图(默认的) |
url | 使用绝对或相对地址指定背景图像 |
background-image : url(images/demo.png);
- 小技巧: 我们提倡 背景图片后面的地址,url不要加引号。
背景平铺(repeat)
- 语法:
background-repeat : repeat | no-repeat | repeat-x | repeat-y
参数 | 作用 |
---|---|
repeat | 背景图像在纵向和横向上平铺(默认的) |
no-repeat | 背景图像不平铺 |
repeat-x | 背景图像在横向上平铺 |
repeat-y | 背景图像在纵向平铺 |
背景位置(position) 重点
- 语法:
background-position : length || length
background-position : position || position
参数 | 值 | |||||
---|---|---|---|---|---|---|
length | 百分数 | 由浮点数字和单位标识符组成的长度值 | ||||
position | top | center | bottom | left | center | right |
注意:
- 必须先指定background-image属性
- position 后面是x坐标和y坐标。 可以使用方位名词或者精确单位。
- 如果指定两个值,两个值都是方位名字,则两个值前后顺序无关,比如left top和top left效果一致
- 如果只指定了一个方位名词,另一个值默认居中对齐。
- 如果background-position后面是精确坐标, 那么第一个,肯定是 x,第二的一定是y
- 如果只指定一个数值,那该数值一定是x坐标,另一个默认垂直居中
- 如果指定的两个值是 精确单位和方位名字混合使用,则第一个值是x坐标,第二个值是y坐标
实际工作用的最多的,就是背景图片居中对齐了。
背景附着
- 背景附着就是解释背景是滚动的还是固定的
语法:
background-attachment : scroll | fixed
参数 | 作用 |
---|---|
scroll | 背景图像是随对象内容滚动 |
fixed | 背景图像固定 |
背景简写
- background:属性的值的书写顺序官方并没有强制标准的。为了可读性,建议大家如下写:
- background: 背景颜色 背景图片地址 背景平铺 背景滚动 背景位置;
- 语法:
background: transparent url(image.jpg) repeat-y scroll center top ;
案例:
导航栏案例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.nav{
text-align: center;
}
.nav a{
width: 120px;
height: 50px;
color: #ffffff;
background: url(image/bg.png) no-repeat;
display: inline-block;
line-height: 50px;
text-decoration: none;
}
.nav a:hover{
background-image: url(image/bgc.png);
}
</style>
</head>
<body>
<div class="nav">
<a href="#">网站首页</a>
<a href="#">网站首页</a>
<a href="#">网站首页</a>
<a href="#">网站首页</a>
<a href="#">网站首页</a>
<a href="#">网站首页</a>
</div>
</body>
</html>
背景透明(CSS3)
- 语法:
background: rgba(0, 0, 0, 0.3);
- 最后一个参数是alpha 透明度 取值范围 0~1之间
- 我们习惯把0.3的0省略掉 这样写 background: rgba(0, 0, 0, .3);
- 注意: 背景半透明是指盒子背景半透明,盒子里面的内容不受影响
- 因为是CSS3 ,所以低于 ie9 的版本是不支持的。
背景总结
属性 | 作用 | 值 |
---|---|---|
background-color | 背景颜色 | 预定义的颜色值/十六进制/RGB代码 |
background-image | 背景图片 | url(图片路径) |
background-repeat | 是否平铺 | repeat/no-repeat/repeat-x/repeat-y |
background-position | 背景位置 | length/position 分别是x 和 y坐标, 切记 如果有 精确数值单位,则必须按照先X 后Y 的写法 |
background-attachment | 背景固定还是滚动 | scroll/fixed |
背景简写 | 更简单 | 背景颜色 背景图片地址 背景平铺 背景滚动 背景位置; 他们没有顺序 |
背景透明 | 让盒子半透明 | background: rgba(0,0,0,0.3); 后面必须是 4个值 |
版权属于:乐心湖's Blog
本文链接:https://www.xn2001.com/archives/459.html
声明:博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!
2 comments
最近看到很多网站都在用这个主题啊,很火爆的样子哇。以前不敢做图片站,现在网速快了,似乎无所谓了... 但我觉得,图片站太费空间了吧。
我这些图片是放在又拍云的,有免费的cdn。 相当于你把图片丢到了别人家的空间,用了很久了一直都算比较稳定,博主可以了解一下哈