<!DOCTYPE html>
<html><head><style>
html, body{
width: 99%; /* to prevent scroll bar when unnessery */
height: 100%;
}
div#main_container{
position: absolute;
top: 50%; margin-top: -200px;
left: 50%; margin-left: -400px;
width: 800px; height: 400px;
border: 1px solid black;
}
</style></head><body>
<div id='main_container'>
Some content
</div>
</body></html>
PROBLEM: If I make window of the browser smaller then my content block, I can’t seen the top and left parts of the block – scrolling bars looks like I am on max of top\left of content, but no top and right of my block is seen.
Why does it happens and how to fix it?
I tested in 3 browsers, so it’s not browser issue.
It happens because it’s meant to happen. Let’s to the math for a browser with it’s height at 200px.
So, what’s 50% of 200px? datarara.. it’s 100px. That puts the top of
#main_containerat 100px in the y-axis. Give it a margin of -200px, and it goes up 200px. If it’s at 100px and it moves up 200px,#main_container‘s top gets placed at -100px. No wonder why part of it is hidden.The Solution
A solution would be to make the
position: absoluteon#main_containerrelative to thebodyelement and not the browser window (that is: setposition:relativeonbody). Then, by setting amin-heightandmin-widthonbodyequal to#main_container‘sheightandwidth(respectively), no matter how short the window would become, the body’s dimensions would never be smaller than#main_container– no possible “overflow”.Applying those styles to the
bodyelement, however, assumes you’ll only have on of those absolutely positioned boxes on your page. If this is not the case, then wrapping#main_containerwith it’s owndivwould probably be the most compatible solution. Here’s that solution:The HTML
The CSS