Home >Web Front-end >CSS Tutorial >How to Auto-Crop and Center an Arbitrary Image within a Specified Square?
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!