第96天:CSS3 背景详解

本文详细介绍了CSS中背景属性的应用技巧,包括背景大小控制、全屏背景自适应设置、背景原点定义以及如何为元素添加多个背景。通过这些技巧,开发者可以更加灵活地控制网页背景效果。

一、背景大小

background: url("images/bg.jpg") no-repeat;
控制背景的大小
1、具体数值
background-size: 500px 500px;
2、百分比
background-size: 50% 50%;
3、cover、contain参数
  • cover 完全覆盖盒子,不一定全部显示
  • background-size: cover;
  • contain 背景图片最大化在盒子中等比显示,但不一定能铺满盒子
  • background-size: contain;

二、全屏背景自适应

 

1 body{
2             background: url("images/2.jpg") no-repeat center;
3             /*background-size: 100% 100%;*/
4             background-size: cover;/*全屏背景自适应*/
5         }

 

三、背景原点

background-origin  背景原点 控制背景从什么地方开始显示

 1 .box{
 2             width: 500px;
 3             height: 500px;
 4             border:30px solid rgba(0,255,0,0.8);
 5             padding: 30px;
 6             background: url(images/bg.jpg) no-repeat ;
 7             /*背景原点 控制背景从什么地方开始显示*/
 8             /*background-origin: padding-box;默认值*/
 9             /*background-origin: border-box;*//*盒子最外面开始*/
10             background-origin: content-box;/*盒子内容区域开始*/
11         }

四、添加多个背景

给盒子加多个背景,按照背景语法格式写,多个背景使用逗号隔开
 1 .box{
 2             width: 624px;
 3             height: 416px;
 4             border: 1px solid #000;
 5             margin: 100px auto;
 6             /*给盒子加多个背景,按照背景语法格式写,多个背景使用逗号隔开*/
 7             background: url(images/bg1.png) no-repeat left top
 8                         ,url(images/bg2.png) no-repeat right top
 9                         ,url(images/bg3.png) no-repeat right bottom
10                         ,url(images/bg4.png) no-repeat left bottom
11                         ,url(images/bg5.png) no-repeat center;
12         }

 

 


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值