Get the App
SLTechnology News&Howtos  ›  Development  › 

How to stretch and scale an image in the background with CSS

Shulou Source: shulou.com Published: 2022-06-01 12:32:50 10月03日 Update

This article is about how to use CSS to stretch and zoom an image in the background. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

You can use only CSS to stretch and scale the image in the background using CSSbackground-size: cover;. In this way, the image is scaled as large as possible so that the background area is completely covered by the background image while retaining its inherent aspect ratio.

However, if the width or height of the adjusted background image is too large relative to the element, part of the image may not be visible (vertically or horizontally).

Let's try the following example to understand the basic operation of this property:

Try this code for example »

Stretch and Scale Background-image with CSS

.box {

Width: 300px

Height: 300px

Background: url ("images/avatar.svg") no-repeat

Background-size: cover

Border: 6px solid # 333

Margin: 20px

}

Thank you for reading! On "how to use CSS to stretch and zoom images in the background" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, you can share it out for more people to see it!

Tags: Image background content more article good practical code element region principle at the same time background width as much as possible attributes article scale horizontal see Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei NVidia Microsoft Apple Docker