How to Crop Image in CSS?
Last Updated :
15 Nov, 2024
Cropping images using CSS is an effective way to display only a portion of the image without altering the image file itself. Cropping can allow the designers to focus on the specific parts of the image while maintaining control over how the image fits within the layout.
1. Using overflow Property
This approach can rely on setting the dimensions of the container and using the overflow: hidden property to ensure that any part of the image that extends beyond the container is not visible. The content can be clipped to fit the container.
Syntax
.container {
width: 300px;
height: 200px;
overflow: hidden;
/* Hide content outside the bounds */
}
.container img {
width: 400px;
/* Image size exceeds container width */
height: auto;
/* Maintain aspect ratio */
}
HTML
<!DOCTYPE>
<html>
<head>
<style>
.container {
width: 300px;
height: 200px;
overflow: hidden;
}
.container img {
width: 400px;
height: auto;
}
</style>
</head>
<body>
<div class="container">
<img src=
"https://media.geeksforgeeks.org/wp-content/uploads/20240911105255/geekforgeek.png"
alt="Sample Image">
</div>
</body>
</html>
Output
Crop Image2. Using clip-path Property
The clip-path property can allows you to create the clipping region where only the portion of the image inside the region is visible. We can clip the image in the different shapes lik rectangles, circles, elipses, and polygons.
Syntax
img {
clip-path: circle(50%);
/* Creates a circular crop of the image */
width: 300px;
}
HTML
<!DOCTYPE html>
<html>
<head>
<style>
img {
clip-path: circle(50%);
width: 300px;
}
</style>
</head>
<body>
<img src=
"https://media.geeksforgeeks.org/wp-content/uploads/20240911105255/geekforgeek.png"
alt="Sample Image">
</body>
</html>
Output
Crop Image3. Using background-image Property
Using the image as a background can allows more flexibility in the cropping, since you can control how image can be displayed within the container using the background-size and background-position properties. This is commonly used for creating the image containers with specific dimensions.
Syntax
.container {
width: 300px;
height: 200px;
background-image: url('image.jpg');
background-size: cover;
/* Crops and scales the image to fill the container */
background-position: center;
/* Centers the image */
}
HTML
<!DOCTYPE html>
<html>
<head>
<style>
.container {
width: 300px;
height: 100px;
background-image: url(
'https://media.geeksforgeeks.org/wp-content/uploads/20240911105255/geekforgeek.png');
background-size: cover;
background-position: center;
}
</style>
</head>
<body>
<div class="container"></div>
</body>
</html>
Output
Crop Image
Similar Reads
How to Add Background Image in CSS? The CSS background-image property is used to add an image as a background to an element.Syntaxbackground-image: url()1. Setting background Image of an ElementA background image is added to an h1 tag using the URL.HTML<h1 style="background-image: url( 'https://media.geeksforgeeks.org/wp-content/up
1 min read
How to Set Background Image in CSS? CSS (Cascading Style Sheets) can allow developers to set the background image for the web pages or specific HTML elements. This feature can help enhance the visual aesthetics of the website. The background-image property in CSS can be used to specific one or multiple background images to be applied
3 min read
How to crop images in ReactJS ? Image cropping is a common requirement in web applications, especially when dealing with user-generated content, photo editing, or image manipulation. ReactJS, being a popular JavaScript library for building user interfaces, provides various techniques and libraries that can be used to implement ima
3 min read
How to Adjust Image Size in CSS? Adjusting the image size using CSS is a common task for web developers when designing responsive and visually appealing web pages. CSS can provide several ways to manipulate the dimensions of the image, including setting fixed sizes, scaling, or making them responsive to the container they are in. W
4 min read
How to Crop an Image in Photoshop? Adobe Photoshop is a raster-based image editing software. It is developed by Adobe.Inc and available for both macOS and Windows operating systems. You can use Photoshop to create or edit images, posters, banners, logos, invitation cards, and various types of graphic designing work. It provides vario
4 min read