Stretch Image To Fit 100% Of Div Height And Width - Stack Overflow
Có thể bạn quan tâm
-
- Home
- Questions
- Tags
- Users
- Companies
- Labs
- Jobs
- Discussions
- Collectives
-
Communities for your favorite technologies. Explore all Collectives
- Teams
Ask questions, find answers and collaborate at work with Stack Overflow for Teams.
Explore Teams Create a free Team - Teams
-
Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams
Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about CollectivesTeams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about TeamsGet early access and see previews of new features.
Learn more about Labs Stretch Image to Fit 100% of Div Height and Width Ask Question Asked 11 years, 1 month ago Modified 4 years, 7 months ago Viewed 179k times 27I have a div with the following CSS
#mydiv{ top: 50px; left: 50px; width: 200px; height: 200px; }and my HTML looks like this
<div id = "mydiv"> <img src = "folder/file.jpg" width = "200px" height = "200px"> </div>I'd like my web image to always be the same size (in a 1:1 aspect ration) no matter what the resolution of the actual image is. If my actual image files are square (with 1:1 ratio) then this isn't a problem. But if the actual image files are not square then the displayed web image do stretch to 100% of both the div's height and width (in this case 200px).
How do I get different image sizes to fit to my DIV?
Share Follow asked May 11, 2013 at 20:57![Lloyd Banks's user avatar](https://i.sstatic.net/UQdmr.jpg?s=64)
4 Answers
Sorted by: Reset to default Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 27You're mixing notations. It should be:
<img src="folder/file.jpg" width="200" height="200">(note, no px). Or:
<img src="folder/file.jpg" style="width: 200px; height: 200px;">(using the style attribute) The style attribute could be replaced with the following CSS:
#mydiv img { width: 200px; height: 200px; }or
#mydiv img { width: 100%; height: 100%; } Share Follow edited May 11, 2013 at 21:14 answered May 11, 2013 at 21:00Instead of setting absolute widths and heights, you can use percentages:
#mydiv img { height: 100%; width: 100%; } Share Follow edited Jan 19, 2014 at 19:49 answered May 11, 2013 at 21:00- 1 And, if your image is right next down the DOM tree use the direct child selector, #mydiv > img - major performance optimization right there. Remember, browsers (and most libraries like jQuery too) read CSS from right to left. – pilau Commented Feb 22, 2014 at 21:41
Or you can put in the CSS,
<style> div#img { background-image: url(“file.png"); color:yellow (this part doesn't matter; height:100%; width:100%; } </style> Share Follow edited Aug 13, 2015 at 2:17 answered Aug 13, 2015 at 2:12will the height attribute stretch the image beyond its native resolution? If I have a image with a height of say 420 pixels, I can't get css to stretch the image beyond the native resolution to fill the height of the viewport.
I am getting pretty close results with:
.rightdiv img { max-width: 25vw; min-height: 100vh; }the 100vh is getting pretty close, with just a few pixels left over at the bottom for some reason.
Share Follow answered Nov 27, 2019 at 3:28![Jerritt Pace's user avatar](https://lh6.googleusercontent.com/-YU0tMnnNsIg/AAAAAAAAAAI/AAAAAAAAAAA/ACHi3rdM3gT2P767Qyihg_RzppqIZXHmrQ/mo/photo.jpg?sz=64)
Your Answer
Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Draft saved Draft discardedSign up or log in
Sign up using Google Sign up using Email and Password SubmitPost as a guest
Name EmailRequired, but never shown
Post Your Answer DiscardBy clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Not the answer you're looking for? Browse other questions tagged
or ask your own question.- The Overflow Blog
- Community Products Roadmap Update, July 2024
- Featured on Meta
- We spent a sprint addressing your requests — here’s how it went
- Upcoming initiatives on Stack Overflow and across the Stack Exchange network...
- Policy: Generative AI (e.g., ChatGPT) is banned
- The [lib] tag is being burninated
- What makes a homepage useful for logged-in users
Linked
2 How to make Image to zoom in when resizing window for a website 0 Scale image to max size in a flexbox 0 CSS : width:100% or fixed height whichever is smallerRelated
24 Getting image to stretch a div 0 How to set image width and height 100% with CSS? 2 stretch image to 100% of viewport 26 Make image fill div completely without stretching 2 Auto height of a div with image 100% 2 Css make an image fit in a 100% wide div 0 Can't seem to stretch an image to 100% of the webpage 1 Image with 100% width and a maximum and minimum height set without stretching img 0 image that has 100% height and stretches to the right and left 1 How to stretch an image to 50% of the screen in cssHot Network Questions
- Read an article about connecting a hot tub to a heat pump, looking for details
- What spells can I cast while swallowed?
- Do you always experience the gravitational influence of other mass as you see them in your frame?
- firefox returns odd results for file:/// or file:///tmp
- What is this brass plumbing device on my pex supply line?
- Why screenshots are being saved in moved folder?
- In-Place Reordering of Doubly Linked List Nodes to Ensure Memory Contiguity
- Does Justice Sotomayor's "Seal Team 6" example, in and of itself, explicitly give the President the authority to execute opponents? If not, why not?
- Bitcoin regtest CPUminer not even starting
- Where is the pentagon in the Fibonacci sequence?
- Weather on a Flat, Infinite Sea
- What is the purpose of the BJT in this circuit?
- Does installing Ubuntu Deskto on Xubuntu LTS adopt the longer Ubuntu support period
- 11 trees in 6 rows with 4 trees in each row
- Can the differential be unitless while the variable have an unit in integration?
- Does it make sense to use a skyhook to launch and deorbit mega-satellite constellations now?
- How do I drill a 60cm hole in a tree stump, 4.4 cm wide?
- Optimizing Pi Estimation Code
- Were there any stone vessels (including mortars) made in Paleolithic?
- A check given by castling: is it a discovered check or a special case?
- Do thermodynamic cycles occur only in human-made machines?
- What is the reason for using decibels to measure sound?
- Fontspec and \readline produce trailing blank symbol
- Using 50 Ω coax cable instead of passive probe
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
lang-htmlTừ khóa » Html Css Stretch Image To Fit Div
-
How To Auto-resize An Image To Fit Into A DIV Container Using CSS
-
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 Scale And Crop Images With CSS Object-fit | DigitalOcean
-
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