【问题标题】:HTML Flex layout looks different in Internet ExplorerHTML Flex 布局在 Internet Explorer 中看起来不同
【发布时间】:2021-05-04 04:22:52
【问题描述】:

我正在尝试让一个简单的 flex 布局在 IE11 中与在 Chrome 中一样工作。

在 Chrome 中它看起来像这样:

但是在 IE11 中它看起来像这样:

代码如下:

html,
body {
  margin: 2px 0 0 0;
  padding: 0;
}

.wrap {
  display: flex;
  min-height: calc( 100vh - 8px);
  flex-direction: column;
  max-width: 1200px;
  margin: auto;
  border: 1px solid #222222;
}

.main {
  flex: 1 1 auto;
  display: flex;
}

header {
  background: green;
  padding: 10px;
}

#footer {
  background: green;
  padding: 10px;
}

.sidebar {
  background: blue;
  flex: 0 0 135px;
  padding: 10px;
}

.content {
  background: yellow;
  padding: 10px;
  flex: 1 1 auto;
}

@media screen and (max-width:680px) {
  .sidebar {
    flex: 0;
    order: 2
  }
  .main {
    flex-direction: column;
  }
}
<body translate="no">


  <div class="wrap">


    <header>

      <div class="header-inner-left">
      </div>

      <div class="header-inner">
      </div>

    </header>

    <main class="main">


      <div class="sidebar">

      </div>



      <div class="content">


      </div>


    </main>



    <div id="footer">
      <div class='footerleft'>

      </div>
      <div class='footerright'>

      </div>
      <div style="clear: both;"></div>
    </div>



  </div>

</body>

显示 HTML 和 CSS。 有没有办法让这个看起来一样。

如果我设置.wrap,那么它使用:

height: calc( 100vh -  8px );

然后加载页面看起来是正确的,但我需要内容能够正确增长到 .wrap 之外,所以设置min-height: calc( 100vh - 8px );

有没有办法做到这一点? 谢谢

【问题讨论】:

  • 如果您可以使用任何替代代码在不同的浏览器中生成一致的输出,那么您可以参考example。它与您的设计相似,您可以进一步尝试根据自己的要求对其进行修改。它可以帮助您避免 IE 浏览器的问题。

标签: html css internet-explorer flexbox css-calc


【解决方案1】:

你真的需要display: flex 上的.wrap 吗?因为在https://caniuse.com/?search=calc 上,您会发现“据报道,IE 和 Edge 不支持 'flex' 内的calc。”

因此,如果您改用display: block,它可能适用于 IE。在 .wrap 的 CSS 规则中,我看到您有 flex-direction: column;,但该规则中没有其他设置会对使用 display: block 产生实际影响。

【讨论】:

  • 嗨,我试过了,但似乎没有帮助。有什么想法吗?
【解决方案2】:

这里有一个可能对你有用的想法:

body {
  display: flex;
}

.wrap {
  min-height: 100vh;
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  max-width: 1200px;
  border: 1px solid #222222;
}

.main {
  flex: 1 1 auto;
  display: flex;
}

header {
  background: green;
  flex: 0 0 25px;
}

#footer {
  background: green;
  flex: 0 0 25px;
}

.sidebar {
  background: blue;
  flex: 0 0 135px;
  padding: 10px;
}

.content {
  background: yellow;
  padding: 10px;
  flex: 1 1 auto;
}

body {
  margin: 0;
}

* {
  box-sizing: border-box;
}
<div class="wrap">
  <header>
    <div class="header-inner-left"></div>
    <div class="header-inner"></div>
  </header>
  <main class="main">
    <div class="sidebar"></div>
    <div class="content"></div>
  </main>
  <div id="footer">
    <div class='footerleft'></div>
    <div class='footerright'></div>
    <div style="clear: both;"></div>
  </div>
</div>

【讨论】:

  • 感谢您查看此内容。这样做的原因是它填满了屏幕,但将.wrap 设置为`height: 100vh;` 阻止它在更长的页面上扩展。将其设置为 height: 100vh; 可以解决此问题,但当内容不再是单个页面时,它就会停止。有什么办法可以解决吗?
  • 尝试添加这个:body { display: flex; }, .wrap { min-height: 100vh; flex-grow: 1 }.
  • 添加这似乎可以解决显示高度问题,但会将页面对齐到屏幕的左侧,它曾经是居中的。无论如何也要居中?谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-13
  • 1970-01-01
  • 2019-03-03
  • 2014-12-09
  • 2014-10-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多