- How to Auto-Resize the Image to fit an HTML Container
- Create HTML
- Add CSS
- Example of auto-resizing an image with the width and height properties:
- Here’s our result:
- Example of resizing an image using the object-fit property set to “cover”:
- Example of auto-resizing an image with the max-width and max-height properties:
- Example of resizing an image using the background-size property set to “contain” & «cover»:
- Related Resources
- Fix image size in css
- Learn Latest Tutorials
- Preparation
- Trending Technologies
- B.Tech / MCA
- Javatpoint Services
- Training For College Campus
- How To Scale and Crop Images with CSS object-fit
- Prerequisites
- Observing the Default Behavior of a Sample Image
- Using object-fit: fill
- Using object-fit: cover
- Using object-fit: contain
- Using object-fit: none
- Using object-fit: scale-down
- Using object-fit and object-position
- Conclusion
How to Auto-Resize the Image to fit an HTML Container
It is not complicated to make the image stretch to fit the container. CSS makes it possible to resize the image so as to fit an HTML container. To auto-resize an image or a video, you can use various CSS properties, which are described in this tutorial. It’s very easy if you follow the steps described below.
Let’s see an example and try to discuss each part of the code.
Create HTML
body> div class="box"> img src="https://pp.userapi.com/c622225/v622225117/10f33/47AAEI48pJU.jpg?ava=1" alt="Example image"/> div> body>
Add CSS
- Set the height and width of the .
- You can add border to your by using the border property with values of border-width, border-style and border-color properties.
- Set the height and width to «100%» for the image.
.box < width: 30%; height: 200px; border: 5px dashed #f7a239; > img < width: 100%; /* takes the 100 % width of its container (.box div)*/ height: 100%; /* takes the 100 % height of its container (.box div)*/ >
Let’s combine the code parts and see how it works! Here is the result of our code.
Example of auto-resizing an image with the width and height properties:
html> html> head> title>Title of the document title> style> body < background: crimson; > .box < width: 40%; height: 200px; border: 5px solid gold; > img < width: 100%; height: 100%; > style> head> body> div class="box"> img src="/uploads/media/default/0001/05/f32e5dec539f7c03f44990789d49d67c20c3e040.jpg" alt="Example image" /> div> body> html>
Here’s our result:
The image takes 40% width and 200px height of its container (red background).
In the example below, we use the «cover» value of the object-fit property. When using the «cover» value, the aspect ratio of the content is sized while filling the element’s content box. It will be clipped to fit the content box.
You can use other values like contain, scale-down, etc. for object-fit and make sure to check them as well. Still, we’ll mostly use the cover value as we like our image to cover its container as much as it doesn’t hurt the aspect ratio.
Example of resizing an image using the object-fit property set to “cover”:
html> html> head> style> .container < width: 60%; height: 300px; border: 5px solid gold; > img < width: 100%; height: 100%; object-fit: cover; > style> head> body> div class="container"> img src="https://images.unsplash.com/photo-1581974267369-3f2fe3b4545c?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60" alt="Image" /> div> body> html>
This image will help you understand it better! See how the image fits its aspect ratio based on the screen size changes!
See another example where the image size is set manually, and the object-fit property is set as well. In this case, when the browser is resized, the image will preserve its aspect ratio and won’t be resized according to the container.
Example of resizing an image using the object-fit property:
html> html> head> style> body < text-align: center; > img < width: 400px; height: 200px; object-fit: cover; > style> head> body> img src="https://images.unsplash.com/photo-1581974267369-3f2fe3b4545c?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60" alt="Image" /> body> html>
Let’s see this example without object-fit property first! Ugly, huh? You’re right!
In the next example, we use the max-width and the rules can be applied to max-height as well. The max-height property sets the maximum height of an element, and the max-width property sets the maximum width of an element. To resize an image proportionally, set either the height or width to «100%», but not both. If you set both to «100%», the image will be stretched.
Example of auto-resizing an image with the max-width and max-height properties:
html> html> head> title>Title of the document title> style> img < width: 600px; /* image initial width */ > div < border: 2px dotted #000000; > .container < width: 500px; /* container initial width */ border: 2px solid gold; > style> head> body> div class="container"> img src="/uploads/media/default/0001/05/8dc771228e65a66d63299043ad824e26fb9b879f.jpg" alt="Circle portrait" /> div> body> html>
Here’s the problem! Our image is out of its container because its width (600px) is bigger than its container width (500px)!
To solve our problem, we’ll use the max-width: 100% property, which not allows the image to take any width bigger than its container (here, not more than 500px).
Now, it will scale down when the width is less than 500px. The same rule is applicable for max-height property.
html> html> head> title>Title of the document title> style> img < width: 600px; max-width: 100%; /* add this line */ > .container < height: 100%; width: 500px; border: 2px solid gold; > style> head> body> div class="container"> img src="https://www.w3docs.com//uploads/media/default/0001/05/8dc771228e65a66d63299043ad824e26fb9b879f.jpg" alt="image example" /> div> body> html>
To use an image as a CSS background, use the background-size property. This property offers two special values: contain and cover. Let’s see examples of these values.
Example of resizing an image using the background-size property set to “contain” & «cover»:
html> html> head> title>Title of the document title> style> .box < width: 300px; height: 250px; border: 5px solid gold; background-image: url("https://images.unsplash.com/photo-1582093236149-516a8be696fe?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60"); background-size: contain; /* try other properties here like cover, contain, etc */ background-repeat: no-repeat; background-position: 50% 50%; > style> head> body> div class="box"> div> body> html>
Here’s what contain will give us!
Much better! I hope you’ve enjoyed it all!
Related Resources
Fix image size in css
Learn Latest Tutorials
Preparation
Trending Technologies
B.Tech / MCA
Javatpoint Services
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
- Website Designing
- Website Development
- Java Development
- PHP Development
- WordPress
- Graphic Designing
- Logo
- Digital Marketing
- On Page and Off Page SEO
- PPC
- Content Development
- Corporate Training
- Classroom and Online Training
- Data Entry
Training For College Campus
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week
Like/Subscribe us for latest updates or newsletter
How To Scale and Crop Images with CSS object-fit
You will likely encounter a scenario where you will want to preserve the original aspect ratio when working with images. Preserving the aspect ratio will prevent images from appearing distorted by either being stretched or squished. A common solution for this problem is to use the background-image CSS property. A more modern approach would be to use the object-fit CSS property.
In this article, you will explore the effects of the fill , cover , contain , none , and scale-down values available to the object-fit CSS property and how it can crop and scale images. You will also explore the object-position CSS property and how it can offset images.
Prerequisites
If you would like to follow along with this article, you will need:
- Understanding CSS property and values.
- Using CSS declarations inline with the style property.
- A code editor.
- A modern web browser that supports object-fit and object-position .
Observing the Default Behavior of a Sample Image
Consider the following code used to display a sample image:
img src="https://assets.digitalocean.com/articles/alligator/css/object-fit/example-object-fit.jpg" width="600" height="337" style="width: 600px; height: 337px;" alt="Sample image of a turtle riding on top of an alligator that is swimming in the water - scaled to 600 x 337." />
This code will produce the following result in the browser:
This image has an original width of 1200px and a height of 674px. Using img attributes, the width has been set to 600 and 337 — half the original dimensions — preserving the aspect ratio.
Now, consider a situation where the layout expects images to occupy a width of 300px and a height of 337px:
img src="https://assets.digitalocean.com/articles/alligator/css/object-fit/example-object-fit.jpg" width="600" height="337" style="width: 300px; height: 337px;" alt="Sample image of a tutle riding on top of an alligator that is swimming in the water - scaled to 300 x 337." />
This code will produce the following result in the browser:
The resulting image no longer preserves the original aspect ratio and appears to be visually “squished”.
Using object-fit: fill
The fill value is the initial value for object-fit . This value will not preserve the original aspect ratio.
img . style="width: 300px; height: 337px; object-fit: fill;" . />
This code will produce the following result in the browser:
As this is the “initial” value for browser rendering engines, there is no change in appearance from the scaled image. The resulting image still appears squished.
Using object-fit: cover
The cover value preserves the original aspect ratio, but the image occupies all the available space.
img . style="width: 300px; height: 337px; object-fit: cover;" . />
This code will produce the following result in the browser:
In certain situations, object-fit: cover will result in the image appearing cropped. In this example image, some parts of the original image on the left and right do not appear because they cannot fit within the bounds of the declared width.
Using object-fit: contain
The contain value preserves the original aspect ratio, but the image is also constrained to not exceed the bounds of the available space.
img . style="width: 300px; height: 337px; object-fit: contain;" . />
This code will produce the following result in the browser:
In certain situations, object-fit: contain will result in the image not filling all the available space. In this example image, there is vertical space above and below the image because the declared height is taller than the scaled-down height.
Using object-fit: none
The none value does not resize the image at all.
img . style="width: 300px; height: 337px; object-fit: none;" . />
This code will produce the following result in the browser:
In situations where the image is larger than the available space, it will appear cropped. In this example image, some parts of the original image on the left, right, top, and bottom do not appear because they cannot fit within the bounds of the declared width and height.
Using object-fit: scale-down
The scale-down value will either display an image like contain or none depending on which would result in a smaller image.
img . style="width: 300px; height: 337px; object-fit: scale-down;" . />
This code will produce the following result in the browser:
In this example image, the image has been scaled down to behave like contain .
Using object-fit and object-position
If the resulting image from object-fit appears cropped, by default the image will appear centered. The object-position property can be used to change the point of focus.
Consider the object-fit: cover example from before:
Now let’s change the position of the visible part of the image on the X-axis to reveal the right-most edge of the image:
img . style="width: 300px; height: 337px; object-fit: cover; object-position: 100% 0;" . />
This code will produce the following result in the browser:
In this example image, the turtle has been cropped out of the image.
And finally, let’s observe what happens if the position is specified outside of the bounds of the available space:
img . style="width: 300px; height: 337px; object-fit: cover; object-position: -20% 0;" . />
This code will produce the following result in the browser:
In this example image, the turtle and alligator heads have been cropped out of the image. There is also spacing to make up the 20% of offset on the left of the image.
Conclusion
In this article, you explored the values available for the object-fit and object-position CSS properties.
Before using object-fit in your project, verify that it is supported in the browsers used by your intended audience by checking the browser support on Can I Use?.
If you’d like to learn more about CSS, check out our CSS topic page for exercises and programming projects.
Want to deploy your application quickly? Try Cloudways, the #1 managed hosting provider for small-to-medium businesses, agencies, and developers — for free. DigitalOcean and Cloudways together will give you a reliable, scalable, and hassle-free managed hosting experience with anytime support that makes all your hosting worries a thing of the past. Start with $100 in free credits!