How To Stretch And Scale An Image In The Background With CSS
Có thể bạn quan tâm
Topic: HTML / CSSPrev|Next
Answer: Use the CSS background-size Property
You can use the CSS background-size: cover; to stretch and scale an image in the background with CSS only. This scales the image as large as possible in such a way that the background area is completely covered by the background image, while preserving its intrinsic aspect ratio.
However, part of the image may not be visible (cropped either vertically or horizontally) if the width or height of the resized background image is too large from the element.
Let's try out the following example to understand how this property basically works:
Example
Try this code » <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Stretch and Scale Background-image with CSS</title> <style> .box{ width: 300px; height: 300px; background: url("images/avatar.svg") no-repeat; background-size: cover; border: 6px solid #333; margin: 20px; } </style> </head> <body> <div class="box"></div> </body> </html>If it doesn't work for you then you can try other values like, background-size: contain;, background-size: 100%;, background-size: 100vw 100vh;, etc.
See the reference on CSS3 background-size property for more details.
Related FAQ
Here are some more FAQ related to this topic:
- How to position text over an image using CSS
- How to apply multiple background images to an element using CSS
- How to transform image size on mouse hover without affecting the layout in CSS
Từ khóa » Html Image Stretch To Fit
-
CSS Object-fit Property - W3Schools
-
Html - How Do You Stretch An Image To Fill A
While Keeping The ...How To Resize An Image In HTML?
How To Auto-Resize The Image To Fit An HTML Container - W3docs
HTML Stretch Background Image
Make Image Stretch To Fit Div Code Example
How To Stretch A Background Image To Fit A Web Page - ThoughtCo
Object-fit | CSS-Tricks
How To Auto-resize An Image To Fit Into A DIV Container Using CSS
How To Fill A Box With An Image Without Distorting It - MDN Web Docs
How To Auto-resize An Image To Fit A Div Container Using CSS?
Object-fit - CSS: Cascading Style Sheets - MDN Web Docs - Mozilla
How To Scale And Crop Images With CSS Object-fit | DigitalOcean
How To Make An Image Stretch Full Width Css?
Copyright © 2022 | Thiết Kế Truyền Hình Cáp Sông Thu