How To Scale And Crop Images With CSS Object-fit | DigitalOcean
Có thể bạn quan tâm
- Blog
- Docs
- Get Support
- Contact Sales
- Tutorials
- Questions
- Product Docs
- Cloud Chats
- Search Community
Report this
What is the reason for this report?This undefined is spamThis undefined is offensiveThis undefined is off-topicThis undefined is otherSubmitTable of contents
- Prerequisites
- Observing the Default Behavior of a Sample Image
- Using objectfit fill
- Using objectfit cover
- Using objectfit contain
- Using objectfit none
- Using objectfit scaledown
- Using objectfit and objectposition
- Conclusion
- Tutorials
- CSS
- How To Scale and Crop Images with CSS object-fit
By Alligator
Table of contentsPopular topicsIntroduction
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.
Deploy your frontend applications from GitHub using DigitalOcean App Platform. Let DigitalOcean focus on scaling your app.
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.
object-fit also supports inherit, initial, and unset.
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.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
Learn more about our products
About the author
Alligator.io is a developer-focused resource that offers tutorials and insights on a wide range of modern front-end technologies, including Angular 2+, Vue.js, React, TypeScript, Ionic, and JavaScript.
See author profileCategory:TutorialTags:CSSStill looking for an answer?
Ask a questionSearch for more helpWas this helpful?YesNoComments(2)Follow-up questions(0)This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Sign in/up to commentThank You Soo Much!! Wow you did me happy =)
ReplyThank you for this great article! Really helps!
Reply
This work is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License.Deploy on DigitalOcean
Click below to sign up for DigitalOcean's virtual machines, Databases, and AIML products.Sign upPopular Topics
- AI/ML
- Ubuntu
- Linux Basics
- JavaScript
- Python
- MySQL
- Docker
- Kubernetes
- All tutorials
- Talk to an expert
Featured tutorials
- SOLID Design Principles Explained: Building Better Software Architecture
- How To Remove Docker Images, Containers, and Volumes
- How to Create a MySQL User and Grant Privileges (Step-by-Step)
- All tutorials
- All topic tags
Join the Tech Talk
Success! Thank you! Please check your email for further details.Please complete your information!
Become a contributor for community
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Sign Up
DigitalOcean Documentation
Full documentation for every DigitalOcean product.
Learn more
Resources for startups and SMBs
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Learn more
Get our newsletter
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
SubmitSubmitNew accounts only. By submitting your email you agree to our Privacy Policy
The developer cloud
Scale up as you grow — whether you're running one virtual machine or ten thousand.
View all productsGet started for free
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
Get started*This promotional offer applies to new accounts only.
© 2025 DigitalOcean, LLC.Sitemap.Từ khóa » Html Css Stretch Image To Fit Div
-
How To Auto-resize An Image To Fit Into A DIV Container Using CSS
-
Stretch Image To Fit 100% Of Div Height And Width - Stack Overflow
-
CSS Object-fit Property - W3Schools
-
How To Auto-resize An Image To Fit A Div Container Using CSS?
-
Make Image Stretch To Fit Div Code Example
-
Css Stretch Image To Fill Div Code Example - Code Grepper
-
How To Auto-Resize The Image To Fit An HTML Container - W3docs
-
Css Resize Image To Fit Div No Stretching - MaxInterview
-
How To Resize An Image In HTML?
-
Object-fit | CSS-Tricks
-
How To Stretch A Background Image To Fit A Web Page - ThoughtCo
-
Object Fit - Tailwind CSS
-
How To Fill A Box With An Image Without Distorting It - MDN Web Docs
-
Object-fit - CSS: Cascading Style Sheets - MDN Web Docs - Mozilla