Results 1 to 4 of 4

Thread: img not preserving aspect ratio when browser resizes

  1. #1
    Join Date
    Jun 2008
    Posts
    192
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default img not preserving aspect ratio when browser resizes

    Hello,

    I'm having a problem with resizing an image when the browser resizes. Please see the attached screen shots to follow along.

    In the screen shot titled "full size.png" I have an image of a body diagram:

    Click image for larger version. 

Name:	full size.jpg 
Views:	138 
Size:	14.7 KB 
ID:	5750

    Then I shrink the browser by clicking and dragging up on the bottom edge of the browser. In the screen shot titled "mid size.png" you can see the result: everything looks fine. The body diagram has shrunk to fit the new browser size. I have the image set to preserve the height at 90%. The screen shots shows that this works (up to this point) and also preserves the aspect ratio. So by however much the image shrinks in height so as to fit the new browser size, so too does it shrink in width by the same ratio.

    Click image for larger version. 

Name:	mid size.jpg 
Views:	131 
Size:	16.7 KB 
ID:	5751

    Now I shrink the browser even more (same way: click and drag upwards the bottom edge). See the screen shot "small size (squishing).png". As you can see, at a certain point between "mid size.png" and "small size (squishing).png", it stops preserving the aspect ratio. The body diagram continues to shrink in height (which is good) but no longer shrinks in width. The guys gets fat.

    Click image for larger version. 

Name:	small size (squishing).jpg 
Views:	124 
Size:	16.0 KB 
ID:	5752

    It's almost as if there is a min-width for the image even though I'm not setting any. I even tried setting min-width: 0, but that didn't work. I don't have any min-widths for any of the image's parents divs either.

    Can anyone suggest what might be going on?

    This problem only shows up in Chrome. IE doesn't have this problem.

    Here is the HTML for the page (a lot of detail is left out, but this is the general layout):
    Code:
    <div id="Header">...</div>
    <div id="Body">
       <div id="LeftMenu">...</div>
       <div id="RightBodyDiagram"> ... <img id="BodyDiagram" ... > ... </div>
    </div>
    <div id="Footer">...</div>
    Here is the CSS:
    Code:
    #Header
    {
        height: 75px;
        width: 100%;
        background-color: #01132A;
        text-align: left;
        min-width: 868px;
    }
    
    #Body
    {
        display: table;
        height: calc(100vh - 105px);
        width: 100vw;
    }
    
    #LeftMenu
    {
        display: table-cell;
        width: 15vw;
        height: calc(100vh - 105px);
        background-color: #c6bfbf;
        min-width: 160px;
    }
    
    #RightBodyDiagram
    {
        width: 85vw;
        height: calc(100vh - 105px);
        display: table-cell;
        vertical-align: middle;
        text-align: center;
    }
    
    #BodyDiagram
    {
        height: 90%;
        outline: none;
    }
    
    #Footer
    {
        position: absolute;
        bottom: 0;
        display: table;
        height: 30px;
        width: 100vw;
        background-color: #000055;
        display: table;
        color: white;
        font-family: arial;
        font-size: 10pt;
    }
    Thanks for the help.
    Last edited by Beverleyh; 12-08-2015 at 08:44 PM. Reason: Formatting

  2. #2
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    Just a reminder on formatting your code. You can use the # button in the toolbar at the top of your post.

    A live page would also be easier for others to offer troubleshooting suggestions. You can set up a reduced, live example in JSBin, JSFiddle or CodePen
    Last edited by Beverleyh; 12-08-2015 at 08:54 PM.
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  3. #3
    Join Date
    Jun 2008
    Posts
    192
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default

    I created a fiddle here: https://jsfiddle.net/Lzqder3c/

    Again, I'm having trouble getting the image to show, but at least you can see the code and substitute any image you want.

    Interestingly, I found that, at least in this simplified example, I can remove the problem by setting the width of the div who id is "body" to 100% instead of 100vw. That is, 100% maintains the aspect ratio. 100vw does not.

    This fix doesn't work in my actually application, however.

  4. #4
    Join Date
    Jun 2008
    Posts
    192
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default

    I discovered something else: the point at which the body diagram starts losing its aspect ratio is when the footer starts overlapping with the two divs: "LeftMenu" and "RightBodyDiagram".

    I discovered this by giving the footer div 0.5 opacity. Then I could see the LeftMenu and RightBodyDiagram divs behind it after shrinking the browser passed a certain point.

    Please see the attachment for an example.

    I'm not sure why the LeftMenu and the RightBodyDiagram divs, or their parent div "Body", stops shrinking with the browser at this point as I don't have a min-height set for them and the height is set to calc(100vh - 105px).
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	footer opacity.jpg 
Views:	68 
Size:	18.6 KB 
ID:	5755  

Similar Threads

  1. image resize with window (maintain aspect ratio)
    By rpflorence in forum JavaScript
    Replies: 14
    Last Post: 01-10-2011, 09:47 PM
  2. .swf aspect ratio
    By nikidon in forum Flash
    Replies: 9
    Last Post: 02-23-2009, 11:56 PM
  3. PHP Photo Album (Aspect Ratio)
    By kenboldt in forum Dynamic Drive scripts help
    Replies: 6
    Last Post: 06-24-2008, 05:42 AM
  4. screen redirect script based on aspect ratio
    By kevin186 in forum Looking for such a script or service
    Replies: 16
    Last Post: 12-31-2007, 08:31 PM
  5. newly created aspect ratio redirect script
    By kevin186 in forum JavaScript
    Replies: 2
    Last Post: 12-31-2007, 04:58 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •