Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
568 views
in Technique[技术] by (71.8m points)

html - Div with 100% height overflowing body in Chrome

I'm trying to make 100% height flex's child as described here and here . In firefox and IE it's shown as expected, but in chrome it's messed up. The divs are getting out of the body. Is it yet another side effect of chrome following the spec to the tee or still a bug?

The code:

<!DOCTYPE html>
<html>
<head>
<style>
    html, body {
        width: 100%;
        height: 100%;
        margin: 0;
        padding: 0;
    }
    div {
        border: 1px solid black;
        box-sizing: border-box;
        margin: 0;
        padding: 0;
    }

    .red-border {
        border: 2px solid red;
    }

    .blue-border {
        border: 2px solid blue;
    }

    .green-border {
        border: 2px solid green;
    }
</style>
</head>
<body class="red-border" >
<div clsas="blue-border" style="display: flex; flex-direction: column; width: 100%; height: 100%;">
    <div  style="flex: 0 1 auto; ">top</content></div>

    <div class="green-border"  style="flex: 1 1 auto; display: flex; flex-direction: row; width: 100%; height: 100%;" >
        <div style="flex: 0 1 auto; ">left</div>
        <div style="flex: 1 1 auto; width: 100%; height: 100%;">
            <div style="background-color: #ff6500; width: 100%; height: 100%;">center</div>
        </div>
        <div style="flex: 0 1 auto; ">right</div>
    </div>

    <div style="flex: 0 1 auto; ">bottom</content></div>
</div>
</body>
</html>

plnkr: http://run.plnkr.co/plunks/wi5MQYK5yEdzdgQBaUWh/

that's how it looks in ff and ie (11): that's how it looks in ff

that's how it looks in chome: that's how it looks in chome

UPDATE: The 'center' div should be a non-flex div and still have to have 100% height. The reason is to have non-flex div is that my real site structure is quite complicated - it uses a lot of webcomponents and some of them just can't use flex. So at some point I have to stop using flex and have 'normal' div to have height 100%.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The problem seems to stem from the fact that you're combining both the height and flex-basis properties in the same declaration block. It appears to create a conflict. In my answer below I've removed the heights and used flex-basis instead. You'll note a couple of other adjustments, as well.


Change:

(1)

<div class="green-border" style="flex: 1 1 auto; display: flex; flex-direction: row; width: 100%; height: 100%;" >

TO

<div class="green-border" style="flex: 1 1 100%; display: flex; flex-direction: row; width: 100%;" >

notes: removed height; used flex-basis instead;


(2)

<div style="flex: 1 1 auto; width: 100%; height: 100%;">

TO

<div style="flex: 1 1 100%; width: 100%; display: flex;">

notes: removed height; used flex-basis instead; established nested flex container;


(3)

<div style="background-color: #ff6500; width: 100%; height: 100%;">center</div>

TO

<div style="background-color: #ff6500; width: 100%; flex: 1 1 100%;">center</div>

notes: removed height; used flex-basis instead; added flex property;


Lastly, the blue border wasn't showing due to a typo.

(4)

clsas="blue-border"

TO

class="blue-border"


With the adjustments above, the layout renders in Chrome like this:

enter image description here

All boxes lie within their containers.

DEMO


Applying 100% height to nested, non-flex elements

In most cases, when using percentage heights on child elements, a percentage height must also be specified for the parent element and all ancestor elements, up to and including the root element.

html, body { height: 100%; }

I have explained the reason for this here:

But a close look at the spec reveals an exception:

The percentage is calculated with respect to the height of the generated box's containing block. If the height of the containing block is not specified explicitly (i.e., it depends on content height), and this element is not absolutely positioned, the value computes to 'auto'.

Note the part: ... and the element is not absolutely positioned.

Hence, try this on the "center" box:

Change:

(5)

<div style="background-color: #ff6500; width: 100%; flex: 1 1 100%;">center</div>

TO

<div style="background-color: #ff6500; width: 100%; flex: 1 1 100%; position: relative;">
    <div style="border: 5px solid yellow; position: absolute; height: 100%; width: 100%;">
       center</div>
</div>

Notes:

  • added position: relative to establish nearest positioned ancestor for absolute positioning
  • created new, nested, non-flex div container
  • applied absolute positioning to new div with height: 100%.

With absolute positioning you do not need to apply percentage heights to parent containers.

DEMO


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...