在 CSS 中,‘background-image’属性用于使用 CSS 设置元素的背景图像。背景图像属性有 4 个不同的属性,如下所述。
-
Url () – 它需要一个图像路径或远程 URL 从特定位置获取图像并将其设置为背景。
-
None – 用户可以使用 none 作为背景图像属性的值来删除背景。
-
Initial – 它设置初始背景,在大多数情况下没有背景。
-
继承 – 它设置与父元素相同的背景图像。
语法
用户可以按照以下语法在CSS中使用“background-image”属性。
background-image: url('URL');
background-image: inherit;
background-image: initial;
background-image: none;
如上面的语法所示,我们可以使用不同的值来设置背景图像。
示例 1
在下面的示例中,我们创建了 HTML div 元素并使用 CSS 指定了高度和宽度。此外,我们还使用了“background-image”CSS 属性来设置 div 元素的背景。
在输出中,用户可以观察到,如果 div 元素的尺寸高于图像,则它会重复设置背景图像。
<html>
<head>
<style>
.div-ele {
background-image: url('https://png.pngtree.com/thumb_back/fh260/background/20210922/pngtree-abstract-nature-green-and-sunny-defocused-light-background-image_906725.png');
height: 400px;
width: 500px;
font-size: 3rem;
color: black;
}
</style>
</head>
<body>
<h2>Setting up the background image using the <i> background-image </i> property</h2>
<div class = "div-ele">
This is a div.
This is a div.
This is a div.
This is a div.
</div>
</body>
</html >
示例 2
在下面的示例中,我们使用“initial”作为背景图像值。在输出中,用户可以观察到它没有为 div 元素设置任何背景,因为初始背景没有。
<html>
<head>
<style>
.div-ele {
background-image: initial;
height: 300px;
width: 500px;
font-size: 2rem;
color: black;
border: 2px yellow solid;
}
</style>
</head>
<body>
<h3>Setting up the background image using the <i> background-image </i> property.</h3>
<div class = "div-ele"> Hi users, how are you? </div>
</body>
</html>
示例 3
在下面的示例中,我们将渐变和图像一起设置为背景。在输出中,用户可以观察到渐变是从上到下的,并且 div 元素的内容位于渐变上方。
<html>
<head>
<style>
.div-ele {
background-image: linear-gradient(rgba(255, 0, 0, 0.6), rgba(0, 0, 255, 0.6)), url('https://www.tutorialspoint.com/css/images/css-mini-logo.jpg');
height: 300px;
width: 500px;
font-size: 4rem;
color: black;
border: 2px yellow solid;
}
</style>
</head>
<body>
<h2>Setting up the background image using the <i> background-image </i> property.</h2>
<div class = "div-ele">
Welcome to TutorialPoint's website!
</div>
</body>
</html>
示例 4
在下面的示例中,我们将两个图像设置为 div 元素的背景。此外,我们为这两个元素设置了不同的背景位置。在输出中,用户可以观察到一张图像位于右下角,另一张图像位于左上角。
每当我们一起使用两个背景图像时,第一个图像就会出现在第二个图像的顶部。
<html>
<head>
<style>
div {
background-image: url("https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTbmvLkYgy28lI-iZWZpd3aAz0mi25dpUNXnU6OUE2V&s"), url("https://media.istockphoto.com/id/1090883040/vector/twinkle-little-star.jpg?s=612x612&w=0&k=20&c=Z5csKM3_ccD2MWqeWn6XfBoCqUeGf1IJHN09hJhCQEM=");
background-position: right bottom, left top;
background-repeat: no-repeat, repeat;
height: 500px;
width: 500px;
font-size: 2rem;
color: white;
}
</style>
</head>
<body>
<h2>Setting up the multiple background images using the <i> background-image </i> property.</h2>
<div>
This div contains 2 background images.
The first one is at the right bottom, and the second one is at the left top position.
</div>
</body>
</html>
用户在本教程中学习了如何使用“background-image”属性来设置图像的背景。用户还学会了将渐变设置为 HTML 元素的背景。用户还可以使用多个图像作为背景,并且当他们添加图像的 URL 作为值时,背景图像也会以创建堆栈的相同顺序出现。
以上就是哪个属性用于使用 CSS 设置元素的背景图像?的详细内容,更多请关注双恒网络其它相关文章!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
8. 精力有限,不少源码未能详细测试(解密),不能分辨部分源码是病毒还是误报,所以没有进行任何修改,大家使用前请进行甄别
9.本站默认解压密码为:www.sudo1.com
本站提供的一切软件、教程和内容信息仅限用于学习和研究目的。
不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。
本站信息来自网络收集整理,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑或手机中彻底删除上述内容。
如果您喜欢该程序和内容,请支持正版,购买注册,得到更好的正版服务。
我们非常重视版权问题,如有侵权请邮件与我们联系处理。敬请谅解!
云资源网 » 哪个属性用于使用 CSS 设置元素的背景图像?
常见问题FAQ
- 免费下载或者VIP会员专享资源能否直接商用?
- 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
- 提示下载完但解压或打开不了?
- 你们有qq群吗怎么加入?