Let's start by adding the different properties one by one. Initially if we don't set margin and float we will have the 3 box below each other like this:
body {
margin: 0;
padding: 0;
}
.square {
width: 50px;
height: 50px;
}
#red {
background-color: red;
}
#green {
background-color: green;
}
#blue {
background-color: blue;
}
<div class="square" id="red"></div>
<div class="square" id="green"></div>
<div class="square" id="blue"></div>
Until now the result is logical. Now let's add float to our elements:
body {
margin: 0;
padding: 0;
}
.square {
width: 50px;
height: 50px;
}
#red {
background-color: red;
float: left;
}
#green {
background-color: green;
float: right;
}
#blue {
background-color: blue;
}
<div class="square" id="red"></div>
<div class="square" id="green"></div>
<div class="square" id="blue"></div>
The green box is floated on the right (logical result) and the red box is floated on the left AND above the blue box so this one is hidden. This behavior is because:
The float CSS property specifies that an element should be placed
along the left or right side of its container, allowing text and
inline elements to wrap around it. The element is removed from the
normal flow of the web page, though still remaining a part of the flowref
Since the blue box is a block element and belong to the same container it won't wrap around the floated element; thus the floated element (red box) will go above it.
Now let's add margin to the blue box (non-floated element)
body {
margin: 0;
padding: 0;
}
.square {
width: 50px;
height: 50px;
}
#red {
background-color: red;
float:left;
}
#green {
background-color: green;
float:right;
}
#blue {
background-color: blue;
margin:50px;
}
<div class="square" id="red"></div>
<div class="square" id="green"></div>
<div class="square" id="blue"></div>
We see that all the elements are pushed down by 50px and the blue box pushed to the left by 50px.
Why this?
Here we are facing a margin-collapsing behavior with the blue box and body. So the margin-top of the blue box is collpased with the margin-top of body because the blue box is the first inflow child of the body; thus all the elements are pushed down since all of them are contained in the body. The blue box is logiaclly pushed from the left with the margin-left.
Now we add the margin to our floated element and these ones will be pushed from their previous position from top and left (for the red)/right (for the green):
body {
margin: 0;
padding: 0;
}
.square {
width: 50px;
height: 50px;
margin:50px;
}
#red {
background-color: red;
float:left;
}
#green {
background-color: green;
float:right;
}
#blue {
background-color: blue;
}
<div class="square" id="red"></div>
<div class="square" id="green"></div>
<div class="square" id="blue"></div>
Now in order to avoid the behavior explained above you need to clear float and avoid marign-collapsing:
body {
margin: 0;
padding: 0;
}
.square {
width: 50px;
height: 50px;
margin:50px;
}
#red {
background-color: red;
float:left;
}
#green {
background-color: green;
float:right;
}
#blue {
background-color: blue;
clear:both;/*clear float and this will also avoid margin collapsing with body*/
}
<div class="square" id="red"></div>
<div class="square" id="green"></div>
<div class="square" id="blue"></div>
And if you only avoid the margin-collapsing behavior you will have this output:
body {
margin: 0;
padding: 1px; /*avoid margin collapsing with body*/
}
.square {
width: 50px;
height: 50px;
margin:50px;
}
#red {
background-color: red;
float:left;
}
#green {
background-color: green;
float:right;
}
#blue {
background-color: blue;
}
<div class="square" id="red"></div>
<div class="square" id="green"></div>
<div class="square" id="blue"></div>