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

J.Joe

I don't understand why the div#footer only recognizes the space taken by div#navigation{float:right}.

Since float elements are taken out of the normal flow, div#footer should ignore both float:right and float:left elements.

I am not trying to add clear:both to div#footer. I am just curious how it happened.

body {
    margin: 0;
    padding: 0;
    background: #ccc;
}
#container {
    width: 600px;
    margin: 0 auto;
}
#header {
    padding: 30px;
    background: #bbb;
}
#content {
    float: left;
    width: 460px;
    background: #fff;
}
#navigation {
    float: right;
    width: 140px;
    height: 200px;
    background: #eee;
}
#footer {
    background: #aaa;
    padding: 10px;
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="en">
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8">
        <title>CSS Floats 101</title>

    </head>
    <body>
        <div id="container">
            <div id="header">
                <h1>Header</h1>
            </div>
            <div id="content">
                <p>
                    Proin vel ante a orci tempus eleifend ut et magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus luctus urna sed urna ultricies ac tempor dui sagittis. In condimentum facilisis porta. Sed nec diam eu diam mattis viverra. Nulla fringilla, orci ac euismod semper, magna diam porttitor mauris, quis sollicitudin sapien justo in libero. Vestibulum mollis mauris enim. Morbi euismod magna ac lorem rutrum elementum. Donec viverra auctor lobortis. Pellentesque eu est a nulla placerat dignissim. Morbi a enim in magna semper bibendum.
                </p>
            </div>
            <div id="navigation">
                navigation
            </div>
            <div id="footer">
                Footer
            </div>
        </div>
    </body>
</html>

I dont understand why the div#footer only recognizes the space taken by div#navigation{float:right}.

Since float elements are taken out of the normal flow, div#footer should ignore both float:right and float:left elements.

TylerH

When elements are floated, they are removed from the normal flow of the document. To understand it completely, that means you need to think about elements being placed in the normal flow, first, and then you can think about removing them with the float property.

Because the #navigation and #content elements are both floated, they are removed from the normal document flow. This moves the #footer element up to the top of the #container element. It's effectively positioned 'underneath' the two floated elements now.

The reason you're seeing the "footer" text still is that all un-cleared text wraps around floated elements (remember, float was designed to be used for wrapping article text around embedded images, like in a magazine article). In this case, the full width of the text's container is filled from side-to-side with floated elements, so the text is pushed to a new line.

It's hard to intuit from your example, so take a look with this one:

body {
    margin: 0;
    padding: 0;
    background: #ccc;
}
#container {
    width: 600px;
    margin: 0 auto;
}
#header {
    padding: 30px;
    background: #bbb;
}
#content {
    float: left;
    width: 460px;
    height: 190px;
    background: #fff;
}
#navigation {
    float: right;
    width: 140px;
    height: 220px;
    background: #eee;
}
#footer {
    background: #aaa;
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="en">
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8">
        <title>CSS Floats 101</title>

    </head>
    <body>
        <div id="container">
            <div id="header">
                <h1>Header</h1>
            </div>
            <div id="content">  
                Content
            </div>
            <div id="navigation">
                navigation
            </div>
            <div id="footer">
                Curabitur feugiat feugiat purus. Aenean eu metus. Nulla facilisi. Veni quis justo vel massa suscipit sagittis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. Quisque mollis, justo vel rhoncus aliquam, urna tortor varius lacus, ut tincidunt metus arcu vel lorem. 
            </div>
        </div>
    </body>
</html>

As you can see, the text still wraps around the bottom of the #navigation element, too.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why does the div following a floated div overlap the floated one?

From Dev

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

From Dev

Why doesn't a fixed-width element take up the space beside a floated element?

From Dev

Floated Element applies background to other div as well

From Dev

floated element clipped by absolute position div

From Dev

Why does a table not clear a float when it can't fit next to the floated element?

From Dev

Why does a table not clear a float when it can't fit next to the floated element?

From Dev

Why does an svg with viewbox of different ratio to dimensions renders element in center?

From Dev

Why does transform break the dimensions of an element with position:fixed?

From Dev

Why doesn't a left-floated element go all the way up in the presence of right-floated elements?

From Dev

Margin collapsing with floated element, why there is an extra margin added?

From Dev

Bootstrap 3.2 popover does not position properly when target element is floated

From Dev

why does it take priority

From Dev

Why is floated div being blocked from wrapping to container edge?

From Dev

floated DIV's with selects creates empty columns in layout, why?

From Dev

floated DIV's with selects creates empty columns in layout, why?

From Dev

Why is this floated div being pulled down by the margin its sibbling?

From Dev

Floated element dropping below another floated element

From Dev

Why does the access time for the first element of a data.frame depends on its dimensions?

From Dev

Why does applying `inline-block` force dimensions onto my span element?

From Dev

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

From Dev

Why does a content editable div fixed on given dimensions still overflow if the text is long enough?

From Dev

How to specify the dimensions for an element to take the full window in HTML

From Dev

How do I make floated DIV's take up the horizontal space of their container?

From Dev

vertically align for floated element

From Dev

Bootstrap Popover on Floated Element

From Dev

Vertically align a floated element

From Dev

Bootstrap Popover on Floated Element

From Dev

vertically align for floated element

Related Related

  1. 1

    Why does the div following a floated div overlap the floated one?

  2. 2

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

  3. 3

    Why doesn't a fixed-width element take up the space beside a floated element?

  4. 4

    Floated Element applies background to other div as well

  5. 5

    floated element clipped by absolute position div

  6. 6

    Why does a table not clear a float when it can't fit next to the floated element?

  7. 7

    Why does a table not clear a float when it can't fit next to the floated element?

  8. 8

    Why does an svg with viewbox of different ratio to dimensions renders element in center?

  9. 9

    Why does transform break the dimensions of an element with position:fixed?

  10. 10

    Why doesn't a left-floated element go all the way up in the presence of right-floated elements?

  11. 11

    Margin collapsing with floated element, why there is an extra margin added?

  12. 12

    Bootstrap 3.2 popover does not position properly when target element is floated

  13. 13

    why does it take priority

  14. 14

    Why is floated div being blocked from wrapping to container edge?

  15. 15

    floated DIV's with selects creates empty columns in layout, why?

  16. 16

    floated DIV's with selects creates empty columns in layout, why?

  17. 17

    Why is this floated div being pulled down by the margin its sibbling?

  18. 18

    Floated element dropping below another floated element

  19. 19

    Why does the access time for the first element of a data.frame depends on its dimensions?

  20. 20

    Why does applying `inline-block` force dimensions onto my span element?

  21. 21

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

  22. 22

    Why does a content editable div fixed on given dimensions still overflow if the text is long enough?

  23. 23

    How to specify the dimensions for an element to take the full window in HTML

  24. 24

    How do I make floated DIV's take up the horizontal space of their container?

  25. 25

    vertically align for floated element

  26. 26

    Bootstrap Popover on Floated Element

  27. 27

    Vertically align a floated element

  28. 28

    Bootstrap Popover on Floated Element

  29. 29

    vertically align for floated element

HotTag

Archive