Prevent overflow by scaling content smaller

clankill3r

I want an (animated gif) image in the left top corner. It should never be bigger then the actual size of the image. If the body is to small to show the whole image then the image should be shown smaller, proportional!

I had it working with using a background-image but I want to replay the gif by clicking on it and this didn't work using a background image so I went back to using an image tag.

In the width it works well, however, in the height it does not.

width = good:

enter image description here

height = not good:

enter image description here

Here the code:

<div id="gifdiv">
    <img src="images/someImg.png"/>
</div>





    body {

        padding: 0;
        border: 0;
        margin: 0;
        position: absolute;
        width: 100%;
        height: 100%;
    }

    #gifdiv {
        width: 100%;
        height: 100%;
        max-width: 660px;
        max-height: 370px;
    }

    img {
        width: 100%;
        height: auto;
        background-size: contain;
        background-repeat: no-repeat; 
    }

Here the code, in case it matters.

    $(document).ready(function() {

        $("#gifdiv").click(function () {
            // first time we replace png to gif
            // after that we just set the same thing again
            // which makes the gif play again from the start            
            var img = $(this).find("img").get(0);  
            var imgSrc = img.src;
            imgSrc = imgSrc.replace(".png", ".gif");
            img.src = imgSrc;     

      });

    });

So how can I make the height not exceed the window height?

a fiddle (without a gif) http://jsfiddle.net/clankill3r/gLmdwqg8/

Pierre Granger

I think this could be enough :

img {
    max-width: 100%;
    max-height: 100%;
    background-size: contain;
    background-repeat: no-repeat; 
}

Your main problem here is that as your image is 100% width, the height (auto or not) can't be reduced if there is not enough place.

http://jsfiddle.net/gLmdwqg8/1/

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

CSS center with translate and prevent content overflow

From Dev

How to prevent a flex item from shrinking smaller than its content?

From Dev

Prevent div from going smaller than its content when resizing

From Dev

How to fit content in a row and prevent overflow in react-native?

From Dev

How to prevent a flex item height to overflow due to it's content

From Dev

Image not scaling smaller as the resolution decreases

From Dev

Image not scaling smaller as the resolution decreases

From Dev

CSS Hover Scaling is unhiding overflow hidden content briefly then re-hiding

From Dev

Prevent GridView from scaling

From Dev

Prevent vertical overflow with CSS

From Dev

Prevent banner overflow

From Dev

Prevent flexbox overflow

From Dev

When screen is smaller then x y use overflow

From Dev

Content overflow outside the div

From Dev

Div content overflow

From Dev

Div content overflow

From Dev

iOS scaling with CGAffineTransformMakeScale on drawRect content

From Dev

Scale Checkbox without scaling content

From Dev

CSS Background Naturaly Scaling content

From Dev

prevent text to line break if overflow

From Dev

Prevent overflow / rubberband scrolling on iOS

From Dev

Prevent Overflow Horizontal Scroll on page

From Dev

Fabric JS: How to prevent scaling active group

From Dev

Prevent DIV from moving when scaling

From Dev

How to prevent Grid's children from scaling?

From Dev

Prevent DIV from moving when scaling

From Dev

How to prevent "bigger text" (font scaling) on iPhone

From Dev

Table content overflow to other divs

From Dev

fixed css menu not to overflow content