Next-Gen App & Browser
Testing Cloud
Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles
Resizing an image using CSS is a common and effective technique in modern web development. CSS allows you to control the dimensions of an image precisely while maintaining design consistency, responsiveness, and performance across devices.
Following are the most reliable methods to resize images using CSS:
1. Using width and height Properties
You can directly set the image size by specifying the width and height in fixed units such as px, em, or %.
img {
width: 300px;
height: 200px;
}
This method gives you exact control but can lead to distortion if the aspect ratio is not preserved.
2. Maintaining Aspect Ratio
To prevent the image from appearing stretched or squished, define only one dimension (usually width) and set the other to auto:
img {
width: 300px;
height: auto;
}
This approach resizes the image while maintaining its original proportions.
3. Responsive Image Resizing
To make images adapt to different screen sizes, use percentage-based sizing and max-width:
img {
width: 100%;
height: auto;
max-width: 600px;
}
This makes the image scale down on smaller screens while ensuring it doesn’t exceed the container’s maximum width.
4. Using CSS Classes for Reusability
Instead of applying styles directly to the <img> tag, define a class for better maintainability:
.responsive-img {
width: 100%;
height: auto;
max-width: 500px;
}
Then use it in your HTML:
<img src="example.jpg" class="responsive-img" alt="Responsive Image">
5. Resizing with object-fit for Better Control
When working with images inside containers or backgrounds, object-fit helps maintain the aspect ratio while fitting the image to its container:
img {
width: 100%;
height: 300px;
object-fit: cover; /* or 'contain' depending on your need */
}
To resize an image in Photoshop, open the image, go to the 'Image' menu, select 'Image Size,' input desired dimensions, ensure 'Constrain Proportions' is checked, and click 'OK.' Save the resized image in the preferred format.
To resize an image on an iPhone, open the photo in the 'Photos' app, tap 'Edit,' then select the crop icon. Adjust the size using the grid and drag the corners. Finally, tap 'Done' to save the resized image.
To resize an image on Mac, use the built-in Preview app. Open the image, click 'Tools' in the menu, select 'Adjust Size,' set desired dimensions, and save.
To resize an image in HTML, use the 'width' and 'height' attributes within the 'img' tag. Specify the desired dimensions in pixels or percentages. Example: <img src='image.jpg' width='200' height='150'>.
To resize an image in Canva, follow these steps: 1. Open your design. 2. Click on the image you want to resize. 3. Drag the corners to adjust the size proportionately. 4. To resize without proportions, hold 'Shift' while dragging. 5. Release when satisfied. 6. Save or download your updated image.
To resize an image to fit a div in CSS, use the ‘background-size’ property with the value 'cover' for the div's background-image. Alternatively, set the 'max-width: 100%' for the image to ensure it adjusts proportionally within the div.
To resize an image to fit the background in CSS, use the 'background-size' property. Set it to 'cover' for the image to cover the entire background area while maintaining aspect ratio. Alternatively, use 'contain' to fit the image within the background while preserving its proportions.
To resize an image proportionally in CSS, use the 'max-width' property set to 100%. This ensures the image adjusts its size according to the parent container while maintaining its aspect ratio. For example: 'img { max-width: 100%; height: auto; }'
KaneAI - Testing Assistant
World’s first AI-Native E2E testing agent.