How to dynamically resize image in css?

13,034

Try to look for articles about ResponsiveUI - that will give you much usefull info. "autoscaling" could be done with next code:

<div class="wrapper">
 <img src="img.png">
</div>

    .wrapper img {
     width: 100%;
     max-width: 100%
     height: 100%;
    }
Share:
13,034
user1754602
Author by

user1754602

Updated on June 04, 2022

Comments

  • user1754602
    user1754602 almost 2 years

    I have a simple html + css page that has 3 images on it. I'm trying to make the page resize depending on the size of the browser window. Right now, I have the 3 images in a div that is set to a height as a percentage of the surrounding containers, and the images are set to height:100% and width:auto. now, that works fine if you resize the entire window- but if you only change the width, they won't resize of course since the height didn't change, and they end up getting pushed down the page which is really ugly.

    my first question: is there a good way to make the images resize no matter if you change the height OR width of the browser using just html/css? If not, should I be using jquery and if so can you point me to a good resource?

    Second question: if it's not possible, how do I at least stop them from getting bumped down a line? I tried making the overflow hidden or scrolled, but they still get bumped, then either cut off your you have to scroll vertically.

    Here's a link to the live page: http://carissalyn.com/Landing.html (yes I realize the images load slowly I'll compress them before it's live). Let me know if you need any other info.

    Here's the relevant css (img container is inside fadingtext which is inside body):

    body,html{
    height:100%;
    margin:0; padding:0;
    }
    
    #imgcontainer{
    height: 100%;
    width: 90%;
    display: inline-block;
    }
    
    img { 
    max-height:90%; 
    width: auto;
    }
    
    #fading_text {
    text-align: center;
    height: 60%;
    -webkit-animation: fade-text 20s 1;
    -moz-animation: fade-text 20s 1;
    }
    

    And here's the relevant html to clarify that there are 3 images:

    <div id="fading_text">
    <div id="imgcontainer">
    
    <a href="" class="imghover"><img src="images/leaf.jpg" alt="portrait" class="border"></a>
    <a href="" class="imghover"><img src="images/DSC_2280-Edit-Edit-Final.jpg"  alt="portrait" class="border"></a>
    <a href="" class="imghover"><img src="images/DSC_2685.jpg" alt="blog" class="border">  </a>
    
    </div>
    </div>
    
  • user1754602
    user1754602 over 11 years
    Does that work with multiple photos though? I don't want to photo width to be 100% if there are more than one... right? I'll look up ResponsiveUI thanks.
  • user1754602
    user1754602 over 11 years
    Any other ideas? I've looked at the ResponsiveUI and while I think that's the direction I'll go, I'm not sure it addresses my problem. Even within the defined sizes, if the width gets changed and not the height I run into this problem.