Home >Web Front-end >CSS Tutorial >How Can I Make a Background Image Fit a Div Without Cropping?

How Can I Make a Background Image Fit a Div Without Cropping?

Barbara Streisand
Barbara StreisandOriginal
2024-12-23 01:52:32605browse

How Can I Make a Background Image Fit a Div Without Cropping?

Fit Background Image to Div

Issue:
A background image is being cut off within a div. The desired outcome is to display the entire image without cropping.

Solution:

To retain the background image's integrity within the div, utilize the background-size property. This property dictates how the image scales relative to its container.

By employing the following values, you can achieve your intended result:

  • Contain: Scales the image to fit within div, maintaining its aspect ratio.
  • Cover: Scales the image to cover the entire div, potentially cropping parts of the image.

Code Examples:

Contain:

background-size: contain;

Cover:

background-size: cover;

Note: The background-size property is supported by most modern browsers.

The above is the detailed content of How Can I Make a Background Image Fit a Div Without Cropping?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn