2023使用CSS设置背景图像的大小?

 所属分类:web前端开发

 浏览:41次-  评论: 0次-  更新时间:2023-09-22
描述:更多教程资料进入php教程获得。 使用CSS,我们可以使用‘background-i...
更多教程资料进入php教程获得。

使用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://img.zzsucai.com/202309/22/E2Hwk882848122532.jpg");
         background-size: 200px 200px;
         width: 500px;
         height: 300px;
         border: 1px solid blue;
         background-repeat: no-repeat;
      }
   </style>
</head>
<body>
   <h3>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>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://img.zzsucai.com/202309/22/v6ZJA206507122655.png");
         background-size: contain;
         width: 500px;
         height: 500px;
         border: 2px solid green;
         font-size: 3rem;
      }
   </style>
</head>
<body>
   <h3>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://img.zzsucai.com/202309/22/CtT8E498325122657.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>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”作为背景大小属性的值。

 标签:
积分说明:注册即送10金币,每日签到可获得更多金币,成为VIP会员可免金币下载! 充值积分充值会员更多说明»

讨论这个素材(0)回答他人问题或分享使用心得奖励金币

〒_〒 居然一个评论都没有……

表情  文明上网,理性发言!