Stretch Image To Fit 100% Of Div Height And Width - Stack Overflow

    1. Home
    2. Questions
    3. Tags
    4. Users
    5. Companies
    6. Labs
    7. Jobs
    8. Discussions
    9. Collectives
    10. Communities for your favorite technologies. Explore all Collectives

  1. Teams

    Ask questions, find answers and collaborate at work with Stack Overflow for Teams.

    Explore Teams Create a free Team
  2. Teams
  3. 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 Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Get 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 27

I 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 Lloyd BanksLloyd Banks 36.2k58 gold badges162 silver badges255 bronze badges Add a comment |

4 Answers 4

Sorted by: Reset to default Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 27

You'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:00 bfavaretto's user avatar bfavarettobfavaretto 71.9k17 gold badges114 silver badges155 bronze badges Add a comment | 6

Instead 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 dodgerogers747's user avatar dodgerogers747dodgerogers747 3,3359 gold badges35 silver badges56 bronze badges 1
  • 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
Add a comment | 4

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:12 Haze811's user avatar Haze811Haze811 436 bronze badges Add a comment | 0

will 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 Jerritt PaceJerritt Pace 1 Add a comment |

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 discarded

Sign up or log in

Sign up using Google Sign up using Email and Password Submit

Post as a guest

Name Email

Required, but never shown

Post Your Answer Discard

By 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
Visit chat

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 smaller 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 css

Hot 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
more hot questions Question feed Subscribe to RSS Question feed

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

lang-html

Từ khóa » Html Css Stretch Image To Fit Div