<div> tag disappears under a floated element, but a <p> tag or text does not?

bloodfire1004

Consider this line of code:

#bluediv {
  background-color: blue;
  width: 100px;
  height: 100px;
  float: left;
}
#greendiv {
  background-color: green;
  width: 100px;
  height: 100px;
}
<div>
  <div id="bluediv">
  </div>
  <div id="greendiv">
  </div>
</div>

You will notice that since #greendiv disappears under the floated #bluediv. At first, I thought it was because <div> is a block-element but when I tried <p>, which is also a block-element, it behaves as below:

#bluediv {
  background-color: blue;
  width: 100px;
  height: 100px;
  float: left;
}
#greendiv {
  background-color: green;
  width: 100px;
  height: 100px;
}
<div>
  <div id="bluediv">
  </div>
  <p>Paragraph</p>
  Normal text
  <div id="greendiv">
  </div>
</div>

It wraps around the floated #bluediv instead (along with the normal text)!

Why do they behave differently?

BoltClock

The paragraph itself does not wrap around the float. Only its text does, along with the bare text between the paragraph and #greendiv (which resides in an anonymous block box).

And that's why you'll also notice that #greendiv is shifted down — it's because of the addition of the paragraph, and the anonymous block box that contains the bare text.

If you make #bluediv translucent, put the bare text in its own block element that you can target with CSS, and make the boxes of both block elements visible (i.e. and not completely transparent against the background), you can see what's really happening:

#bluediv {
  background-color: rgba(0, 0, 255, 0.5);
  width: 100px;
  height: 100px;
  float: left;
}
#greendiv {
  background-color: green;
  width: 100px;
  height: 100px;
}
p, span {
  display: block;
  border: solid;
}
<div>
  <div id="bluediv">
  </div>
  <p>Paragraph</p>
  <span>Normal text</span>
  <div id="greendiv">
  </div>
</div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Getting a dynamic element under a div tag

From Dev

Adding a border to a P tag element under certain conditions in Sass

From Dev

Why does a div take the dimensions of a floated element?

From Dev

Why does a html div tag with a p tag inside it sit differently than one with an img tag inside it?

From Dev

How to I scrape text from the first sub div tag which is under a Main div tag

From Dev

Not able to select a text under "div" tag using selenium

From Dev

Find element by text in tag

From Dev

Replace <div> tag with <p> tag using php

From Dev

Why does an empty span tag in a floated parent render on a new line?

From Dev

Positioning div right under an a tag

From Dev

jQuery - How to move some tag element with text inside to closest div

From Dev

"<\div>" text inside <div> tag

From Dev

Run Function If Div Element Does Not Contain HTML Tag

From Dev

How to show a <tag> inside a text tag e.g <p>,<div>,etc?

From Dev

Making a div inline with <p> tag

From Dev

CSS: div p, a not applying to a tag

From Dev

javascript not updating the text of p tag

From Dev

Position div under floated div

From Dev

add a start div tag to the element and end div tag to another element

From Dev

click on div element inside an <a> tag

From Dev

setting text in a <div> tag in PHP

From Dev

Why does span tag under div disappear when jQuery refreshes the data div

From Dev

Placement of a div tag does not work

From Dev

Jsoup accessing div tag under table tag not working

From Dev

Write clickable xpath for entire text in different "span" tags but under one "p" tag

From Dev

Why does some of my text within html paragraph tag fall outside of the <p></p>when in browser

From Dev

HTML align text under image tag

From Dev

how to make <p> tag and <a> tag inside a <div> inline

From Dev

How to select <p> tag of a div tag using JSoup in android

Related Related

  1. 1

    Getting a dynamic element under a div tag

  2. 2

    Adding a border to a P tag element under certain conditions in Sass

  3. 3

    Why does a div take the dimensions of a floated element?

  4. 4

    Why does a html div tag with a p tag inside it sit differently than one with an img tag inside it?

  5. 5

    How to I scrape text from the first sub div tag which is under a Main div tag

  6. 6

    Not able to select a text under "div" tag using selenium

  7. 7

    Find element by text in tag

  8. 8

    Replace <div> tag with <p> tag using php

  9. 9

    Why does an empty span tag in a floated parent render on a new line?

  10. 10

    Positioning div right under an a tag

  11. 11

    jQuery - How to move some tag element with text inside to closest div

  12. 12

    "<\div>" text inside <div> tag

  13. 13

    Run Function If Div Element Does Not Contain HTML Tag

  14. 14

    How to show a <tag> inside a text tag e.g <p>,<div>,etc?

  15. 15

    Making a div inline with <p> tag

  16. 16

    CSS: div p, a not applying to a tag

  17. 17

    javascript not updating the text of p tag

  18. 18

    Position div under floated div

  19. 19

    add a start div tag to the element and end div tag to another element

  20. 20

    click on div element inside an <a> tag

  21. 21

    setting text in a <div> tag in PHP

  22. 22

    Why does span tag under div disappear when jQuery refreshes the data div

  23. 23

    Placement of a div tag does not work

  24. 24

    Jsoup accessing div tag under table tag not working

  25. 25

    Write clickable xpath for entire text in different "span" tags but under one "p" tag

  26. 26

    Why does some of my text within html paragraph tag fall outside of the <p></p>when in browser

  27. 27

    HTML align text under image tag

  28. 28

    how to make <p> tag and <a> tag inside a <div> inline

  29. 29

    How to select <p> tag of a div tag using JSoup in android

HotTag

Archive