<div id="container">
<div id="top"></div>
<div id="left"></div>
<div id="right"></div>
<div id="clear"></div>
</div>
#container{
width:200px;
margin-left:auto;
margin-right:auto;
margin-top:50px;
}
#top{
width:200px;
height:20px;
border:medium ridge #FFF;
}
#left{
float:left;
width:50px;
height:20px;
border:medium ridge #FFF;
}
#right{
float:right;
width:40px;
height:20px;
border:medium ridge #FFF;
}
#clear{
clear:both;
}
Why the #right and #top are not right aligned?
Its because the
topelement is actually overflowing the bounds of the container, while the floated elementrightis being restricted to it. Thetopelement is overflowing thecontainerbecause the border is not included in the width. Sotopis actually occupying 204px.Problem Illustrated via Example: http://jsfiddle.net/KhJ6e/2/
To fix, adjust
topto account for the 2px border on each side. (subtract 4 from width of container) or specify width asautodepending on your intentions.Example: http://jsfiddle.net/KhJ6e/1/