Forum Moderators: not2easy
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title></title>
<style type="text/css">
/*<![CDATA[*/
body, div { margin: 0; padding: 0 }
body { text-align: center }
#mainWrapper{ background-color: #ccccee; width: 600px; margin: 0 auto; }
#header { position: relative; background-color: #eeeeee }
#divLeft, #divRight { position: absolute; height: 200px; width: 200px; }
#divLeft { right: 600px; top: 0px; background-color: red }
#divRight { left: 600px; top: 0px; background-color: green }
/*]]>*/
</style>
</head>
<body>
<div id="mainWrapper">
<div id="header">
<div id="divLeft">Left box</div>
<div id="divRight">Right box</div>
<p>Some text.</p>
</div>
</div> <!-- endDiv mainWrapper-->
</body>
</html>
divLeft and divRight are inside the wrapper because the wrapper is centered. Think of it like a T, where the crossbar is made of the L and R divs and the upright part is the centered div that they are relative to.
So if the centered relative part is 600px wide and I put an absolute div with "right: 600px" inside it, the absolute div will rest against the left side of the centered div. The browser ignores it and there are no scrollbars until the window is less than 600px.
Note also that mainWrapper stays at 600px width, showing that the absolute positioned divs inside it have no effect on it's size, as expected.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title></title>
<style type="text/css">
/*<![CDATA[*/
body, div { margin: 0; padding: 0 }
body { text-align: center }
#mainWrapper{ background-color: #ccccee; width: 1000px; margin: 0 auto; }
#header { float: left; background-color: #eeeeee; width: 600px; }
#divLeft, #divRight { height: 200px; width: 200px; }
#divLeft { float:left; top: 0px; background-color: red }
#divRight { float: right; top: 0px; background-color: green }
/*]]>*/
</style>
</head>
<body>
<div id="mainWrapper">
<div id="divLeft">Left box</div>
<div id="header">Some text</div>
<div id="divRight">Right box</div>
</div>
</body>
</html>
[edited by: tedster at 9:03 pm (utc) on May 1, 2010]
#header { position: relative; background-color: #eeeeee }
#divLeft, #divRight { position: absolute; height: 200px; width: 200px; }
#divLeft { right: 600px; top: 0px; background-color: red }
#divRight { left: 600px; top: 0px; background-color: green } #header { position: relative; background-color: #eeeeee; }
#main {position: absolute; top: 0px; left: -300px; background-color:#eeeeee; }
#divLeft, #divRight { position: absolute; height: 200px; width: 200px; }
#divLeft { right: 600px; top: 0px; background-color: red; }
#divRight { left: 600px; top: 0px; background-color: green; } #main {position: absolute; top: 0px; left: -300px; background-color:#eeeeee; } #divLeft, #divRight { position: absolute; right: 600px; top: 0px; background-color: red; height: 200px; width: 200px; } #divLeft, #divRight { position: absolute; height: 200px; width: 200px; }
#divLeft { right: 600px; top: 0px; background-color: red }