简体   繁体   English

为什么当我使用背景图像时图像尺寸会减小?

[英]why image size is reduce when I used background-image?

I am trying to display a image using background-image and img src tag.我正在尝试使用background-imageimg src标签显示图像。 I am confused when I am using same image using background-image it become smaller .but when I used as img src tag it look good why?当我使用background-image图像时,我感到困惑。但是当我用作img src 标签时,它看起来不错,为什么?

here is my code https://codesandbox.io/s/affectionate-cartwright-pw2mq?file=/src/styles.css这是我的代码https://codesandbox.io/s/affectionate-cartwright-pw2mq?file=/src/styles.css

<div class="curve-img-container">

jjjj
    </div>

        <img src="src/bg-curvy-desktop.svg" alt="">

my output is this我的 output 是这个

在此处输入图像描述

why there is a difference??in both case it look same?为什么有区别?在这两种情况下它看起来都一样?

The background image appears with respect to the height and width of the element you are setting it to.背景图像相对于您设置的元素的heightwidth出现。 When you use image tag initialy it loads with actual height and width of the image ( which is different comparing to your div ) so they appear different.当您最初使用图像标签时,它会加载图像的实际heightwidth (与您的 div 相比不同) ,因此它们看起来不同。

So in your case if you want both of them to look a like you need same height and width for img and div.因此,在您的情况下,如果您希望它们看起来像您需要相同的 img 和 div heightwidth

So for example you can do these if you want both of them to look the same.因此,例如,如果您希望它们看起来相同,则可以执行这些操作。

Note that object-fit: cover;object-position: center;注意object-fit: cover;object-position: center; is equivalent of background-size: cover;background-position: center;相当于background-size: cover;background-position: center; of css, Hope this answers your doubt css,希望这能回答你的疑问

And as a side note always use img tag as it better for SEO.作为旁注,请始终使用 img 标签,因为它更适合 SEO。

 img{ height: 50px; width: 50px; object-fit: cover; object-position: center; }.mydiv{ height: 50px; width: 50px; background-size: cover; background-position: center; }
 <div class="mydiv" style="background-image: url(https://image.shutterstock.com/image-photo/bright-spring-view-cameo-island-260nw-1048185397.jpg)"></div> <img src="https://image.shutterstock.com/image-photo/bright-spring-view-cameo-island-260nw-1048185397.jpg" alt="">

The background-image CSS property sets background images on an element so the size of that element decides the dimension of the image background-image CSS 属性在元素上设置背景图像,因此该元素的大小决定图像的尺寸

In your case, you have set the styles for div element:在您的情况下,您已经为div元素设置了 styles :

  • width: 100% - which will take 100% of the viewport width: 100% - 占视口的 100%
  • height: 200px - which will set both the element and the image height to 200px height: 200px - 将元素和图像高度都设置为 200px

While <img> element embeds an image into the document which means the browser will render the image as it is (without changing its dimension).<img>元素将图像嵌入到文档中,这意味着浏览器将按原样呈现图像(不改变其尺寸)。

In your case the dimension of the image is:在您的情况下,图像的尺寸是:

  • width: 1440px; height:449px;

You should use the css value background-repeat: no-repeat for prevent the background image from duplicating itself in the div.您应该使用 css 值background-repeat: no-repeat以防止背景图像在 div 中复制自身。

For controlling the background image size inside the div, read about the css property background-size : https://www.w3schools.com/cssref/css3_pr_background-size.asp要控制 div 内的背景图像大小,请阅读 css 属性background-sizehttps://www.w3schools.com/cssref/css3_pr_background-size.asp

in your code sample you have set the height for the container of the image background to 200px, so the image will take this height also as it is relative to what ever its container size...在您的代码示例中,您已将图像背景容器的高度设置为 200px,因此图像也将采用此高度,因为它相对于其容器大小...

Please note also that you should consider the background size as it is going to be auto by default, this will prevent the background from stretching and will keep the height/width percentage to prevent image being distorted.另请注意,您应该考虑背景大小,因为默认情况下它将是自动的,这将防止背景拉伸并保持高度/宽度百分比以防止图像失真。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM