1

OK so I was reading a bit about floats. If something is left floated the content that come after it floats around the floated element right:

HTML:

<div class='block pink float'></div>
  <div class='block blue float'></div>
  <div>Mr. Chairman, we have in this country one of the most corrupt institutions the world has ever known. I refer to the Federal Reserve Board and the Federal reserve banks. The Federal Reserve Board, a Government board, has cheated the Government of the United States and the people of the United States out of enough money to pay the national debt. The depredations and the iniquities of the Federal Reserve Board and the Federal Reserve banks acting together have cost this country enough money to pay the national debt several times over. ...

Some people think the Federal reserve banks are United States Government institutions. They are not Government institutions. They are private credit monopolies which prey upon the people of the United States for the benefit of themselves and their foreign customers, foreign and domestic speculator sand swindlers, and rich and predatory money lenders. ...

Those 12 private credit monopolies were deceitfully and disloyally foisted upon this country by bankers who came here from Europe and who repaid us for our hospitality by undermining our American institutions. Those bankers took money out of this country to finance Japan in a war against Russia.They created a reign of terror in Russia with our money in order to help that war along. They instigated the separate peace between Germany and Russia and thus drove a wedge between the Allies in the World War. ...

Every effort has been made by the Federal Reserve Board to conceal its power but the truth is the Federal Reserve Board has usurped the Government of the United States. ...

Mr. Chairman, when the Federal reserve act was passed the people of the United States did not perceive that a world system was being set up here which would make the savings of an American school-teacher available to a narcotic-drug vendor in Macao. They did not perceive that the United States was to be lowered to the position of a coolie country which has nothing but raw materials and heavy goods for export. That Russia was destined to supply man power and that this country was to supply financial power to an international superstate--a superstate controlled by International bankers and international industrialists acting together to enslave the world for their own pleasure.</div>
  <div class='block orange'></div>

CSS:

.float { float: left; }

.block {
    width: 200px;
    height: 200px;
}

.pink { background-color: #ee3e64 }
.blue { background-color: #44accf }
.green { background-color: #b7d84b }
.orange { background-color: #E2A741 }

Result is as I would expect: https://jsfiddle.net/vejxdn4z/

But now if I change to HTML to:

  <div class='block pink float'></div>
  <div class='block blue float'></div>
  <div class='block green'></div>
  <div class='block orange'></div>

I get this: https://jsfiddle.net/gseqx6w7/

Why are the pink and blue on top of the others and not acting the same as the first result?

Robert
  • 10,126
  • 19
  • 78
  • 130
  • Because you are restricting the width of the green and orange divs. You don't restrict the div's width in your first example. – dewd Mar 13 '16 at 01:12

2 Answers2

2

From MDN

text and inline elements will wrap around it.

All of your .block elements, because they are <div>s, they are block elements

Know that block elements normally are placed one under the other, like appropriately enough, blocks.

So what happens in your second fiddle is this:

The two floated .blocks are placed next to each other, outside of the normal flow, and the rest of the .blocks are placed like normal block elements, following the normal flow, that is, one under the other, as if the two floated .blocks did not exist.

This happens in your first fiddle as well, it's just that you do not see the actual block being below the two floated blocks, you only see the text which wraps around the floated blocks.

I added some colour so you can actually see that it happens in both cases:

https://jsfiddle.net/mkarajohn/vejxdn4z/1/

In your second fiddle, if you wanted the rest of the blocks to be placed next to the two floated blocks, you should add display: inline-block to their CSS

Dimitris Karagiannis
  • 8,942
  • 8
  • 38
  • 63
  • If you add text to the third div in the second example, the thrid div does not behave like the third div in the first example. It only does if you remove the width restriction from the third div. – dewd Mar 13 '16 at 01:27
  • @dewd It behaves exactly the same, only this time it is restricted to 200px of width. Not sure what you mean. – Dimitris Karagiannis Mar 13 '16 at 01:30
  • If the width of the 3rd element is restricted to less than that of the floated elements, its content appears below the first 2 elements, not next to it as in example 1. It is correct to state that element 3's box model ignores the floated element in its positioning. It's just the content does not. Content will appear next to or below the floated element, not behind it. – dewd Mar 13 '16 at 01:41
  • @dewd I don't think that we disagree on something. I mentioned what you say in my answer above: "text and inline elements will wrap around it." .Since the content is text, in this case, it does exactly that :) – Dimitris Karagiannis Mar 13 '16 at 01:46
0

When you place the "< div> ...TEXT TEXT TEXT < /div>" so the orange block is recognized an block element, so is float left bottom, and it's OK, it just need a blockin element to make that effect, so when you place a

<div class='block green'></div>

That make the same effect as the text, but with the difference that this time the orange is below the pink block element.

If you decide to remove the third div in each case you'll see that they both act the same way.

Lester Vargas
  • 370
  • 2
  • 6
  • 23