CSS (二) 背景

css背景样式
1、设置背景色:background-color
设置input标签背景色为蓝色,这个属性接受任何合法的颜色值

input { background-color: blue; }

在这里插入图片描述2、设置背景图像:background-image
设置背景图像,必须为这个属性设置一个 URL 值为图片路径。

body { 
      background-image: url(log.png); 
      }

这时背景图片会默认铺满页面:
在这里插入图片描述
背景图平铺:background-repeat
如果需要在页面上对背景图像进行平铺,可以使用 background-repeat 属性。
属性值 :
repeat: 导致图像在水平垂直方向上都平铺,默认效果
repeat-x :图像只在水平上重复

body {
       background-image: url(log.png);
	   background-repeat: repeat-x 
	   }

在这里插入图片描述

repeat-y :图像在垂直方向上重复

body { 
       background-image: url(log.png);
	   background-repeat: repeat-y 
	   }

在这里插入图片描述
no-repeat :不允许图像在任何方向上平铺。

body { 
	   background-image: url(log.png);
	   background-repeat: no-repeat 
	   }

在这里插入图片描述
3、背景定位background-position
可以利用 background-position 属性改变图像在背景中的位置。
定位方式:位置关键字、百分比、长度值
在这里插入图片描述
位置关键字:

top: 固定顶点位置,否则图片会上下剧中而显示不全

body { 
	   background-image: url(log.png);
	   background-repeat: no-repeat;
	   background-position: center top;
	   }

在这里插入图片描述
left:

body { 
	   background-image: url(log.png);
	   background-repeat: no-repeat;
	   background-position: letf top;
	   }

在这里插入图片描述
right:

body { 
	   background-image: url(log.png);
	   background-repeat: no-repeat;
	   background-position: right top;
	   }

在这里插入图片描述

bottom:

body { 
	   background-image: url(log.png);
	   background-repeat: no-repeat;
	   background-position: right bottom;
	   }

在这里插入图片描述
百分比数值:

body { 
	   background-image: url(log.png);
	   background-repeat: no-repeat;
	   background-position: 50% 50%;
	   }

在这里插入图片描述
长度值:
长度值解释的是元素内边距区左上角的偏移。偏移点是图像的左上角。
比如,如果设置值为 50px 100px,图像的左上角将在元素内边距区左上角向右 100 像素、向下 200 像素的位置上:

body { 
	   background-image: url(log.png);
	   background-repeat: no-repeat;
	   background-position: 100px 200px;
	   }

在这里插入图片描述
3、背景关联:background-attachment
声明图像相对于可视区是固定的,因此不会受到滚动的影响。
如果文档比较长,那么当文档向下滚动时,背景图像也会随之滚动。当文档滚动到超过图像的位置时,图像就会消失。
如下:滑动页面时图像会固定不动

body { 
	   background-image: url(log.png);
	   background-repeat: no-repeat;
	   background-position: 100px 200px;
	   background-attachment: fixed;
	   }

在这里插入图片描述
4、背景图大小:background-size

通过像素指定

body { 
	   background-image: url(log.png);
	   background-repeat: no-repeat;
	   background-size: 300px 200px;
	   }

在这里插入图片描述

通过百分比指定

body { 
	   background-image: url(log.png);
	   background-repeat: no-repeat;
	   background-size: 10% 10%; 
	   }

background-size: 10% 10%;

原始图片大小

body { 
	   background-image: url(log.png);
	   background-repeat: no-repeat;
	   background-size: auto; 
	   }

在这里插入图片描述

背景填满容器

body { 
	   background-image: url(log.png);
	   background-repeat: no-repeat;
	   background-size: cover;
	   }

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

久醉绕心弦,

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值