31

フレックスボックス ベースのレイアウトを使用して、ページの固定フッターを取得しようとしています。これは Chrome と Firefox ではうまく機能しますが、IE11 ではメイン コンテンツの直後にフッターが配置されます。つまり、メイン コンテンツは、利用可能なすべてのスペースを埋めるために引き伸ばされません。

body {
    border: red 1px solid;
    min-height: 100vh;
    display: -ms-flexbox;
    display: -webkit-flex;
    display: flex;
    -ms-flex-direction: column;
    -webkit-flex-direction: column;
    flex-direction: column;
}
header, footer  {
    background: #dd55dd;
}
main {
    background: #87ccfc;
    -ms-flex: 1 0 auto;
    -webkit-flex: 1 0 auto;
    flex: 1 0 auto;
}
<body>
    <header role="banner"><h1> .. </h1></header>
    <main role="main">
        <p>.....</p>
    </main>
    <footer>...</footer>
</body>

vhコンテナーの高さの単位がIE で測定されている場合、フレックス レイアウトでメイン要素を伸ばすにはどうすればよいですか? この動作が IE がフレックスボックス仕様を実装する方法のバグの結果であるかどうかを調べていましたが、この問題についての言及は他に見つかりませんでした。

JSFiddleデモ

4

4 に答える 4

32

問題はvh単位ではありませんが、min-height

半機能の CSS のみのソリューションを見つけました。

min-height: 100vh;
height: 100px;

エクストラheightを使用すると、コンテンツの高さが十分でない場合でも、IE が画面を垂直方向に埋めることができます。欠点は、コンテンツがビューポートよりも長い場合、IE がコンテンツをラップしなくなることです。


これでは不十分なので、JS で解決策を作成しました。

検出

この関数はバグをテストします: バグがあるtrueことを意味します。

function hasBug () {
    // inner should fill outer
    // if inner's height is 0, the browser has the bug

    // set up
    var outer = document.createElement('div');
    var inner = document.createElement('div');
    outer.setAttribute('style', 'display:-ms-flexbox; display:flex; min-height:100vh;');
    outer.appendChild(inner);
    (document.body || document.documentElement).appendChild(outer);

    // test
    var bug = !inner.offsetHeight;

    // remove setup
    outer.parentNode.removeChild(outer);

    return bug;
}

修理

修正はheight、要素の を手動で設定することで構成されますpx

function fixElementHeight (el) {
    // reset any previously set height
    el.style.height = 'auto'; 

    // get el height (the one set via min-height in vh)
    var height = el.offsetHeight; 

    // manually set it in pixels
    el.style.height = height + 'px'; 
}

要素の高さは、そのコンテンツの高さに正確に設定されます。CSS のみのソリューションで見られる動作を使用して、IE でheightセカンダリとして使用されます。min-height

使用法

これら 2 つの関数を定義したら、次のように設定します。

if(hasBug()) {
    // fix the element now
    fixElementHeight(el);

    // update the height on resize
    window.addEventListener('resize', function () {
        fixElementHeight(el);
    });
}

デモ

function hasBug () {
    // inner should fill outer
    // if inner's height is 0, the browser has the bug

    // set up
    var outer = document.createElement('div');
    var inner = document.createElement('div');
    outer.setAttribute('style', 'display:-ms-flexbox; display:flex; min-height:100vh;');
    outer.appendChild(inner);
    (document.body || document.documentElement).appendChild(outer);

    // test
    var bug = !inner.offsetHeight;

    // remove setup
    outer.parentNode.removeChild(outer);

    return bug;
}

function fixElementHeight (el) {
    // reset any previously set height
    el.style.height = 'auto'; 

    // get el height (the one set via min-height in vh)
    var height = el.offsetHeight; 

    // manually set it in pixels
    el.style.height = height + 'px'; 
}

var output = document.getElementById('output');
output.innerHTML = hasBug()?'Browser is buggy':'Browser works correctly';


var el = document.getElementById('flex');

if(hasBug()) {
  // fix the element now
  fixElementHeight(el);

  // update the height on resize
  window.addEventListener('resize', function () {
    fixElementHeight(el);
  });
}
.half-screen {
  display:-ms-flexbox;
  display: flex;
  min-height: 50vh;

  padding: 10px;
  background: #97cef0;
}


.content {
  padding: 10px;
  background: #b5daf0;
}
The inner box should fill the outer box vertically, even if the browser is buggy.
<div class="half-screen" id="flex">
  <div class="content" id="output">
    Text
  </div>
</div>

于 2015-04-14T08:49:00.467 に答える
-1

通常、これは役立ちます:

html, body {
   height: 100%;
}

vh..単位を使用する必要はありません。

完全なサンプル:

* {
  margin: 0;
}
html,
body {
  height: 100%;
}
body {
  border: red 1px solid;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  -ms-flex-direction: column;
  -webkit-flex-direction: column;
  flex-direction: column;
}
header,
footer {
  background: #dd55dd;
}
main {
  background: #87ccfc;
  -ms-flex: 1;
  -webkit-flex: 1;
  flex: 1;
  overflow-y: scroll;
  -webkit-overflow-scrolling: touch;
}
<header>
  <h1>Herons - How they plan to eat your eyes!</h1>
</header>
<main>
  Herons are foul and devious birds.
</main>
<footer>
  <small>© 2014 Institute for the prevention of Herons</small>
</footer>

于 2015-07-20T09:45:00.460 に答える