目录
语法
示例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团队完成了完成,我知道这是一项巨大的努力,而且很长时间。所有新文档也是如此。

使用Redwood.js和Fauna构建以太坊应用 使用Redwood.js和Fauna构建以太坊应用 Mar 28, 2025 am 09:18 AM

随着最近比特币价格超过20k美元的攀升,最近打破了3万美元,我认为值得深入研究创建以太坊

您可以从浏览器获得有效的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)网站上的这一点,当您滚动时,彼此之间的卡片堆放集。

比较浏览器的响应式设计 比较浏览器的响应式设计 Apr 02, 2025 pm 06:25 PM

这些桌面应用程序中有许多目标是同时在不同的维度上显示您的网站。因此,例如,您可以写作

在WordPress块编辑器中使用Markdown和本地化 在WordPress块编辑器中使用Markdown和本地化 Apr 02, 2025 am 04:27 AM

如果我们需要直接在WordPress编辑器中向用户显示文档,那么最佳方法是什么?

为什么Flex布局中的紫色斜线区域会被误认为是'溢出空间”? 为什么Flex布局中的紫色斜线区域会被误认为是'溢出空间”? Apr 05, 2025 pm 05:51 PM

关于Flex布局中紫色斜线区域的疑问在使用Flex布局时,你可能会遇到一些令人困惑的现象,比如在开发者工具(d...

See all articles