How to Scale and Crop Images to a Fixed 80×80 Box with CSS

This guide explains how to uniformly scale backend‑provided images of varying dimensions to fit an 80×80 container by proportionally resizing based on the shorter side and then cropping the central area, with a simple CSS solution using background‑size and background‑position, including a cover shortcut.

Tencent IMWeb Frontend Team
Tencent IMWeb Frontend Team
Tencent IMWeb Frontend Team
How to Scale and Crop Images to a Fixed 80×80 Box with CSS

Practical Scenario

We need to display images from the backend inside an 80×80 container. Because the original images have inconsistent sizes, we first proportionally scale them based on the shorter side and then crop the central 80×80 region.

Example: a 600×370 image. The shorter side is 370, so we scale it by 80/370, resulting in a 130×80 image, then center‑crop the middle 80×80 area.

Code Implementation

1. Create an 80×80 container and set the image as its background.

2. Scale the background using background-size. If the image’s height is the shorter side, use background-size:auto 80px; if the width is shorter, use background-size:80px auto. The CSS background-size:cover handles both cases automatically.

3. Center the background with background-position:center center.

Final CSS example:

.img-box{
  width:80px;
  height:80px;
  background-image:url('your-image.jpg');
  background-size:cover;
  background-position:center center;
}
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

CSSResponsive DesigncropImage Scaling
Tencent IMWeb Frontend Team
Written by

Tencent IMWeb Frontend Team

IMWeb Frontend Community gathering frontend development enthusiasts. Follow us for refined live courses by top experts, cutting‑edge technical posts, and to sharpen your frontend skills.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.