【问题标题】:Firefox ignoring height: 100%Firefox 忽略高度:100%
【发布时间】:2016-02-24 08:14:12
【问题描述】:

我有以下适用于 chrome 和资源管理器的 sn-p,但是我似乎无法让 firefox 使左侧栏 .leftpanel 拉伸到父 .viewer 的完整高度

JSfiddle

片段

html,
body {
  border: 0;
  padding: 0;
  margin: 0;
  width: 100%;
  height: 100%;
}
ul {
  margin: 0;
  padding: 0;
}
li {
  display: inline-block;
  list-style-type: none;
}
.fullscreen {
  width: 100%;
  height: 100%;
  position: absolute;
  background-color: pink;
  display: table;
  z-index: 1;
}
.centeralign {
  vertical-align: middle;
  width: 100%;
  height: 100%;
  display: table-cell;
  background-color: blue;
  text-align: center;
  padding: 2em;
}
.viewer {
  text-align: left;
  display: inline-block;
  width: 90%;
  height: 90%;
  position: relative;
  background-color: green;
  padding: 1em;
  min-height: 35.45em;
}
.leftpanel {
  width: 15em;
  /*Alter depending on how much space this takes*/
  float: left;
  height: 100%;
  position: relative;
  background-color: yellow;
}
.leftpanel .slide {
  width: 15em;
  height: 9.706em;
  display: block;
  margin-bottom: 1em;
  background-color: red;
}
.leftpanel .comments_container {
  display: table;
  position: absolute;
  left: 0;
  bottom: 0;
  height: 14em;
  width: 15em;
  background-color: orange;
}
<ul class="fullscreen">
  <li class="centeralign">
    <ul class="viewer">
      <li class="leftpanel">
        <ul class="slide">
          <li class="slide_image">
          </li>
          <li class="slide_menu">
          </li>
        </ul>
        <ul class="slide">
          <li class="slide_image">
          </li>
          <li class="slide_menu">
          </li>
        </ul>
        <ul class="comments_container">
          <li class="comments">
            <span class="comment">Test</span>
          </li>
          <li class="new_comment">
            <form action="new_comment.php">
              <input type="text" name="comment" />
              <input type="submit" name="Send" />
            </form>
          </li>
        </ul>
      </li>
      <li class="viewport">
      </li>
      <li class="close">
        X
      </li>
    </ul>
  </li>
</ul>

【问题讨论】:

  • 您尝试过重置吗?如果没有,请捷克退出github.com/murtaugh/HTML5-Reset
  • 我其实有一个重置!但很可惜它并没有解决问题
  • 虽然我承认这不是我正在运行的重置
  • 您是否尝试从.viewer 中删除min-height: 35.45em;
  • 您的代码“过于复杂”。作为前端开发人员,我学到的一件事是,如果你混合绝对/相对定位,显示:inline-block/table/table-cell 并混合浮动,会发生意想不到的事情。

标签: html css


【解决方案1】:

很遗憾,我无法告诉您为什么 firefox 会这样做或导致此问题的原因,但是,我可以为您提供解决此问题的可能方法,其中不包括对您的代码进行太多更改。

解决方案很简单,在.viewer 元素上使用display:flex 而不是 display:inline-block 并给children,在本例中为.leftpanel 元素属性align-items:stretch

要了解更多信息,请查看this css-tricks guidethis MDN guide

您更新后的代码将如下所示:

HTML:没有任何改变。

CSS:

.viewer {
    text-align: left;
    display: flex; /* changed the display to flex */
    width: 90%;
    height: 90%;
    position: relative;
    background-color: green;
    padding: 1em;
    min-height: 35.45em;
}

.leftpanel {
    width: 15em; /*Alter depending on how much space this takes*/
    float: left;
    height: 100%;
    position: relative;
    background-color: yellow;
    align-items: stretch; /* Added the align-items property */
}

JSFiddle

希望这会有所帮助!

【讨论】:

  • 当您使用 flex 时,旧版本的 Internet Explorer 会发生什么情况?我还没有真正阅读过它,因为听起来它不适用于 IE11 以下的版本。尽管目前这似乎只影响了大约 2% 的用户......
  • @tomc 您可以使用modernizr 来检测是否存在弹性功能,并在必要时提供备用样式。 Modernizr 将在 html 元素中添加 flexboxno-flexboxflexbox-legacy 等类,阅读更多关于 herehere (modernizr) 的信息
  • @tomc 当然,只有当你真的必须达到最后的 2%
【解决方案2】:

乍一看,您的问题是您正在为您的父级.viewer 设置一个大的min-height,因此只需将其删除,因为您已经在另一个父级中将height 设置为90%

html,
body {
  border: 0;
  padding: 0;
  margin: 0;
  width: 100%;
  height: 100%;
}
ul {
  margin: 0;
  padding: 0;
}
li {
  display: inline-block;
  list-style-type: none;
}
.fullscreen {
  width: 100%;
  height: 100%;
  position: absolute;
  background-color: pink;
  display: table;
  z-index: 1;
}
.centeralign {
  vertical-align: middle;
  width: 100%;
  height: 100%;
  display: table-cell;
  background-color: blue;
  text-align: center;
  padding: 2em;
}
.viewer {
  text-align: left;
  display: inline-block;
  width: 90%;
  height: 90%;
  position: relative;
  background-color: green;
  padding: 1em;
  /* min-height: 35.45em; remove this line */
}
.leftpanel {
  width: 15em;
  /*Alter depending on how much space this takes*/
  float: left;
  height: 100%;
  position: relative;
  background-color: yellow;
}
.leftpanel .slide {
  width: 15em;
  height: 9.706em;
  display: block;
  margin-bottom: 1em;
  background-color: red;
}
.leftpanel .comments_container {
  display: table;
  position: absolute;
  left: 0;
  bottom: 0;
  height: 14em;
  width: 15em;
  background-color: orange;
}
<ul class="fullscreen">
  <li class="centeralign">
    <ul class="viewer">
      <li class="leftpanel">
        <ul class="slide">
          <li class="slide_image">
          </li>
          <li class="slide_menu">
          </li>
        </ul>
        <ul class="slide">
          <li class="slide_image">
          </li>
          <li class="slide_menu">
          </li>
        </ul>
        <ul class="comments_container">
          <li class="comments">
            <span class="comment">Test</span>
          </li>
          <li class="new_comment">
            <form action="new_comment.php">
              <input type="text" name="comment" />
              <input type="submit" name="Send" />
            </form>
          </li>
        </ul>
      </li>
      <li class="viewport">
      </li>
      <li class="close">
        X
      </li>
    </ul>
  </li>
</ul>

【讨论】:

  • 当浏览器调整到太小的高度时,可能没有设置最小高度导致子元素脱离viewer
  • 我想我不明白你的问题,但我会尽力向你解释..你的父母.viewer 有一个(最小)height 5000px(示例)但你的孩子@ 987654330@ 仅使用 height 的 1000 像素,因此从子节点的末尾到父节点的末尾向下 4000 像素因此,如果您删除 min-height,子节点的末尾将在父节点的末尾 height
【解决方案3】:

虽然您的问题已经得到解答,但这里是“通用”解决方案:

.clearfix:before,

.clearfix:after { content: ''; display: table }

.clearfix:after { clear: both }

您的viewer 类需要在 HTML 中添加众所周知的“clearfix”,请在 Google 上搜索以获取正确解释。修改您的查看器 HTML &lt;ul class="viewer clearfix"&gt; 并将该类添加到您的 global CSS(用于一般用途)中,您就可以开始了。

【讨论】:

  • 谢谢你!我改变了我接受的答案,因为你的解决方案对我来说效果更好,让我保持最小高度!
  • 随时准备……,你知道的。这是一个不断出现的令人讨厌的小问题,请注意这一点!深入研究 flexbox,非常值得您花时间。
  • 检查我的codepen,这不是最简单的例子,但非常精致(并且响应迅速)......
  • 我错过了leftpanel 上的float 属性,浮动总是会导致这个问题。
猜你喜欢
  • 2015-12-20
  • 1970-01-01
  • 2021-04-01
  • 2013-08-10
  • 1970-01-01
  • 1970-01-01
  • 2018-08-20
  • 1970-01-01
  • 2011-01-06
相关资源
最近更新 更多