【问题标题】:100% min-height working in Opera, but not in other browsers100% 最小高度在 Opera 中工作,但在其他浏览器中不行
【发布时间】:2013-08-05 23:14:14
【问题描述】:

这是我正在使用的代码:

html,body{
  height: 100%;
}

div{
  min-height: 100%;
  height: auto !important;
  height: 100%;                /* this is for older IE */
}

http://jsfiddle.net/YYcLJ/

这也失败了(但在 Opera 中有效):

div{
  min-height: 100%;
  height: auto;
}

所以 100% 的高度应该应用到所有 div 直到 html。但这仅发生在第一个 DIV :(

在 Opera 中它可以正常工作。第一个 DIV 是 body 的 100%,第二个是第一个 div 的 100%,依此类推...

有没有什么方法可以让其他浏览器也能正常工作?

【问题讨论】:

  • 你可以给一个想要的快照
  • 页面应该是完全红色的(树中最后一个 div 的 bg)...
  • 我希望最小高度是屏幕的 100%。 然后你应该删除 height:auto。这将使屏幕达到最小高度,在您的情况下为 100%。 实际高度可能更大,取决于内容 然后你不应该删除height:auto,因为如果内容必须决定和填充高度,应该存在自动高度。很抱歉,但你的陈述正在制造矛盾。如果以上几点没有解决您的问题,请提供您想要的屏幕截图。 - @亚历克斯
  • Alex @NathanLee 是对的!

标签: html css cross-browser height hierarchy


【解决方案1】:

为此,您只需要提供div 即可拥有100%;height 以使其工作。 另外,你的层次结构的方式,你也需要改变你的divsdisplay

此解决方案适用于跨浏览器。你可以查一下。

这里是WORKING SOLUTION

HTML:

<div class="d1">
    <div class="d2">
        <div class="d3">
            hi
        </div>
    </div>
</div>    

CSS:

html,body{
     height: 100%; /* Change 01. Div with height of 100% */
}

div{
  height: 100%;
}

.d1{
    background: #3cc;
    display:table; /* Change 02. Changing display of Divs */
    width:100%;
    height:100%;
}

.d2{
    background: #ddd;
    display:table-row; /* Change 03. Changing display of Divs */
}

.d3{
  background: #c00;
  display:table-cell; /* Change 04. Changing display of Divs */

}

我希望这就是你要找的。​​p>

【讨论】:

  • 似乎有效,但我认为我不能在实际标记中使用 display: table。似乎刹车很多东西..
  • 休息??你能详细说明什么中断了吗??
  • 我不确定这就是 Alex 想要的。当我在第二个 div 下添加内容时,它并排分成两个单独的 div 元素。我以为她希望它们都是水平的。
  • 我建议你也检查一下 OP 的要求,因为她有一个 min-height IE7 或更低版本不支持。所以我认为她没有看 IE7 或更低版本,仅供参考,OP 以及所有其他回答的人完全不支持 IE7 或更低版本。如果你想要那些浏览器的东西,我建议使用 、、
    ,因为它们最适合 IE7 或更低版本。我希望我已经澄清了你的观点。 - @apaul34208
  • 我想我们可能偶然发现了XY Problem
【解决方案2】:

正如specification 所说

如果没有明确指定包含块的高度(即,它取决于内容高度),并且该元素不是绝对定位的,则百分比值被视为'0'(对于'min-height')或“无”(代表“最大高度”)。

所以在这种情况下 Opera/Presto 的行为是错误的。请注意,Opera 15+ 的行为正确。

CSS3 draft 允许使用相对于视口的单位。所以在这种特殊情况下,您可以设置

div {
    height: auto;
    min-height: 100%;  /* fallback for browsers that doesn't support "vh" */
    min-height: 100vh; /* vh == 1% of viewport height */
}

【讨论】:

  • 我会说规范是错误的:) 当我添加 min-height:100% 我希望元素具有父容器的最小 100% 高度:|。无论如何,谢谢,您使用 vw 的解决方案似乎是唯一有效的解决方案:D
【解决方案3】:

尝试替换:

height: auto !important;

到:

height: inherit !important;

在这种情况下它可以工作,但它仍然使用高度属性设置 div 的大小而不是最小高度。

【讨论】:

    【解决方案4】:

    小提琴:http://jsfiddle.net/YYcLJ/5/

    我会在div 元素上使用position: absolute。然后你会得到想要的结果。

    div{
      position: absolute;
      min-height: 100%;
      height: auto !important;
      height: 100%;
      width: 100%;
    }
    

    这是可行的,因为绝对定位将div 元素从文档流中取出,从而允许它们的高度与指定的一样。由于我们没有声明div 元素的topbottom,因此它们的顶部位置将与它们的位置是静态的相同。

    http://www.w3.org/TR/CSS2/visudet.html#abs-non-replaced-height

    【讨论】:

      【解决方案5】:

      我想我们这里可能有一点XY Problem。看起来您已尝试实现 min-height 的解决方法以使其在古代 Internet Explorer 中运行,但该解决方法不适用于其他浏览器。

      为了获得所需的跨浏览器 min-height 尝试在 .d3 上使用 height:auto; 并为 IE 6 及以下版本添加 IE 条件注释。

      Working Example

      html, body {
          height: 100%;
      }
      div {
          height:100%;
          min-height:100%;
      }
      .d1 {
          background: #3cc;
      }
      .d2 {
          background: #ddd;
      }
      .d3 {
          background: #c00;
          height:auto;
      }
      

      将此添加到您的 HTML 文档以支持 IE6 及以下:

      <!--[if lte IE 6]>
      <style>
      .d3 { 
         height: 100%; 
      }
      </style>
      <![endif]-->
      

      在 Firefox、Chrome、Safari、Opera、IE10、IE9、IE8、IE7、IE6 和 IE5 中测试和运行。

      所有的 div 都得到 height:100%;min-height:100%;。在 .d3 上,height:100%; 由于selector's specificity 而被height:auto; 覆盖,但仍保留min-height:100%;

      对于 Internet Explorer 6 和条件 CSS 将应用于 .d3 并用 height:100%; 覆盖 height:auto;

      这行得通,因为(谢天谢地?)IE 对待“高度”应该如何对待“最小高度”。
      -CSS Tricks

      【讨论】:

        【解决方案6】:

        第一件事,请尝试添加 CSS 重置脚本。

        对于演示,我只是将 marginpadding 重置为 0

        CSS:

        * {
            margin: 0;
            padding: 0;
        }
        

        那就不要使用height: auto !important;,因为浏览器会根据孩子来计算高度,这是默认值。

        添加box-sizing 属性以允许您定义某些元素以以某种方式适应区域。

        为了价值使用border-box。该值将指定该元素的宽度和高度(以及最小/最大属性),确定元素的边框。内容的宽度和高度是通过从指定的widthheight 属性中减去各自边的边框和填充宽度来计算的

        IE(8+)、Opera(8.5+)、Chrome(*) 和 Safari(3) 支持 box-sizing 属性。

        对于 IE 6/7,您可以通过 Christian Schepp Schaeferbox-sizing: border-box 使用 polyfill

        这篇关于box-sizingChris Coyier的文章。

        这个完整的解决方案可以跨浏览器工作。 Demo

        HTML:

        <div class="first">
            First DIV
            <div class="second">
                Second DIV
                <div class="third">
                    <div class="fourth">
                        Fourth DIV
                    </div>
                </div>
            </div>
        </div>
        

        CSS:

        * {
            margin: 0;
            padding: 0;
        }
        html,body{
               height: 100%;            /* ie 6 will use this instead of min-height */
               min-height: 100%;        /* ie 6 will ignore this */
        }
        
        div{
            min-height: 100%;
            width:100%;
            -webkit-box-sizing: border-box;
            -moz-box-sizing: border-box;
            -o-box-sizing: border-box;
            box-sizing: border-box;
            *behavior: url(pathto/boxsizing.htc); /* Add the behavior/HTC after every box-sizing: border-box. You must add prefix with a star so IE6/7 can see it */
        }
        
        .first{
            padding:50px;
            background: red;
        }
        
        .second{
            padding:25px;
            background: green;
        }
        
        .third{
            background: yellow;
        }
        
        .fourth{
            background: brown;
        }
        

        【讨论】:

        • 这是因为依赖于身体的 div 的大小和身体的高度总是设置为 100%.. 你只需要像这样改变 div 的高度 div {min-height :100%;}。因此,如果 div 的内容大于主体,则 div 将根据内容更新高度...see here
        • 抱歉,请尝试再次阅读NathanLee 的评论,清楚吗?或者,如果您想获得视口的大小,您可以使用 vw 作为宽度,使用 vh 作为高度.. 但请记住 Can I Use this?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多