Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overflow hidden not working on container with 100% width and height

Tags:

html

css

I am trying to get a image to fill up the viewport and auto resize when the browser gets resized, which is working, the only problem is that for some reason I can't get the image to NOT extend outside of the #wrapper element, even though it has overflow set to hidden, I am assuming it is because I am using percentages instead of a fixed width and height?

Now when I put overflow: hidden; on the body element it works, but then when you select text anywhere on the page and drag down, it scrolls down to the bottom of the image, which is problematic as I have a footer menu that is absolutely positioned to the bottom of the screen, which then moves up with the image as it is dragged down, and ruins the whole effect.

So basically I just want to have a auto resizing background, that doesn't overflow the viewport and cause scrollbars, and that allows your positioned content to stay where it is and not scroll up when you drag down on selected text.

HTML:

<html>
    <head>
        <title>project</title>
    </head>
    <body>
        <div id="wrapper">
            <div id="content">
                <img src="image.jpg" width="1400" height="1200" />
            </div>
        </div>
    </body>
</html>

CSS:

html, body {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
}

#wrapper {
    width: 100%;
    height: 100%;
    overflow: hidden;
    position: relative;
}

#content {
    width: 100%;
    height: 100%;
}

#wrapper img {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
}
like image 771
Odyss3us Avatar asked Sep 18 '11 11:09

Odyss3us


People also ask

How do I hide content of overflow?

Set the div with a width or height, (otherwise it won't know whether something is overflowing). Then, add the overflow:hidden; CSS property-value pair.

What happens if we use overflow hidden?

hidden - The overflow is clipped, and the rest of the content will be invisible. scroll - The overflow is clipped, and a scrollbar is added to see the rest of the content. auto - Similar to scroll , but it adds scrollbars only when necessary.

How do I fix the overflow in CSS?

To change this, set the min-width or min-height property.” This means that a flex item with a long word won't shrink below its minimum content size. To fix this, we can either use an overflow value other than visible , or we can set min-width: 0 on the flex item.

How do I make an image overflow hidden?

To activate the overflow property enclose the image, within a div of a particular width and height, and set overflow:hidden . This will ensure that the base container will retain its structure, and any image overflow will be hidden behind the container.


2 Answers

I know this is an old post, but for the benefit of others, since position:fixed isn't the most compatible way either, changing your #wrapper element to have position:relative; solves the problem.

Thanks, Scott

like image 151
scott Avatar answered Nov 14 '22 23:11

scott


Ok, got it working, I changed this

#wrapper img {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
}

to this

#wrapper img {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
}

so as you can see, all I changed was the position property, from absolute to fixed, and that did the trick. :)

like image 41
Odyss3us Avatar answered Nov 15 '22 00:11

Odyss3us