目錄
文法
範例4
Using the background-size CSS property to set the size of the background image
Example 4
Using the background-size CSS property to set the size of the background image
Using the background-size: contain CSS property to set the size of the background image
Using the background-size: length auto CSS property to set the size of the background image
首頁 web前端 css教學 使用CSS設定背景影像的大小?

使用CSS設定背景影像的大小?

Sep 08, 2023 am 09:05 AM

使用CSS設定背景影像的大小?

使用CSS,我們可以使用‘background-image’屬性為HTML元素設定背景圖片。此外,在新增背景圖像後,還需要設定其尺寸。

我們可以使用CSS的「background-size」屬性來設定背景圖片的大小。

文法

使用者可以按照下面的語法使用CSS來設定背景圖像的大小。

background-size: width length | width | contain | inherit | cover | initial
登入後複製

我們可以使用上述值來設定背景圖片的大小。在這裡,我們已經解釋了所有的值。

  • 寬度長度 - 設定背景圖像的寬度和高度。使用者可以使用像素、百分比或 rem 作為寬度和長度值。

  • 寬度 - 它只設定圖像的寬度並設定「自動」高度。

  • Contain − 用於設定HTML元素的背景圖片,而不會減少背景圖片的尺寸。

  • 繼承 − 它從父元素的背景圖片中繼承背景圖片大小。

  • 封面 − 它以一種方式設定背景圖片的尺寸,使其能夠適應HTML元素。

範例 1

在下面的範例中,我們為HTML div元素設定了背景圖像。 div元素的尺寸為500 X 300。使用background-size屬性,我們將背景圖像設定為200 X 200的尺寸。

在輸出中,使用者可以觀察到背景圖像的大小小於div元素。此外,我們使用了「background-repeat: no-repeat」CSS屬性來避免重複。

<html>
<head>
   <style>
      .div {
         background-image: url("https://www.tutorialspoint.com/css/images/css-mini-logo.jpg");
         background-size: 200px 200px;
         width: 500px;
         height: 300px;
         border: 1px solid blue;
         background-repeat: no-repeat;
      }
   </style>
</head>
<body>
   <h3 id="Using-the-background-size-CSS-property-to-set-the-size-of-the-background-image">Using the background-size CSS property to set the size of the background image</h3>
   <div class = "div">
      This is a content of the div.
   </div>
</body>
</html>
登入後複製

Example 2

的翻譯為:

範例2

在下面的範例中,我們使用「background-size: cover」CSS 屬性來設定背景圖片的尺寸。

在輸出中,我們可以觀察到背景圖像適合 div 元素。然而,當我們將background-size設定為cover時,一些影像邊緣被切掉了。

<html>
<head>
   <style>
      .div {
         background-image: url("https://i.pinimg.com/736x/91/aa/88/91aa882cdcb4632063535e86c829d3ba.jpg");
         background-size: cover;
         width: 500px;
         height: 500px;
         border: 2px solid green;
      }
   </style>
</head>
<body>
   <h3 id="Using-the-i-background-size-i-CSS-property-to-set-the-size-of-the-background-image">Using the <i> background-size </i> CSS property to set the size of the background image</h3>
   <div class = "div">
      This is a content of the div.
   </div>
</body>
</html>
登入後複製

Example 3

的中文翻譯為:

範例 3

在下面的範例中,我們使用「background-size: contian」CSS 屬性來設定背景圖片大小。

在輸出中,我們可以看到它設定了背景影像,而不會抑制影像的尺寸。

<html>
<head>
   <style>
      .div {
         background-image: url("https://cdn.pixabay.com/photo/2016/06/02/02/33/triangles-1430105__480.png");
         background-size: contain;
         width: 500px;
         height: 500px;
         border: 2px solid green;
         font-size: 3rem;
      }
   </style>
</head>
<body>
   <h3 id="Using-the-i-background-size-contain-i-CSS-property-to-set-the-size-of-the-background-image">Using the <i> background-size: contain </i> CSS property to set the size of the background image</h3>
   <div class = "div">
      Hello! How are you?
   </div>
</body>
</html>
登入後複製

Example 4

的翻譯為:

範例4

在下面的範例中,我們只給了'background-size'屬性的寬度值。每當我們只使用一個值作為'background-size'屬性時,它會將高度設為自動,使用者可以在輸出中看到這一點。

<html>
<head>
   <style>
      .div {
         background-image: url("https://images.pexels.com/photos/235985/pexels-photo-235985.jpeg?cs=srgb&dl=pexels-pixabay-235985.jpg&fm=jpg");
         background-size: 20rem;
         width: 500px;
         height: 500px;
         border: 2px solid green;
         font-size: 3rem;
      }
   </style>
</head>
<body>
   <h3 id="Using-the-i-background-size-length-auto-i-CSS-property-to-set-the-size-of-the-background-image">Using the <i> background-size: length auto </i> CSS property to set the size of the background image</h3>
   <div class = "div">
      Hello! How are you?
   </div>
</body>
</html>
登入後複製

使用者學會了使用 CSS 設定背景圖片的大小。我們可以設定自訂尺寸來設定背景圖片。此外,我們可以將背景尺寸的寬度和高度設定為“auto”值。如果使用者希望使用背景覆寫整個 HTML 元素,則應使用「cover」作為背景大小屬性的值。

以上是使用CSS設定背景影像的大小?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

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

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

VUE 3 VUE 3 Apr 02, 2025 pm 06:32 PM

它的出局!恭喜Vue團隊完成了完成,我知道這是一項巨大的努力,而且很長時間。所有新文檔也是如此。

您可以從瀏覽器獲得有效的CSS屬性值嗎? 您可以從瀏覽器獲得有效的CSS屬性值嗎? Apr 02, 2025 pm 06:17 PM

我有人寫了這個非常合法的問題。 Lea只是在博客上介紹瞭如何從瀏覽器中獲得有效的CSS屬性。那樣的是這樣。

在CI/CD上有點 在CI/CD上有點 Apr 02, 2025 pm 06:21 PM

我說的“網站”比“移動應用程序”更合適,但我喜歡Max Lynch的框架:

帶有粘性定位的堆疊卡和一點點的雜物 帶有粘性定位的堆疊卡和一點點的雜物 Apr 03, 2025 am 10:30 AM

前幾天,我發現了科里·金尼文(Corey Ginnivan)網站上的這一點,當您滾動時,彼此之間的卡片堆放集。

在WordPress塊編輯器中使用Markdown和本地化 在WordPress塊編輯器中使用Markdown和本地化 Apr 02, 2025 am 04:27 AM

如果我們需要直接在WordPress編輯器中向用戶顯示文檔,那麼最佳方法是什麼?

比較瀏覽器的響應式設計 比較瀏覽器的響應式設計 Apr 02, 2025 pm 06:25 PM

這些桌面應用程序中有許多目標是同時在不同的維度上顯示您的網站。因此,例如,您可以寫作

如何將CSS網格用於粘頭和頁腳 如何將CSS網格用於粘頭和頁腳 Apr 02, 2025 pm 06:29 PM

CSS網格是一系列屬性的集合,旨在使佈局比以往任何時候都容易。像任何東西一樣,那裡有一點學習曲線,但是網格是

Google字體可變字體 Google字體可變字體 Apr 09, 2025 am 10:42 AM

我看到Google字體推出了新設計(Tweet)。與上一次大型重新設計相比,這感覺更加迭代。我幾乎無法分辨出區別

See all articles