How To Resize An Image To Fit An HTML Container | Codetain
Có thể bạn quan tâm
- Development
4/27/2020
![hero image for How to resize an image to fit an HTML container](https://www.datocms-assets.com/21211/1587976315-fitincontainer.jpg?auto=format&w=1100)
![Avatar image of Artur Woźniak](https://www.datocms-assets.com/21211/1593002608-mg6268.jpg?auto=format&w=600)
Frontend Developer
Do you need help from our developers?
Contact usIn this article, I would like to present how to resize an image to fit a HTML container.
The most popular approach is to add object-fit property to img tag, we will use it as well, plus we will add other CSS properties.
To get things done we will use this CSS selectors and properties.
.project { display: flex; margin: 50px0; } .project__image { flex: 1; } .project__image--relative { position: relative; } .project__image--background-img { background: url('https://images.pexels.com/photos/4015752/pexels-photo-4015752.jpeg?cs=srgb&dl=drewno-wakacje-ludzie-sztuka-4015752.jpg&fm=jpg'); background-position: center; background-size: cover; background-repeat: no-repeat; } .project__imageimg { width: 100%; height: 400px; object-fit: cover; object-position: center; } .project__image--relativeimg { position: absolute; height: 100%; } .project__description { flex: 1; margin: 0; padding: 20px; align-self: center; }In the following examples, we will change HTMLand CSS structure to achieve our goal.
Example 1 - background image
<div class="project"> <divclass="project__imageproject__image--background-img"></div> <pclass="project__description"> ImagedescriptionReceivedthelikewiselawgracefulhis. Normightsetalongcharmnowequalgreen. Pleasedyetequallycorrectcolonelnotone. Sayanxiouscarriedcompactconductsexgeneralnaycertain. Mrsforrecommendexquisitehouseholdeagernesspreservednow. Myimprovedhonouredheamecstaticquittinggreatestformerly. Examineshebrotherprudentadddayham. Farstairsnowcomingbedopposehuntedbecomehis. Youzealouslydeparturehadprocuringsuspicion. Bookswhosefrontwouldpurseifbedodecay. Quittingyouwayformerlydisposedperceiveladyshipare. Commonturnedboydirectandyet. </p> </div>As you can see we have no img tag in this example. We use background image with background-size: cover toresize the background image and make sure the image is fully visible.
![](https://www.datocms-assets.com/21211/1587977630-image1.png?auto=format&w=764)
Example 2 - nested img element and object-fit: cover property
<div class="project"> <divclass="project__image"> <imgsrc="https://images.pexels.com/photos/4015752/pexels-photo-4015752.jpeg?cs=srgb&dl=drewno-wakacje-ludzie-sztuka-4015752.jpg&fm=jpg" alt="exampleimage"> </div> <pclass="project__description"> ImagedescriptionReceivedthelikewiselawgracefulhis. Normightsetalongcharmnowequalgreen. Pleasedyetequallycorrectcolonelnotone. Sayanxiouscarriedcompactconductsexgeneralnaycertain. Mrsforrecommendexquisitehouseholdeagernesspreservednow. Myimprovedhonouredheamecstaticquittinggreatestformerly. Examineshebrotherprudentadddayham. Farstairsnowcomingbedopposehuntedbecomehis. Youzealouslydeparturehadprocuringsuspicion. Bookswhosefrontwouldpurseifbedodecay. Quittingyouwayformerlydisposedperceiveladyshipare. Commonturnedboydirectandyet. </p> </div>In this example Img tag is child of div with class project__image, it gets CSS properties from project__image img class.
![](https://www.datocms-assets.com/21211/1587977983-image2.png?auto=format&w=777)
Example 3 - nested img with position absolute
<div class="project"> <divclass="project__imageproject__image--relative"> <imgsrc="https://images.pexels.com/photos/4015752/pexels-photo-4015752.jpeg?cs=srgb&dl=drewno-wakacje-ludzie-sztuka-4015752.jpg&fm=jpg" alt="exampleimage"> </div> <pclass="project__description"> ImagedescriptionReceivedthelikewiselawgracefulhis. Normightsetalongcharmnowequalgreen. Pleasedyetequallycorrectcolonelnotone. Sayanxiouscarriedcompactconductsexgeneralnaycertain. Mrsforrecommendexquisitehouseholdeagernesspreservednow. Myimprovedhonouredheamecstaticquittinggreatestformerly. Examineshebrotherprudentadddayham. Farstairsnowcomingbedopposehuntedbecomehis. Youzealouslydeparturehadprocuringsuspicion. Bookswhosefrontwouldpurseifbedodecay. Quittingyouwayformerlydisposedperceiveladyshipare. Commonturnedboydirectandyet. </p> </div>In the above example we changed CSS class for img container. Now our img is a child of div with class project__image project__image--relative. It will consume the values from project__image img and project__image—relative img where we add position absolute and we override height to be 100% to stretch img to cover the whole container.
![](https://www.datocms-assets.com/21211/1587978091-image3.png?auto=format&w=776)
Now, I know the above examples are very simple, but I just wanted to highlight possible solutions to resize an image to fit an HTML Container.
However, I hope you could learn something new thanks to this article.
Helpful resources:
- Object-fit: cover: https://www.w3schools.com/css/css3_object-fit.asp
- RWD - images: https://www.w3schools.com/css/css_rwd_images.asp
- Resize to fit: https://www.w3docs.com/snippets/css/how-to-auto-resize-an-image-to-fit-an-html-container.html
Từ khóa » Html Scale Image To Fit Container
-
How Do I Auto-resize An Image To Fit A 'div' Container? - Stack Overflow
-
CSS Object-fit Property - W3Schools
-
How To Auto-resize An Image To Fit A Div Container Using CSS?
-
How To Auto-resize An Image To Fit Into A DIV Container Using CSS
-
How To Resize An Image In HTML?
-
How To Auto-Resize The Image To Fit An HTML Container - W3docs
-
Object-fit - CSS: Cascading Style Sheets - MDN Web Docs - Mozilla
-
How To Fill A Box With An Image Without Distorting It - MDN Web Docs
-
Object Fit - Tailwind CSS
-
How To Auto-resize An Image To Fit Into A DIV Container Using CSS?
-
Resize Images Proportionally While Keeping The Aspect Ratio
-
How To Scale And Crop Images With CSS Object-fit | DigitalOcean
-
Object-fit | CSS-Tricks
-
A Deep Dive Into Object-fit And Background-size In CSS