公告:魔扣目录网为广大站长提供免费收录网站服务,提交前请做好本站友链:【 网站目录:https://www.morko.net 】, 免友链快审服务(50元/站),

点击这里在线咨询客服
新站提交
  • 网站:51998
  • 待审:31
  • 小程序:12
  • 文章:1030137
  • 会员:747

使用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>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://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>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>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设置背景图像的大小?的详细内容,更多请关注www.92cms.cn其它相关文章!

分享到:
标签:CSS 图像 大小 背景 设置
用户无头像

网友整理

注册时间:

网站:5 个   小程序:0 个  文章:12 篇

  • 51998

    网站

  • 12

    小程序

  • 1030137

    文章

  • 747

    会员

赶快注册账号,推广您的网站吧!
最新入驻小程序

数独大挑战2018-06-03

数独一种数学游戏,玩家需要根据9

答题星2018-06-03

您可以通过答题星轻松地创建试卷

全阶人生考试2018-06-03

各种考试题,题库,初中,高中,大学四六

运动步数有氧达人2018-06-03

记录运动步数,积累氧气值。还可偷

每日养生app2018-06-03

每日养生,天天健康

体育训练成绩评定2018-06-03

通用课目体育训练成绩评定