打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
CSS——CSS 背景应用

文章目录

基本属性

CSS 可以添加背景颜色和背景图片,以及来进行图片设置。

background-color背景颜色
background-image背景图片地址
background-repeat是否平铺
background-position背景位置
background-attachment背景固定还是滚动
背景的合写(复合属性)
background:背景颜色 背景图片地址 背景平铺 背景滚动 背景位置

背景图片(image)

语法:

background-image : none | url (地址) 

参数:

none :  无背景图(默认的)
url :  使用绝对或相对地址指定背景图像

background-image 属性允许指定一个图片展示在背景中(只有CSS3才可以多背景)可以和 background-color 连用。 如果图片不重复地话,图片覆盖不到地地方都会被背景色填充。 如果有背景图片平铺,则会覆盖背景颜色。

小技巧: 我们提倡 背景图片后面的地址,不要加引号。

背景平铺(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属性。默认值为:(0% 0%)。
如果只指定了一个值,该值将用于横坐标。纵坐标将默认为50%。第二个值将用于纵坐标。

注意:

  1. position 后面是x坐标和y坐标。 可以使用方位名词或者精确单位。
  2. 如果和精确单位和方位名字混合使用,则必须是x坐标在前,y坐标后面。比如 background-position: 15px top; 则 15px 一定是 x坐标 top是 y坐标。

但是实际工作用的最多的,就是背景图片居中对齐了。

背景附着

语法:

background-attachment : scroll | fixed 

参数:

scroll :  背景图像是随对象内容滚动
fixed :  背景图像固定

说明:

设置或检索背景图像是随对象内容滚动还是固定的。

背景简写

background属性的值的书写顺序官方并没有强制标准的。为了可读性,建议大家如下写:

background:背景颜色 背景图片地址 背景平铺 背景滚动 背景位置

background: transparent url(image.jpg) repeat-y  scroll 50% 0 ;

背景透明(CSS3)

CSS3支持背景半透明的写法语法格式是:

background: rgba(0,0,0,0.3);

最后一个参数是alpha 透明度 取值范围 0~1之间

注意: 背景半透明是指盒子背景半透明, 盒子里面的内容不收影响。

背景图方位案例

注意:

  1. positon后面可以跟方位名词 他们之间可以没有上下顺序
  2. position 如果只写一个方位名词,另外一个默认是居中的
  3. position 后面也可以跟 值px 但是 必须有顺序 x 在前面 y 后面 不能颠倒

效果图


(图自己找哈,别找我哈)

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
/*body {*/
/*background-color: pink;*/
/*background-image: url(images/3.jpg);*/
/*repeat :  背景图像在纵向和横向上平铺
no-repeat :  背景图像不平铺
repeat-x :  背景图像在横向上平铺
repeat-y :  背景图像在纵向平铺 */
/*background-repeat: repeat-y;
} */

div {
width: 500px;
height: 500px;
background-color: purple;
background-image: url(images/30565.jpg);
background-repeat: no-repeat; /*不平铺*/
/*background-position: x  y ;*/
/*background-position: center top;  top 上  bottom 下  left  左  right  右 */
/*background-position: center center;  top 上  bottom 下  left  左  right  右 */
/*background-position: bottom  right;  top 上  bottom 下  left  左  right  右 */

/*1. positon后面可以跟方位名词  他们之间可以没有上下顺序
2. position 如果只写一个方位名词,另外一个默认是居中的
3. position 后面也可以跟 值px 但是 必须有顺序 x 在前面 y 后面 不能颠倒 */
/*background-position: left;  top 上  bottom 下  left  左  right  右 */
/*background-position: bottom;  top 上  bottom 下  left  左  right  右 */
/*background-position:  12px 50px;*/
/*background-position:  50px 12px ;*/
background-position: 100px center;
}
</style>
</head>
<body>

<div>


</div>
</body>
</html>
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
CSS控制背景图片
Свитер. Обсуждение на LiveInternet
11种常用css样式之background学习
CSS样式更改篇——背景Background
css背景图片的设置
网页背景图片代码
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服