搜尋
首頁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刪除
我們如何標記Google字體並創建Goofonts.com我們如何標記Google字體並創建Goofonts.comApr 12, 2025 pm 12:02 PM

Goofonts是由開發人員和設計師丈夫簽名的附帶項目,它們都是版式的忠實擁護者。我們一直在標記Google

永恆的Web開發文章永恆的Web開發文章Apr 12, 2025 am 11:44 AM

Pavithra Kodmad向人們詢問了他們認為是關於網絡開發的一些最永恆的文章的建議

與部分元素的交易與部分元素的交易Apr 12, 2025 am 11:39 AM

同一天發表了兩篇文章:

使用JavaScript API練習GraphQl查詢使用JavaScript API練習GraphQl查詢Apr 12, 2025 am 11:33 AM

學習如何構建GraphQL API可能具有挑戰性。但是您可以學習如何在10分鐘內使用GraphQL API!碰巧的是,我得到了完美的

組件級CMS組件級CMSApr 12, 2025 am 11:09 AM

當一個組件生活在數據查詢居住在附近的數據查詢的環境中時,視覺組件和

將類型設置在圓上...帶偏移路徑將類型設置在圓上...帶偏移路徑Apr 12, 2025 am 11:00 AM

這裡是Yuanchuan的一些合法CSS騙局。有此CSS屬性偏移路徑。曾幾何時,它被稱為Motion-Path,然後被更名。我

'恢復”在CSS中有什麼作用?'恢復”在CSS中有什麼作用?Apr 12, 2025 am 10:59 AM

Miriam Suzanne在Mozilla開發人員的視頻中解釋了該主題。

現代戀人現代戀人Apr 12, 2025 am 10:58 AM

我喜歡這樣的東西。

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尊渡假赌尊渡假赌尊渡假赌

熱工具

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器