css背景图片属性

本文详细解释了CSS中的四个关键背景属性:background-repeat(重复方式),background-position(位置设置),background-size(大小调整)和background-attachment(是否固定)。通过实例展示了如何控制网页元素的背景图像展示效果。
摘要由CSDN通过智能技术生成

基础代码:

div {
  width: 200px;
  height: 200px;
  background: url(./css-logo.png);
}

<div></div>

1、background-repeat:默认是repeat

        设置背景图片在容器内是否平铺。

        

background-repeat: repeat-y;
background-repeat: repeat-x;
background-repeat: no-repeat;

2、background-position:可使用关键字或坐标(数字+单位,正负均可;水平方向,正数向右,垂直方向,正数向下。)

        注:设置时可设置两个值,如center top,若两个值相同,可简写为center。

background-position: 100px 100px;
background-position: right;
background-position: center;
background-position: left;
background-position: bottom;
background-position: top;

3、background-size:设置背景图片大小(可设置为关键字:cover、contain;百分比-根据盒子尺寸计算大小;数字+单位)

        contain:等比例缩放,以完全装入,可能显示空白;

        cover:等比例缩放,以完全覆盖,可看不全。

background-size: 50px;
background-size: 50%;
background-size: contain;
background-size: cover;

4、background-attachment:fixed,背景不会随元素的内容滚动。

background-attachment: fixed;

小贴士:

大背景图一般设置为background-repeat: no-repeat;background-position: center top;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值