Table of contents
1.
Introduction
2.
CSS Crop Image
3.
How to Crop Image in CSS?
4.
CSS Techniques for Image Cropping
4.1.
CSS Techniques for Image Cropping Example
4.2.
HTML CSS
5.
Output
5.1.
Using Clip Property
5.2.
Using object-fit Property
5.3.
Using Negative Margins
5.4.
Using CSS Grid
6.
Frequently Asked Questions
6.1.
How do I crop an image instead of stretch in CSS?
6.2.
Can you crop background image in CSS?
6.3.
How to reduce image in CSS?
7.
Conclusion
Last Updated: Jun 21, 2024
Easy

CSS Crop Image

Author Kanak Rana
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Images play a pivotal role in web design, contributing to the visual appeal and user experience of a website. Often, you may find yourself needing to display a specific part of an image or crop it to fit a certain layout. This is where CSS image cropping comes into play

CSS Crop Image

In this article, we'll delve into the world of CSS image cropping, exploring techniques, use cases, and best practices to achieve precise and visually pleasing results.

CSS Crop Image

Sometimes, you might need to focus on a specific part of an image or adjust its dimensions to harmonize with your design.

CSS image cropping offers a versatile solution to tailor images according to your design's requirements. Using a combination of CSS properties and techniques, you can effectively crop, resize, and present images to enhance your website's visual appeal.

How to Crop Image in CSS?

Image cropping is a common requirement for most websites. While many platforms employ languages like PHP or Node for cropping images, let's focus on two CSS methods that rely less on programming languages. Sometimes, rather than genuinely cropping an image, we may only need to conceal a specific area.

In essence, MDN Web Docs serve as an invaluable resource for comprehending CSS advancements, and we can explore image cropping techniques in CSS without becoming overly entangled in intricate programming languages.

CSS Techniques for Image Cropping

There are multiple properties in which we can crop the images that are given below with their subjective output.

CSS Techniques for Image Cropping Example

  • HTML CSS

HTML CSS

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<style>

.image-container {

   display: inline-block;

   margin: 10px;

   border:1px solid black;

   overflow: hidden;

 }



.image-container img,

.image-container .image {

   width: 200px;

   height: 200px;

 }



/* Using clip property */

.clip-image img {

   clip: rect(0px, 150px, 200px, 50px);

 }



/* Using object-fit property */

.object-fit-image img {

   object-fit: cover;

 }



/* Using negative margins */

.negative-margin-image img {

   margin-left: -50px;

   margin-top: -50px;

 }



/* Using CSS Grid */

.grid-image {

   display: grid;

   grid-template-columns: 1fr 1fr;

 }

.grid-image img {

   max-width: 100%;

   max-height: 100%;

 }

</style>

</head>

<body>

<div class="image-container clip-image">

   <img src="https://files.codingninjas.in/438375-17407.webp" alt="Cropped Image using clip">

</div>



<div class="image-container object-fit-image">

   <img src="https://files.codingninjas.in/438375-17407.webp" alt="Cropped Image using object-fit">

</div>



<div class="image-container negative-margin-image">

   <img src="https://files.codingninjas.in/438375-17407.webp" alt="Cropped Image using negative margins">

</div>



<div class="image-container grid-image">

   <img src="https://files.codingninjas.in/438375-17407.webp" alt="Cropped Image using CSS Grid">

</div>

</body>

</html>

Output

Following are the outputs of different kinds of techniques for image cropping:

Using Clip Property

The clip property is a straightforward way to crop images using CSS. It allows you to define a rectangular region to display, hiding any content outside that region.

Cropped Image using clip

Using object-fit Property

The object-fit property is useful for cropping images within HTML <img> elements. It defines how the image should fit within its container, enabling options like cover, contain, and more.

Cropped Image using object-fit

Using Negative Margins

Negative margins can be used to adjust image positions within containers, revealing specific parts of an image while cropping out other areas. This is handy for highlighting specific image details.

Cropped Image using negative margins

Using CSS Grid

You can employ CSS Grid to create custom grids where images can be positioned and cropped precisely. By defining rows and columns in the grid, you can control the placement of images within specific grid cells, achieving unique cropping effects.

Cropped Image using CSS Grid

Also Read - Image Sampling

Frequently Asked Questions

How do I crop an image instead of stretch in CSS?

Use CSS properties like background-size: cover; for background images or object-fit: cover; for <img> tags to crop images without stretching, ensuring they fit within specified dimensions while maintaining aspect ratio.

Can you crop background image in CSS?

Yes, you can crop a background image in CSS using background-size: cover; or background-position: center; to ensure the image fits within the container dimensions without stretching, effectively cropping it.

How to reduce image in CSS?

Reduce image size in CSS using width and height properties to specify smaller dimensions, or use max-width and max-height to constrain proportions while maintaining image quality.

Conclusion

CSS image cropping is a powerful tool for designers to customize images for their designs. By using various CSS techniques, you can create attractive layouts, improve user experience, and boost performance. 

So now that you know about CSS Crop Image, you can refer to similar articles.


You may refer to our Guided Path on Code360 to enhance your skill set on DSA, Competitive Programming, System Design, etc. Check out essential interview questions, practice our available mock tests, look at the interview bundle for interview preparations, and so much more!

Live masterclass