首页 / 知识
HTML与CSS中背景相关属性
2023-04-11 15:16:00
一.背景尺寸属性
1.什么是背景尺寸属性
背景尺寸属性是CSS3中新增的一个属性,专门用于设置背景图片大小
background-size:xxxx;
取值:
1.具体像素>>background-size:200px100px;
2.百分比>>background-size:100%80%;
3.宽度等比拉伸>>background-size:auto100px;
4.高度等比拉伸>>background-size:100pxauto;
5.cover>>background-size:cover;
5.1告诉系统图片需要等比拉伸
5.2告诉系统图片需要拉伸到宽度<a>和</a>高度都填满元素
6.contain>>background-size:contain;
6.1告诉系统图片需要等比拉伸
6.2告诉系统图片需要拉伸到宽度<a>或</a>高度都填满元素(<a>只保证一边填满</a>)
background-size
二.背景图片定位区域属性
<a>background-origin</a>:告诉系统背景图片从什么区域开始显示,默认情况下就是从padding区域开始显示;
取值:
1.<a>padding-box</a>:默认值>>background-origin:padding-box;告诉系统背景图片从什么区域开始显示,默认情况下就是从padding区域开始显示;
2.<a>border-box</a>>>background-origin:border-box;从border位置开始
3.<a>content-box</a>>>background-origin:content-box;从content位置开始
<htmllang="en"><head><metacharset="UTF-8"><title>113-背景图片定位区域属性</title><style>*{margin:0;padding:0;}ulli{list-style:none;float:left;width:100px;height:100px;text-align:center;line-height:100px;border:20pxdashed#000;padding:50px;margin-left:20px;background:url("images/dog.webp")no-repeat;}ulli:nth-child(2){/*告诉系统背景图片从什么区域开始显示,默认情况下就是从padding区域开始显示*/background-origin:padding-box;}ulli:nth-child(3){background-origin:border-box;}ulli:nth-child(4){background-origin:content-box;}</style></head><body><ul><li>默认</li><li>padding</li><li>border</li><li>content</li></ul></body></html>
背景图片定位区域属性
三.背景绘制区域属性
<a>background-clip:xxx;</a>背景绘制区域属性是专门用于指定从哪个区域开始绘制背景的,默认情况下会从border区域开始绘制背景
<htmllang="en"><head><metacharset="UTF-8"><title>114-背景绘制区域属性</title><style>*{margin:0;padding:0;}ulli{list-style:none;float:left;width:100px;height:100px;text-align:center;line-height:100px;border:20pxdashed#000;padding:50px;margin-left:20px;background:redurl("images/dog.webp")no-repeat;}ulli:nth-child(2){/*背景绘制区域属性是专门用于指定从哪个区域开始绘制背景的,默认情况下会从border区域开始绘制背景*/background-clip:padding-box;}ulli:nth-child(3){background-clip:border-box;}ulli:nth-child(4){background-clip:content-box;}</style></head><body><ul><li>默认</li><li>padding</li><li>border</li><li>content</li></ul></body></html>
背景绘制区域属性(红色为绘制区域)
四.多重背景图片
<a>先添加的背景图片会盖住后添加的背景图片</a>
元素c3之后可以设置多张背景图片
多张背景图片之间用逗号隔开即可
background:url("images/animal1.webp")no-repeatlefttop,url("images/animal2.webp")no-repeatrighttop,url("images/animal3.webp")no-repeatleftbottom;
注意点:
先添加的背景图片会盖住后添加的背景图片
background:url("images/animal1.webp")no-repeatlefttop,url("images/animal2.webp")no-repeatrighttop,url("images/animal3.webp")no-repeatleftbottom,url("images/animal4.webp")no-repeatrightbottom,url("images/animal5.webp")no-repeatcentercenter;
建议在编写多重背景时拆开编写
background-image:url("images/animal1.webp"),url("images/animal2.webp"),url("images/animal3.webp");background-repeat:no-repeat,no-repeat,no-repeat;background-position:lefttop,righttop,leftbottom;
完整代码如下:
<htmllang="en"><head><metacharset="UTF-8"><title>115-多重背景图片</title><style>*{margin:0;padding:0;}p{width:500px;height:500px;border:1pxsolid#000;margin:0auto;/*多张背景图片之间用逗号隔开即可注意点:先添加的背景图片会盖住后添加的背景图片建议在编写多重背景时拆开编写*//*background:url("images/animal1.webp")no-repeatlefttop,url("images/animal2.webp")no-repeatrighttop,url("images/animal3.webp")no-repeatleftbottom,url("images/animal4.webp")no-repeatrightbottom,url("images/animal5.webp")no-repeatcentercenter;*/background-image:url("images/animal1.webp"),url("images/animal2.webp"),url("images/animal3.webp");background-repeat:no-repeat,no-repeat,no-repeat;background-position:lefttop,righttop,leftbottom;}</style></head><body><p></p></body></html>
多重背景图片
四.多重背景图片联系
<a>先添加的背景图片会盖住后添加的背景图片</a>
<htmllang="en"><head><metacharset="UTF-8"><title>116-多重背景图片-练习</title><style>*{margin:0;padding:0;}p{width:600px;height:190px;border:1pxsolid#000;margin:100pxauto;background-image:url("images/bg-plane.webp"),url("images/bg-sun.webp"),url(images/bg-clouds.webp);background-repeat:no-repeat,no-repeat,no-repeat;background-size:50px50px,50px50px,autoauto;background-position:50px150px,400px50px,0px0px;animation:move10slinear0sinfinitenormal;}@keyframesmove{from{background-position:50px150px,400px50px,0px0px;}to{background-position:500px-150px,400px50px,-600px0px;}}</style></head><body><p></p></body></html>
本文转载自中文网 |
最新内容
相关内容
python如何读取列表中元素的位置?
python如何读取列表中元素的位置?,位置,数据,异常,培训,字符串,元素,索引,方法,示例,结果,python读取列表中元素位置的方法:1、使用index()方python3中怎么编写类?
python3中怎么编写类?,培训,方式,步骤,关键字,以上,过程,方法,更多,内容,python中创建类的方法:方式一:利用class关键字classChinese(object):python中获取路径的三种方法
python中获取路径的三种方法,工作,代码,情况,培训,下来,路径,文件,也就是,桌面,目录,python中获取路径总结下来分为三种情况:1、获取工作目录python如何调用另一个文件夹中的内
python如何调用另一个文件夹中的内容?,系统,培训,文件,模块,内容,路径,函数,所在,前缀,语句,python中调用另外一个文件夹中的内容:1、同一文件python中怎么对一个数进行因式分解
python中怎么对一个数进行因式分解?,代码,培训,因式分解,因数,个数,最小,整数,数组,假定,分解,1、Python因式分解代码:importtime#对一个数进python中函数怎么表示?
python中函数怎么表示?,名称,标准,培训,代码,函数,圆括号,字符串,表达式,选择性,自变量,python中函数定义规则:·函数代码块以def关键词开头,后chr在python中怎么用?
chr在python中怎么用?,数字,培训,整数,字符,参数,示例,语法,范围,形式,以上,python中chr()用一个范围在range(256)内的(就是0~255)整数作参数,python中怎么样进行矩阵运算?
python中怎么样进行矩阵运算?,矩阵,培训,数据,数值,行列,函数,开头,元素,以上,时候,python的numpy库提供矩阵运算的功能,因此我们在需要矩阵运Python怎么取出列表中的相邻元素?
Python怎么取出列表中的相邻元素?,代码,异常,培训,元素,指针,序列,对象,表示,语句,函数,1、python的迭代器。iter()能把一个序列生成为一个和如何在python代码中指定保存的文件
如何在python代码中指定保存的文件格式,代码,培训,文件格式,格式,二进制文件,文件,后缀,以上,方法,更多,python指定保存文件格式的方法:1、保python如何看变量属性
python如何看变量属性,培训,属性,函数,变量,参数,对象,方法,列表,范围内,字典,1、使用dir()函数查看dir()函数不带参数时,返回当前范围内的变Python中怎么计算圆周长?
Python中怎么计算圆周长?,公式,培训,圆周,半径,浮点,以上,结果,更多,内容,python中怎么计算圆周长呢?1、首先输入圆的半径2、其次将输入的半