0

https://jsfiddle.net/0k02qsjw/1/

.left1 {

background-color:red;
width:100px;
height:100px;
float:left;

}
.left2 {

background-color:blue;
width:100px;
height:100px;


}

//left3 left4 similar to left2. fiddle

<div class="left1">Div1</div>
<div class="left2">Div2child</div>
<div class="left3">Div3</div>
<div class="left4">Div4</div>

I'm learning float and clear in css and am stuck on one problem. Float works as expected in the above example when I float each div individually. When I float left1, left2 moves up as expected. But why is it that children on left2 not move up?

I understand that floated elements are out of flow -> may that's why. If so, why does overflow:hidden on left2 work as if left2 was floated(see below).
https://jsfiddle.net/0k02qsjw/2/

I need help in understanding what is happening in this case. This could be a duplicate of

Floating elements within a div, floats outside of div. Why?

But i don't find any answer to my specific question there.

Community
  • 1
  • 1
stackMan10
  • 732
  • 6
  • 25

1 Answers1

2

https://developer.mozilla.org/en-US/docs/Web/CSS/float

The float CSS property specifies that an element should be taken from the normal flow and placed along the left or right side of its container, where text and inline elements will wrap around it.

So when you float an element, other block elements don't float around it - the inline and text elements in a neighboring element float around it.

If you're floating .left1, the content in .left2 doesn't go up and wrap around .left1 because they're the same width. .left2 needs to be wider than .left1 for it's inline and text content to be able to wrap around .left1. You can see this by setting the width of .left2 to 200px. https://jsfiddle.net/0k02qsjw/3/

Michael Coker
  • 52,626
  • 5
  • 64
  • 64