Home >Web Front-end >CSS Tutorial >How to Auto-Crop and Center an Arbitrary Image within a Specified Square?

How to Auto-Crop and Center an Arbitrary Image within a Specified Square?

Susan Sarandon
Susan SarandonOriginal
2024-12-04 21:33:14358browse

How to Auto-Crop and Center an Arbitrary Image within a Specified Square?

Auto-Cropping and Centering an Arbitrary Image

Given an image of unknown dimensions, the task of cropping a square from its center and displaying it within a specified square requires a dynamic solution.

Using Background Images

One approach involves using a background image positioned centrally within an element sized to the desired cropped dimensions.

.center-cropped {
  width: 100px;
  height: 100px;
  background-position: center center;
  background-repeat: no-repeat;
}

<div>

This method leverages CSS's ability to automatically scale and position the background image, ensuring that it remains centered and cropped within the specified dimensions, regardless of the original image size.

The above is the detailed content of How to Auto-Crop and Center an Arbitrary Image within a Specified Square?. 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