Absolute bottom positioning in relative div
I want to position开发者_运维百科 "rightBottomPart" to the bottom of "rightPart" and I want "rightPart to be as high as "leftPart". The problem is that I don't know the height of the content in "leftPart" and therefore I can't set the height of "text". (Height in "text" would solve it)
Right now it looks like this:
and I want it to look like this:
My code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
</head>
<body style="margin: 0px 0px 0px 0px;">
<div id="headers" style="background-color: Olive; width: 300px; height: 50px;"></div>
<div id="text" style="background-color: Navy; position: relative; width: 300px;">
<div id="leftPart" style="background-color: Gray; width: 200px; float: left;">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
<div id="rightPart" style="background-color: Red; float: right;">
<div id="rightTopPart" style="background-color: Lime; position: absolute; right: 0px; top: 0px;">top</div>
<div id="rightBottomPart" style="background-color: Yellow; position: absolute; right: 0px; bottom: 0px;">bottom</div>
</div>
</div>
</body>
</html>
It looks right in IE7, but not in the rest of the browsers I've tested. If I remove the DOCTYPE-tag it also looks good in IE8, but still not in Google Chrome.
What am I missing?
Thanks Carl
To keep floats and position under control, you need to keep two things in mind: the position is absolute according to its parent element and usually needs dimensions, and floated objects do not have dimensions by default.
Since your test represents a simple two-column model, have a look at this nice overview here, it might clarify things up a little bit: equal height columns with css
So, the trick here is to give #text a float and pos:rel and then the #right*Part will know where they are positioned.
Have a look here:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<style>
body { margin: 0; }
#headers { background: Olive; width: 300px; height: 50px; }
#text { background: Navy; position: relative; width: 300px; display: block; float:left; }
#leftPart { background: Gray; width: 200px; float: left; display: inline-block; }
#rightPart { background: Red; }
#rightTopPart { background: Lime; position: absolute; right: 0; top: 0; }
#rightBottomPart { background: Yellow; position: absolute; right: 0; bottom: 0; }
</style>
</head>
<body>
<div id="headers"></div>
<div id="text">
<div id="leftPart">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
<div id="rightPart">
<div id="rightTopPart">top</div>
<div id="rightBottomPart">bottom</div>
</div>
</div>
</body>
</html>
Kind regards, mtness
精彩评论