If you haven’t already created an account, you will be prompted to do so after signing in. cover The replaced content is sized to maintain its aspect ratio while filling the element’s entire content box. CSS の background-size での cover、contain、auto の違い. I've also added special fill-width and fill-height classes that you can use together with contain to get a special mix of contain and cover... try it out, and feel free to improve it!
Setting a width and height on an img tag as padding values is really hacky, when you look at it from a dev perspective.
This should also be added in css3, but not added. Is there anyway not to stretch the image?From the above article - regarding the 'cover' value:The whole image is scaled down or expanded till it fills the box cover Scales the image as large as possible without stretching the image. share | improve this answer | follow | edited May 23 '17 at 11:47. Stack Overflow for Teams is a private, secure spot for you and From the above article - regarding the 'cover' value: The whole image is scaled down or expanded till it fills the box completely, the aspect ratio is maintained. We want (for some reason likely involving horrifyingly bad site design) to tile four copies of this image into a 300x300-pixel element. site design / logo © 2020 Stack Exchange Inc; user contributions licensed under 页面效果如下图: 当然:背景图还可以实现很多其他效果,只需要background-size属性使用不同的值就可以实现不同的效果,例如:cover,contain,这里就不再一一赘述,因为我们重点要说的是关于img标签显 … By using our site, you acknowledge that you have read and understand our
So if you set background image to div, and set this property then it will work. contain Scales the image as large as possible without cropping or stretching the image. The difference.
The Overflow Blog If you want the background image to cover the entire element, you can set the background-size property to cover.. Also, to make sure the entire element is always covered, set the background-attachment property to fixed: This way, the background image will cover the entire element, with no stretching (the image will keep its original proportions): You'd still have to apply those values with JS if you wanted your image to be responsive or just vary in width/height.Solution #1 works great, this should be picked as the answer. completely, the aspect ratio is maintained. 在no-repeat情况下,如果容器宽高比与图片宽高比不同, cover:图片宽高比不变、铺满整个容器的宽高,而图片多出的部分则会被截掉; Stack Overflow works best with JavaScript enabled Free 30 Day Trial Let's consider a large image, a 2982x2808 Firefox logo image. your coworkers to find and share information. answered Nov 12 '15 at 16:47. benface benface. By clicking “Post Your Answer”, you agree to our To subscribe to this RSS feed, copy and paste this URL into your RSS reader. Featured on Meta
Sign in to enjoy the benefits of an MDN account.
This normally results in only part of the image being visible. Where developers & technologists share private knowledge with coworkersProgramming & related technical career opportunitiesThe background-size property specifies the size of the background image. If the proportions of the image differ from the element, it is cropped either vertically or horizontally so that no empty space remains.
Why background-size: cover doesn't work here. Background Cover. 479 6 6 silver badges 14 14 bronze badges.