搜索
首页web前端css教程哪个属性用于使用 CSS 设置元素的背景图像?

哪个属性用于使用 CSS 设置元素的背景图像?

在 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 id="Setting-up-the-background-image-using-the-i-background-image-i-property">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 id="Setting-up-the-background-image-using-the-i-background-image-i-property">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 id="Setting-up-the-background-image-using-the-i-background-image-i-property">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 id="Setting-up-the-multiple-background-images-using-the-i-background-image-i-property">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 设置元素的背景图像?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:tutorialspoint。如有侵权,请联系admin@php.cn删除
这么多颜色链接这么多颜色链接Apr 13, 2025 am 11:36 AM

最近有一系列有关颜色的工具,文章和资源。请允许我通过将它们四舍五之后关闭几个标签,以供您享受。

自动利润在Flexbox中的工作方式自动利润在Flexbox中的工作方式Apr 13, 2025 am 11:35 AM

罗宾以前已经介绍过这一点,但是我在过去的几周里听到了一些关于它的困惑,看到另一个人在解释它,我想

移动彩虹移动彩虹Apr 13, 2025 am 11:27 AM

我绝对喜欢三明治网站的设计。在许多美丽的功能中,这些标题是滚动时带有彩虹的下线。它不是

新年,新工作?让我们做一个网格驱动的简历!新年,新工作?让我们做一个网格驱动的简历!Apr 13, 2025 am 11:26 AM

许多流行的简历设计通过以网格形状铺设部分来充分利用可用的页面空间。让我们使用CSS网格创建一个布局

将用户摆脱过多习惯的一种方法将用户摆脱过多习惯的一种方法Apr 13, 2025 am 11:25 AM

页面重新加载是一回事。有时,当我们认为它没有响应或认为新内容可用时,我们会刷新页面。有时我们只是生气

域驱动的设计与React域驱动的设计与ReactApr 13, 2025 am 11:22 AM

关于如何在React世界中组织前端应用的指导很少。 (只需移动文件,直到“感觉正确”,大声笑)。真相

检测非活动用户检测非活动用户Apr 13, 2025 am 11:08 AM

大多数情况下,您并不真正在乎用户是否积极参与或暂时非活动。不活跃,意思,也许他们

Wufoo ZapierWufoo ZapierApr 13, 2025 am 11:02 AM

Wufoo一直在集成方面非常出色。他们与特定应用程序(例如广告系列显示器,MailChimp和Typekit)进行集成,但他们也

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
4 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器