惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

MongoDB | Blog
MongoDB | Blog
博客园 - 聂微东
Attack and Defense Labs
Attack and Defense Labs
WordPress大学
WordPress大学
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Spread Privacy
Spread Privacy
AI
AI
宝玉的分享
宝玉的分享
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
C
Cyber Attacks, Cyber Crime and Cyber Security
爱范儿
爱范儿
Help Net Security
Help Net Security
V
Visual Studio Blog
大猫的无限游戏
大猫的无限游戏
Forbes - Security
Forbes - Security
P
Privacy & Cybersecurity Law Blog
Project Zero
Project Zero
IT之家
IT之家
Hugging Face - Blog
Hugging Face - Blog
博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
有赞技术团队
有赞技术团队
T
Troy Hunt's Blog
美团技术团队
T
Threatpost
K
Kaspersky official blog
V
V2EX
Scott Helme
Scott Helme
Vercel News
Vercel News
T
The Blog of Author Tim Ferriss
T
Tailwind CSS Blog
V
Vulnerabilities – Threatpost
Last Week in AI
Last Week in AI
PCI Perspectives
PCI Perspectives
Google Online Security Blog
Google Online Security Blog
Apple Machine Learning Research
Apple Machine Learning Research
Engineering at Meta
Engineering at Meta
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
I
InfoQ
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
酷 壳 – CoolShell
酷 壳 – CoolShell
GbyAI
GbyAI
L
LINUX DO - 最新话题
T
The Exploit Database - CXSecurity.com
L
LangChain Blog
S
Security @ Cisco Blogs
The Last Watchdog
The Last Watchdog
H
Hacker News: Front Page
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
TaoSecurity Blog
TaoSecurity Blog

ImageKit.io Blog

Next.js Image Optimization with ImageKit Use Video as a Background in Your Next.js Project How to Fix Autoplay Video in Next.js How Durian Scaled a Visual-First Retail Experience to 350K Monthly Visitors Online How Matsmart accelerated image delivery across countries with ImageKit AI in Digital Asset Management: From Smart Workflows to Agentic Automation How Joseph Joseph unified and secured global video delivery with ImageKit How Modall powers fast, effortless media delivery across 40+ projects with ImageKit Digital Asset Management (DAM) Trends: 2026 Report How to add a poster image to Video.js player (and automate it) HLS streaming with Video.js + React Building the future of storytelling with fast, AI-powered video delivery How PushOwl delivers 100M+ image-rich notifications seamlessly with ImageKit How Homify delivers millions of interior design images seamlessly with ImageKit Better event discovery with lightning‑fast videos & images Adding video player in React Native Video player in Angular applications Crop and resize videos in React Next.js image and video upload React image and video upload React video optimization How we quadrupled our traffic to 625K monthly page views How Apollo 24|7 boosted performance & reduced costs with ImageKit Simplify your media workflows with ImageKit DAM integrations Extending Lighthouse for custom image and video optimization analysis Brand Asset Management: What is it? How does it work? WordPress Digital Asset Management Guide - Manage your WP media assets better Why Shopify retailers need a digital asset management solution DAM vs. SharePoint: Which is best for you? AI-powered Metadata and Tagging in Digital Asset Management How Hopscotch built India's largest online Kids' fashion brand with ImageKit Dropbox Vs. DAM: Which Is The Right Tool For Digital Asset Management Digital Asset Management for Photographers: A Complete Guide Why digital asset management for agencies is essential Helping both Top and Bottom Line: SaffronStays rapid, profitable growth with ImageKit How KreditBee simplified media experiences with ImageKit Google Drive alternatives for businesses (with fast-growing teams) Node.js image upload ImageKit: The Secret Ingredient in Swiggy’s Expansion Journey Streamlining the Design Approval Process: A Comprehensive Guide AV1 Codec - Complete guide for video application devs PHP image and video upload Angular image & video upload AV1 vs VP9: Which codec should you choose? Adding video player in Next.js React Video Player VP8 vs VP9 - In the context of online video delivery Exploring WebM vs MP4 7 Free Digital Asset Management Software that are not Open-Source Comparing 9 Top Digital Asset Management Tools in the Market What are Brand Standards and Why do they Matter? Boost Sales and Brand Appeal: Essential Tips for eCommerce Image Management Brand Recall: The Strategy to Create Unforgettable Brands How to upload files in HTML? Branding for Small Businesses (2025 Edition) Everything you need to know about VP9 codec Recent updates from ImageKit and what's next Best Ways to Write RFP For Digital Asset Management (+ with free RFP template) What is Brand Dilution? How to Avoid It? Explained with [Examples] The Importance of Brand Identity: Leveraging Digital Asset Management for Impact From Launch to Scale: How to Launch a Brand Campaign Digital Asset Management Requirements - What do You Need to Evaluate and How? Marketing Collateral Management: A Quick End-to-End Guide Video Content Management System: What Is It And How To Choose One? Dropbox vs. Google Drive vs. Onedrive: The Best Cloud Storage Solution How to Build Brand Trust: Get Started In 2025 Google Drive vs. Box: A Detailed Comparison How Digital Asset Management Solutions Help Protect Brand Equity A DAM Solution Can Safeguard Your Digital Intellectual Property - Here’s How WebP Vs. PNG: Which Image Format Should You Use and Why? How to Resize Images in Bootstrap Easily Progressive jpegs (PJPEG): the key to loading images faster on your website Dropbox vs. Google Drive: The Best Cloud Storage For Digital Assets Dropbox Pros & Cons In 2024: An In-Depth Analysis and Why A DAM Solution Stands Out Google Drive Vs OneDrive: The Better Storage Option For Digital Assets Manage your video assets better with video metadata Understanding DAM's Role in Strengthening Brand Identity Digital asset management strategy: What to know before creating one The Ultimate Guide To Marketing Agency Onboarding 6 Solutions To Simplify Large File Sharing Over The Web A Step-by-Step Breakdown of a Video Production Workflow 13 Digital Asset Management Use Cases You Should Know How to Conduct a Brand Audit and Manage Your Brand Assets Costly Consequences of Inconsistent Branding And How DAM Can Help Dynamic Asset Transformation: What It Is, Why You Need It, and How ImageKit Can Help Everything You Need to Know About HTML Video Autoplay How To Select Your DAM Vendor: A Complete Guide How to Boost User Experience with Smart Digital Asset Management React Image Optimization: A Guide for Web Developers Why Should DAM Be A Part Of Your MarTech Stack? Unleashing the Power of Content Repurposing with ImageKit MKV vs MP4: Which Video File Format Is Better for Your Needs? Digital Asset Management For Ecommerce: A Complete Guide How an Image Tagging Software can Transform Your Image Search How to Manage Your Content Lifecycle Effectively M4V vs MP4: Which Video Format Should You Use and Why? Why Every Business Needs An Image Management System All The Questions To Ask During A Dam Demo Which is the Best Image Format for Your Website? Uploading Multiple Files Using JavaScript: A Comprehensive Guide
How To Scale and Crop Images with CSS
Rahul Nanwani · 2023-08-07 · via ImageKit.io Blog

A simple image when rightly used can speak louder about a business and its offerings to the target audience than words. When choosing the image to best explain a solution, one will likely encounter moments when it is necessary to crop particular portions of an image to better explain the details.

Image cropping is of great importance as it aids in giving focus to specific elements in an image and helps improve the visual impact and composition of the image.

In this article, we will look at cropping images with CSS using code samples. Finally, we will also see how using ImageKit can help you crop images easily and at scale without designer or developer dependencies.

To understand how to crop an image, you need a basic understanding of HTML and CSS.

The Different Methods of Image Cropping with CSS

There are several ways to crop images with CSS. This article sticks to explaining the 8 core ways you can crop images with CSS.

CLIP-PATH

CSS clip path is a property that allows users to define a clipping region to crop or mask an image. With this feature, users can utilize different functions and values to define the clipping path. Here are some examples:

.image1 {
  width: 200px;
  height: 200px;
  clip-path: circle(50% at center);
}

The code above sets the width and height of the image to 200px. The value attached to the clip-path property will create a circular clipping path for the image that covers 50% of its size and is centered within the element. This is the result below.

Values for clip-path can vary thus forming several shapes. Other values include

  1. Ellipse Shape
.image2 {
  width: 200px;
  height: 200px;
  clip-path: ellipse(50% 25% at center);
}

This code will create an elliptical clip-path that covers 50% of the image's width and 25% of its height, centered within the element.

2. Inset Image

.image3 {
  width: 200px;
  height: 200px;
  clip-path: inset(10px 20px 30px 40px);
}

This code will create an inset clip-path, cropping the image by 10 pixels from the top, 20 pixels from the right, 30 pixels from the bottom, and 40 pixels from the left.

3. Polygon Shape

.image4 {
  width: 200px;
  height: 200px;
  clip-path: polygon(0 0, 100% 0, 100% 50%, 50% 100%, 0 50%);
}

This code will create a custom polygonal clip-path that defines a five-sided shape.

To view the complete code, check out this codepen.

OVERFLOW: HIDDEN

The overflow: hidden property is used to hide any part of an image that exceeds the boundaries of the container. To use this property, there is a need to wrap the image within a container element and apply the overflow: hidden property to the container. This will ensure the restriction of the visible area of the image to fit the dimension specified for the container. The code below illustrates how it works.

.image-container {
  width: 300px;
  height: 200px;
  overflow: hidden;
}

.image {
  width: 100%;
  height: auto;
}

The code above gives the result below

The .image-container is given a fixed width and height of 300px and 200px, which represents the desired size of the cropped image. An overflow: hidden property was applied to the container to hide any content that exceeds the specified size.

The .image class handles the image to be displayed and ensures it fills the desired size of the container by setting the width to 100% and also maintains the aspect ratio by setting the height: auto.

To view the full code, check out this code pen here

BACKGROUND-SIZE, BACKGROUND-POSITION, AND BACKGROUND-CLIP

These three properties, when applied can crop any image of choice.

The background-image property in CSS is used to set the background image of an element and the background-size property is used to control the size of the background image.

  1. Background Image with cover size
.image-container {
  width: 300px;
  height: 200px;
  background-image: url('https://ik.imagekit.io/demo/tr:h-300/sample_image.jpg');
  background-size: cover;
  background-repeat: no-repeat;
}

Enabling the background-size: cover makes the background image scaled proportionally to cover the entire container and crop any part that exceeds the container boundaries.

2. Background Image with Contain size

.image-container {
  width: 300px;
  height: 200px;
  background-image: url('https://ik.imagekit.io/demo/tr:h-300/sample_image.jpg');
  background-size: contain;
  background-repeat: no-repeat;
}

Enabling the background-size: contain makes the background image scaled proportionally to fit within the container without cropping it. This value ensures that the image is visible enough and its aspect ratio is preserved. For this property, the entire image will be visible, and it may not cover the entire container. Any space within the container will show the background or content behind the image.

c. Background Image with Custom Size

.image-container {
  width: 300px;
  height: 200px;
  background-image: url('https://ik.imagekit.io/demo/tr:h-300/sample_image.jpg');
  background-size: 50% 70%;
  background-repeat: no-repeat;
}

The background-size is set to a custom size in this code example. The value 50% and 70% represents the width and height of the background image as percentages of the container's width and height, respectively. This custom value specifies the portion of the image to be displayed within the container.

To view the complete code, check it out on Code Pen.

BACKGROUND POSITION

The background-position property enables a user to position the image within its container.

  1. Background-position: Center
.image-position1 {
  width: 300px;
  height: 200px;
  background-image: url('https://ik.imagekit.io/demo/tr:h-300/sample_image.jpg');
  background-position: center;
  background-repeat: no-repeat;
}

With the background-position: center, the background image is centered within the element.

Other values normally assigned to the background-position property include top-right, bottom left, or any custom position, etc.

To view the complete code, check it out on code pen.

BACKGROUND CLIP

The CSS background-clip property is used to control how the background image is clipped or displayed relative to its borders. For instance:

.image1 {
  width: 300px;
  height: 200px;
  background-image: url('https://ik.imagekit.io/demo/tr:h-300/sample_image.jpg');
  background-size: cover;
  background-clip: padding-box;
}

This code takes the width and height of an image1 as 300px and 200px respectively, and gives the background clip a value for padding-box. The padding box value crops the background image within the padding area and hides the image outside the element’s content area.

Other possible values for the background-clip element are border-box and content-box.

To view the complete code, check out this codepen.

OBJECT FIT AND POSITION

The CSS object-fit property enables the control of an image within its container. It allows for the specification of how an image should be resized. On the other hand, the Object-position property allows for the control of the position of an image within its container.

a. Object fit: Cover

.image {
  width: 200px;
  height: 200px;
  object-fit: cover;
}

The object-fit: cover property ensures that the image covers the entire space of the element while also preserving its aspect ratio. Any excess portion of the image will be cropped to fit within the container.

b. Object-fit: Contain

.image {
  width: 200px;
  height: 200px;
  object-fit: contain;
}

With the object-fit: contain property, the image is scaled to fit within the container while preserving its aspect ratio. For this property, the entire image will be visible, and it may not cover the entire container. Any space within the container will show the background or content behind the image.

c. Object-position: top right

.image {
  width: 200px;
  height: 200px;
  object-fit: cover;
  object-position: top right;
}

The object-position: top right property specifies that the image should be positioned at the top right corner of the container while the object-fit: cover in the code property ensures that the image fills the container while maintaining its aspect ratio.

d. Object-position: center bottom

.image {
  width: 200px;
  height: 200px;
  object-fit: cover;
  object-position: center bottom;
}

The object-position: center bottom, positions the image at the center horizontally and at the bottom vertically within the container. This creates a cropping effect that removes the top and sides of the image.
To view the complete code, check it out on code pen.

TRANSFORM: SCALE

The CSS transform: scale property allows the user to scale, resize, and images.

.image {
  width: 300px;
  height: 200px;
  position: relative;
}

.image img {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) scale(0.8);
}

In this code snippet, the position: relative property on the container is used to establish a positioning context.

Inside the .image element, the position of the img element is set to absolute, its top: 50% and its left: 50% to position it in the center of the container. The transform: translate(-50%, -50%) scale(0.8) property scales the image by a factor of 0.8, making it larger in both width and height. The translate(-50%, -50%)  part ensures the image remains centered within the container after scaling.

To view the complete code, check it out on code pen.

BORDER RADIUS

The CSS border-radius property can be used to crop images. This is illustrated below:

.image1 {
  width: 300px;
  height: 200px;
  border-radius: 50%;
}

The dimensions of the .image1 element are set to 300px width and 200px height with a border-radius: 50%. This creates a circular shape for the element and results in a cropped image with rounded corners. The border-radius property can be used with different values to create other shapes and cropping effects. For instance:

.image2 {
  width: 300px;
  height: 200px;
  border-radius: 10px 50% 30px 20px;
}

The border-radius:10px 50% 30px 20px creates an elliptical-shaped image with varying radii on each corner.

To view the complete code, check out this code pen.

BACKGROUND CLIP

The CSS background-clip property is used to control how the background image is clipped or displayed relative to its borders. For instance:

.image {
  width: 300px;
  height: 200px;
  background-image: url('https://ik.imagekit.io/demo/tr:h-300/sample_image.jpg');
   background-clip: padding-box;
}

This code takes the width and height of an image as 300px and 200px respectively and gives the background clip a value for padding-box. The padding box value crops the background image within the padding area and hides the image outside the element’s content area.

Other possible values for the background-clip element are border-box and content-box.

To view the complete code, check out this codepen.

DISADVANTAGE OF CSS-BASED CROPPING

One thing to note is that with CSS-based cropping, you are still downloading the full image to the browser, and then cropping it which means there is a higher bandwidth usage (or data transfer). What if your image delivery service itself could do this in real-time on the server, so that instead of downloading a larger un-cropped image you could download a smaller cropped version directly?

With this method, image cropping will not be difficult, and 90% time you just need basic cropping (rectangular) that focuses on the right part of the image and you need to do this across platforms. This is what ImageKit helps with.

Effortlessly Crop Your Images In Real-Time with ImageKit

ImageKit.io is an image management service for online businesses that provide intelligent real-time image optimization, resizing, cropping, and fast CDN delivery.

It is an image transformation tool with 50+ transformations that help companies and developers quickly implement image optimization and real-time resizing in their web applications without worrying about scale.

With ImageKit, it is relatively easy to crop or transform an image to suit the required business need. It does this through its vast cropping features. These cropping features include:

Maintain ratio crop strategy - (c-maintain_ratio)

This is the default crop strategy. If nothing is specified in the URL, this strategy gets applied automatically. In this strategy, the aspect ratio is preserved and this is accomplished by resizing the image to the requested dimension and in the process cropping parts from the original image.

The code below illustrates how it works:

Pad resize crop strategy - (cm-pad_resize)

The performs the task of preserving the aspect ratio of an image without cropping by adding padding around the output image to make it match the exact dimensions specified.

A perfect example is the URL below:

https://ik.imagekit.io/marvelkalu/new_sample_image.png?tr=w-100,h-200,cm-pad_resize,bg-1C2125

This image is given a width of 300px and a height of 200px. The cm-pad_resize strategy is used to add a pad around the image and give the pad a black color.

Forced crop strategy - (c-force)

This forcefully squeezes the original image to get it to fit completely within the specified output dimensions. There is no cropping involved and it does not preserve the aspect ratio of the image.

To illustrate this strategy, let us examine this URL:

https://ik.imagekit.io/marvelkalu/new_sample_image.png?tr=w-300,h-100,c-force

The strategy forces the image to fit into the specified dimensions

Max-size cropping strategy - (c-at_max)

This strategy ensures the whole image content is preserved without cropping, and the aspect ratio is preserved. To illustrate this strategy, let us examine this URL:

https://ik.imagekit.io/marvelkalu/new_sample_image.png?tr=w-300,h-100,c-at_max

Max-size-enlarge cropping strategy - (c-at\max\enlarge)

This strategy is similar to the max-size cropping strategy with the addition that it also allows an image to enlarge more than its original dimensions.

The URL below illustrates this strategy:

https://ik.imagekit.io/marvelkalu/new_sample_image.png?tr=w-300,h-200,c-at_max_enlarge

Min-size cropping strategy - (c-at_least)

This strategy is similar to the max-size cropping strategy, however, unlike the max-size strategy, the image is equal to or larger than the requested dimensions.  The URL below illustrates this strategy:

https://ik.imagekit.io/marvelkalu/new_sample_image.png?tr=w-300,h-200,c-at_least

In this strategy, the aspect ratio is preserved and instead of trying to resize the image, a region of the requested dimension from the original image is extracted. The URL below illustrates this strategy:

https://ik.imagekit.io/marvelkalu/new_sample_image.png?tr=w-300,h-200,cm-extract

Imagekit also has a smart auto-crop feature that uses AI to intelligently determines the most important part of the image and keeps it in focus while cropping the image. It is one great feature that completely stands out from CSS and is perfect for generating thumbnails that are centered on the image’s subject.

Cropping images on a large scale can be quite a difficult task. When cropping images, it is important to preserve the aspect ratio of the image. An image CDN like Imagekit.io can handle all your cropping needs and give the exact quality required.

Get started with a free trial. Setting up your account only takes a few minutes and our plan is free until you are ready to upgrade.