Reputation: 35
Why do they render differently depending on the order in which they're marked-up? It looks like i want it to when I put "thirdDiv" first.
My html looks something like this:
<div id="container">
<div id="firstDiv">1st</div>
<div id="secondDiv">2nd</div>
<div id="thirdDiv">3rd</div>
</div>
My css looks something like this:
body{ width: 100px; margin: 0px auto; color: white; }
#container{ width: 100px; height:60px; }
#firstDiv{ position: relative; width:60px; height: 40px; background: blue; }
#secondDiv{ position: relative; width: 60px; height:20px; background-color:red; }
#thirdDiv{ float:right; width:40px; height:60px; background-color: yellowgreen; }
(incorrect) http://jsfiddle.net/3B7xQ/
vs.
(correct) http://jsfiddle.net/EQSa5/
Upvotes: 1
Views: 2505
Reputation: 7359
Basically the reason why this is happening is because when an element floats it removes it self out of the flow of the document (from it's current position than moving the specified direction) allowing other elements to move up into the space around the floated element. In your case the third element removed itself from it's current position moved to the right but there was nothing under it that would move around it.
Here it is with #thirdDiv float: none
http://jsfiddle.net/EQSa5/2/
Here it is with #thirdDiv float: right
http://jsfiddle.net/EQSa5
Notice how it moves from its current spot to the right and lets the things below it fall in place.
Here it is with #thirdDiv float: none
http://jsfiddle.net/3B7xQ/1/
Here it is with #thirdDiv float: right
http://jsfiddle.net/3B7xQ
Same as last time notice how it moves from its current spot to the right and lets the things below it fall in place, but in this case there is nothing below it.
Here it is with #thirdDiv float: none
http://jsfiddle.net/qELaZ/1/
Here it is with #thirdDiv float: right
http://jsfiddle.net/qELaZ/
Hope that helped. I also left an article that could help you read up on it a little more. :)
Here is an article explaining that exact case as well. (It's a good article on understanding floats and the whole thing is worth the read.) http://alistapart.com/article/css-floats-101
Specifically there is this part of that article titled float first. http://alistapart.com/article/css-floats-101#section5
Upvotes: 1
Reputation: 139
Thats just because of the float element you have as part of the CSS for the green div. The "float-right" makes the div go to the right of the page, depending on its vertical position within that page.
Upvotes: 1