在Internet Explorer中,height:100%无法正常工作。

26

我对Internet Explorer中的CSS属性height:100%有疑问。

height:100%在IE中不起作用,但在Firefox和Chrome中可以。

HTML文件:

<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<html style="height:100%;">

<head>
    <meta charset="utf-8" />
    <link rel="stylesheet" href="style.css" type="text/css" />
</head>

<body>
    <div id="menu_neu">
        <ul id="topNav">
            <li class="current"><a href="#section-1">SECTION 1</b></a>
            </li>
            <li><a href="#section-2">SECTION 2 </b></a>
            </li>
        </ul>
    </div>
    <div id="wrapper">
        <!--SECTION- 1 .....................................................................................................................................-->
        <div class="section" id="section-1">SECTION - 1
            <div style="height:100%;" id="intro">
                <div class="story">
                    <div class="bg1"></div>
                    <div class="bg2"></div>
                    <div class="bg3"></div>
                    <div class="float-left" style="margin-top:120px; width:500px;">
                         <h2>HEIGHT 100% works in Chrome and Firefox,<br> <b>but not in IE!</b></h2>

                        <div id="s1Pfeil1" style="padding-top:120px;padding-left:255px;"> <a href="#second" title="Next Section"><img src="images/icon_down_normal.png" alt="Next" /></a>

                        </div>
                        <!--/box-links-->
                    </div>
                    <!--/float-left-->
                </div>
                <!--./story-->
            </div>
            <!--/intro-->
            <div style="height:100%;" id="second">
                <div class="story">
                    <div class="bg1_2"></div>
                    <div class="bg2_2"></div>
                    <div class="bg3_2"></div>
                    <div class="float-right" style="margin-top:45px; width:500px;">
                            <h2>HEIGHT 100% works in Chrome and Firefox,<br> <b>but not in IE!</b></h2>

                        <div id="s1Pfeil2" style="padding-top:200px;padding-left:225px;">   <a href="#intro" title="Before Section"><img src="images/icon_up_normal.png" alt="Next" /></a>&nbsp;&nbsp;</a>  <a href="#third" title="Next Section"><img src="images/icon_down_normal.png" alt="Next" /></a></a>&nbsp;&nbsp;</a>
                        </div>
                        <!--/box-links1-->
                    </div>
                    <!--/float-right-->
                </div>
                <!--./story-->
            </div>
            <!--#second-->
            <div id="third" style="height:100%;">
                <div class="story">
                    <div class="bg1_3"></div>
                    <div class="bg2_3"></div>
                    <div class="bg3_3"></div>
                    <div class="float-left" style="margin-top:80px; width:500px;">
                            <h2>HEIGHT 100% works in Chrome and Firefox,<br> <b>but not in IE!</b></h2>

                        <div id="s1Pfeil3" style="padding-top:200px;padding-left:225px;">   <a href="#second" title="Next Section"><img src="images/icon_up_normal.png" alt="Next" /></a>

                        </div>
                        <!--/box-links2-->
                    </div>
                    <!--/float-left-->
                </div>
                <!--.story-->
            </div>
            <!--#third-->
        </div>
        <!--/section1-->
        <!--SECTION- 2 BRANDSTIFTER..................................................................................................................................-->
        <div class="section" id="section-2">SECTION -2
            <div id="intro_bs">
                <div class="story_bs">
                    <div class="bg1_bs"></div>
                    <div class="bg2_bs"></div>
                    <div class="bg3_bs"></div>
                    <div class="float-left_bs" style="margin-top:50px; width:600px;">
                            <h2>HEIGHT 100% works in Chrome and Firefox,<br> <b>but not in IE!</b></h2>

                        <div id="s2Pfeil1" style="padding-top:-50px;padding-left:305px;"> <a href="#second_bs" title="Next Section"><img src="images/icon_down_normal.png" alt="Next" /></a>

                        </div>
                        <!--/box-links-->
                    </div>
                    <!--/float-left_bs-->
                </div>
                <!--.story-->
            </div>
            <!--#intro-->
            <div id="second_bs">
                <div class="story_bsc">
                    <div class="bg1_2_bs"></div>
                    <div class="bg2_2_bs"></div>
                    <div class="bg3_2_bs"></div>
                    <div class="float-right_bs" style="margin-top:120px; width:500px;">
                            <h2>HEIGHT 100% works in Chrome and Firefox,<br> <b>but not in IE!</b></h2>

                        <div id="s2Pfeil2" style="padding-top:80px;padding-left:305px;"><a href="#intro_bs" title="Next Section"><img src="images/icon_up_normal.png" alt="Next" /></a>&nbsp;&nbsp;<a href="#third_bs" title="Next Section"><img src="images/icon_down_normal.png" alt="Next" /></a>
                        </div>
                    </div>
                </div>
                <!--.story-->
            </div>
            <!--#second-->
            <div id="third_bs">
                <div class="story_bsc">
                    <div class="bg1_3_bs"></div>
                    <div class="bg2_3_bs"></div>
                    <div class="bg3_3_bs"></div>
                    <div class="float-right_bs" style="margin-top:120px; width:500px;">
                            <h2>HEIGHT 100% works in Chrome and Firefox,<br> <b>but not in IE!</b></h2>

                        <div id="s2Pfeil3" style="padding-top:100px;padding-left:225px;">   <a href="#second_bs" title="Next Section"><img src="images/icon_up_normal.png" alt="Next" /></a>

                        </div>
                    </div>
                </div>
                <!--.story-->
            </div>
            <!--#third-->
        </div>
        <!--/section-2-->
    </div>
    <!--/wrapper-->
    <div class="suche">
        <form class="search" action="" method="post">
            <label for="search-box">Search:</label>
            <input id="search-box" type="text" name="search" onclick="search_bigger();" onblur="search_smaller();">
        </form> <a href="http://www.facebook.com/BrandortMarketingKommunikationsGmbh" title="Werde Fan von Brandort. auf Facebook" alt="Werde Fan von Brandort. auf Facebook" target="_blank"><div class="herz"></div></a><a href="images/icon_quicklinks_phone_hover_big.png" rel="lightbox" title="So erreichen Sie uns" alt="So erreichen Sie uns"><div class="telefon"></div></a>

    </div>
    <div class="css-panes">
        <div style="display: block;"></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
    </div>
</body>

CSS文件:

html{
    height:100%;
}

body{
    font-family: 'universal_light';
    margin: 0;
    min-width: 980px;
    padding: 0;
    line-height:2em;
    background-color: #f6aa27;
    height: 100%;

}

p{
    margin: 0 0 20px 0; 
}

p, ul{
    font-family:'universal_light';
    font-weight:normal;
    font-size:16px;
}

img{
    border: 0;
}

h1, #pixels{
    font-family:"Museo 900", Verdana, serif;
    font-size-adjust:0.40;
    font-weight:700;
    font-style:normal;
}

h2{
    font-family: 'universal_black';
    color:#000000;
    font-size:30px;
    font-weight:normal;
}

#wrapper{
    height: 100%;
}

#header h1, #pixels{
    color: #48941A;
}



#nav{
    list-style: none;
    position: fixed;
    right: 20px;
}

#nav li{
    margin: 0 0 15px 0; 
}

#nav_bs{
    list-style: none;
    position: fixed;
    right: 20px;
}

#nav_bs li{
    margin: 0 0 15px 0; 
}

#nav_bsa{
    list-style: none;
    position: fixed;
    right: 20px;
}

#nav_bsa li{
    margin: 0 0 15px 0; 
}

#header, #intro, #second{
    width: 100%;
}



/*hier wurde "fix" nach der "no-repeat" gelöscht*/
#intro{
    background-color:#f6aa27;
    margin: 0 auto;
    padding: 0;
    height: 1200px;
    width:100%;
    border-style:solid;
    border-width:5px;

}



/*hier wurde "fix" nach der "no-repeat" gelöscht*/
#intro .bg1{

    width: 900px;
    position: absolute;
    height: 900px;
    margin: 0 auto;
    padding: 0;
    margin-top: -26px;
    margin-left: 66px;

}
/*hier wurde "fix" nach der "no-repeat" gelöscht*/
#intro .bg2{

    width: 900px;
    position: absolute;
    height: 900px;
    margin: 0 auto;
    padding: 0;

}
#intro .bg3{
    background: url(images/kohle.png) 50% 0 no-repeat fixed;
    width: 1000px;
    position: absolute;
    height: 900px;
    margin: 0 auto;
    padding: 0;

}

#second{
    background:url(images/bg1.jpg) 50% 0 no-repeat;
    background-color:#f6aa27;
    color: #D3D3D3;
    height: 100%;
    min-height: 100%;
    margin: 0 auto;
    padding: 0;
    overflow: hidden;
    border-style:solid;
    border-width:5px;
}

#section-2{
background-color:#D3D3D3;
}

#second .bg1_2{
    background:url(images/second/feurig.png) 50% 0 no-repeat;
    width: 900px;
    position: absolute;
    height: 900px;
    margin: 0 auto;
    padding: 0;
    margin-left: 143px;
    margin-top: -10px;

}

#second .bg2_2{
    background:url(images/second/handschrift.png) 50% 0 no-repeat;
    width: 900px;
    position: absolute;
    height: 900px;
    margin: 0 auto;
    padding: 0;

}

#second .bg3_2{
    background:url(images/second/brandeisen.png) 50% 0 no-repeat fixed;
    width: 900px;
    position: absolute;
    height: 100%;
    margin: 0 auto;
    padding: 0;

}


#third{
    background:url(images/bg1.jpg) 50% 0 no-repeat;
    background-color:#f6aa27;
    color: #000000;
    height: 100%;
    min-height: 100%;
    margin: 0 auto;
    padding: 0;
    overflow: hidden;
        border-style:solid;
    border-width:5px;
}

#third .bg1_3{
    background:url(images/third/handschrift.png) 50% 0 no-repeat;
    width: 900px;
    position: absolute;
    height: 900px;
    margin: 0 auto;
    padding: 0;

}
#third .bg2_3{
    background:url(images/third/feurig.png) 50% 0 no-repeat;
    width: 900px;
    position: absolute;
    height: 900px;
    margin: 0 auto;
    padding: 0;
}

#third .bg3_3{
    background:url(images/third/seil.png) 50% 0 no-repeat fixed;
    width: 900px;
    position: absolute;
    height: 900px;
    margin: 0 auto;
    padding: 0;
}

#fifth{
    background: #ccc;
    height: 400px;
    margin: 0 auto;
    padding: 40px 0 0 0;
}



#header, #intro_bs, #second_bs{
    width: 100%;
}

#intro_bs{
    background:url(images/bg2.jpg) 50% 0 no-repeat;
    background-color:#c9cacc;
    color: #000000;
    height: 900px;
    margin: 0 auto;
    padding: 0;
        border-style:solid;
    border-width:5px;

}

#intro_bs .bg1_bs{
    background:url(images/intro_bs/mitarbeiter.png) 50% 0 no-repeat;
    width: 900px;
    position: absolute;
    height: 900px;
    margin: 0 auto;
    padding: 0;

}

#intro_bs .bg2_bs{
    background:url(images/intro_bs/feurig.png) 50% 0 no-repeat;
    width: 900px;
    position: absolute;
    height: 900px;
    margin: 0 auto;
    padding: 0;

}
#intro_bs .bg3_bs{
    background:url(images/intro_bs/handschrift.png) 50% 0 no-repeat;
    width: 900px;
    position: absolute;
    height: 900px;
    margin: 0 auto;
    padding: 0;
}

#second_bs{
    background:url(images/bg2.jpg) 50% 0 no-repeat;
    background-color:#c9cacc;
    color: #000000;
    height: 950px;
    margin: 0 auto;
    padding: 0;
    overflow: hidden;
        border-style:solid;
    border-width:5px;
}

#second_bs .bg1_2_bs{
    background:url(images/second_bs/feurig.png) 50% 0 no-repeat;
    width: 900px;
    position: absolute;
    height: 900px;
    margin: 0 auto;
    padding: 0;

}

#second_bs .bg2_2_bs{
    background:url(images/second_bs/handschrift.png) 50% 0 no-repeat;
    width: 900px;
    position: absolute;
    height: 900px;
    margin: 0 auto;
    padding: 0;

}

#second_bs .bg3_2_bs{
    background:url(images/second_bs/steffen.png) 50% 0 no-repeat;
    width: 900px;
    position: absolute;
    height: 950px;
    margin: 0 auto;
    padding: 0;
    margin-left: -121px;


}


#third_bs{
    background:url(images/bg2.jpg) 50% 0 no-repeat;
    background-color:#c9cacc;
    color: #000000;
    height: 950px;
    margin: 0 auto;
    padding: 0;
    overflow: hidden;
        border-style:solid;
    border-width:5px;
}

#third_bs .bg1_3_bs{
    background:url(images/third_bs/feurig.png) 50% 0 no-repeat;
    width: 900px;
    position: absolute;
    height: 900px;
    margin: 0 auto;
    padding: 0;

}
#third_bs .bg2_3_bs{
    background:url(images/third_bs/handschrift.png) 50% 0 no-repeat;
    width: 900px;
    position: absolute;
    height: 900px;
    margin: 0 auto;
    padding: 0;

}
#third_bs .bg3_3_bs{
    background:url(images/third_bs/mike.png) 50% 0 no-repeat;
    width: 900px;
    position: absolute;
    height: 950px;
    margin: 0 auto;
    padding: 0;
    margin-left: -121px;
    margin-top: -287px;
}




.story{
    margin: 0 auto;
    min-width: 980px;
    overflow: auto;
    width: 980px;

}

.story .float-left, .story .float-right{
    padding: 100px 0 0 0;
    position: relative;
    width: 350px;   
}


.linkp1 a {
    background:url('images/brandort_norm.gif') 0 0 no-repeat fixed;
    /*height: 12px;
    width: 95px;*/
    display: block;
    z-index:11111;
}

.linkp1 a:hover {
    background:url('images/brandort_hover.gif') 0 0 no-repeat fixed;
}


.linkp2 a {
    background:url('images/brandstifter_norm.gif') no-repeat fixed;
    /*height: 12px;
    width: 95px;*/
    display: block;
    z-index:11111;
}

.linkp2 a:hover {
    background:url('images/brandstifter_hover.gif') no-repeat fixed;
}

.linkp3 a {
    background:url('images/brandsaetze_norm.gif') no-repeat fixed;
    /*height: 12px;
    width: 95px;*/
    display: block;
    z-index:11111;
}

.linkp3 a:hover {
    background:url('images/brandsaetze_hover.gif') no-repeat fixed;
}

.linkp4 a {
    background:url('images/brandschaetze_norm.gif') no-repeat fixed;
    /*height: 12px;
    width: 95px;*/
    display: block;
    z-index:11111;
}

.linkp4 a:hover {
    background:url('images/brandschaetze_hover.gif') no-repeat fixed;
}

.linkp5 a {
    background:url('images/brandpunkt_norm.gif') no-repeat fixed;
    /*height: 12px;
    width: 95px;*/
    display: block;
    z-index:11111;
}

.linkp5 a:hover {
    background:url('images/brandpunkt_hover.gif') no-repeat fixed;
}




#menu_neu {
height: 40px;
width: 1200px;
text-align: center;
line-height:1em!important;
position: fixed;
left: 40%;
width: 640px;
z-index: 10000;
}

ul#topNav{
    margin-top:51px;
    margin-left: -218px;
    color:#000000;
}

ul#topNav li {
    list-style: none;
    display: inline;
    margin: 0; padding: 0;
    margin-top: 50%;
}

ul#topNav a, ul#topNav span {
    float: left;
    margin-right: 0.4em;
    margin-left: 0.1em;
    padding: 0.2em 1em;
    text-decoration: none;
    color:#000000;
    font-size:14px;
}

* html ul#topNav a, * html ul#topNav span {  /* Korrektur fuer IE 5.x */
    width: 8em;
    wi\dth: 6em;
}

ul#topNav a:hover, ul#topNav span {
    color:#000000;
    font-size:14px;
}

ul#topNav a:active {
    color:#000000;
    font-size:14px;
}

ul#topNav div {
     clear: left;
}

#topNav .current a {
    color:#F45D32;
}

演示: http://ealtinel.com/height/

如何在IE中让它工作?


你的第一个<meta>标签也应该放在<head>标签内! - Sirko
我已经修改了,但它还是不起作用 :( - aldimeola1122
在你的CSS中,将html、body {height:100%}设置为自动高度(例如:#wrapper{height:auto;}) - Anna.P
你想要实现什么目标? - Savas Vedova
嗨,Aldimeola...你的问题出在图片标签上(例如:<img src="images/icon_down_normal.png" alt="Next" />)。如果我移除图片标签,在IE中它可以正常工作...问题在于图片的定位。 - Anna.P
显示剩余4条评论
8个回答

40

在IE浏览器中,要使一个元素具有height:100%;属性,所有父级元素都必须具有height:100%;属性。如果“链条”中断,IE浏览器将忽略所有相关属性。

如果您的CSS包含以下内容,则应该可以正常工作:

html,body { height:100%; } 
#wrapper { height:100%; }
.section { height:100%; }
我已经在IE中打开了您的页面并使用开发者工具应用了这些更改,并可以验证其有效性。

@aldimeola1122,你需要把 #wrapper{ height:100%; } 放回去。 - snumpy

15
为了使用height: 100%,父容器应该有固定的高度。
例如,这样做是可以的:
<div style="height: 400px;">
    <div style="height: 100%; background: red; float: left; width: 200px;">
        Left Column
    </div>
    <div style="height: 100%; margin-left: 210px;">
        Right Column
    </div>
</div>

以下内容无法正常工作:
<div style="height: 100%;">
    --- same code
</div>

如果你不知道父元素的高度,想要实现固定高度的话,可以使用 position: absolute;

<div style="position: relative;">
    <div style="position: absolute; left: 0; top: 0; bottom: 0; width: 200px; background: red;">
        Left Column
    </div>
    <div style="margin-left: 210px;">
        Right Column
    </div>
</div>

否则,您可以像其他答案中提到的那样使用JavaScript。但我更喜欢纯CSS解决方案。
在此处查看实时演示
希望这有所帮助。

@aldimeola1122 抱歉我错过了你的评论... 我刚看到它,但链接已失效。 - Savas Vedova
@LaraBelle 我猜那个浏览器已经死了10年了 :) - Savas Vedova
@SavasVedova 不在我高级开发人员的行列中。他希望所有事情都得到支持。 - hdotluna
1
@LaraBelle,抱歉,我没有测试IE 6的工具。也许你可以尝试设置每个父元素的高度为100%,或者你可以使用JavaScript来设置高度(我猜这是一个更简单的解决方案)。 - Savas Vedova

2
为了使其正常工作,您需要使父HTML和子HTML具有相同的属性。在某些情况下,您需要使用像素才能使其正常运行。
html,body, \\ this has to go to every element you want to have a height 100% 
{
  height:100%;
}

我也有一些好东西。
window.onload=function(){
if(navigator.appName == "Microsoft Internet Explorer"){ \\ to detect if the browser used by the client is IE
winHight = window.innerHeight
document.getElementById("mainContainer").setAttribute('style',"height:"+winHeight+";");
}
}

0

我在一个项目中也遇到了同样的问题,但是无法找到解决方案,因为width: 100%可以工作,但不包括height。所以我使用了一个小技巧,将我的页面包装在一个div中,并使用JavaScript更新div的高度。

windowHeight = window.innerHeight
document.getElementById("mainContainer").setAttribute('style',"height:"+windowHeight+";");

并将其链接到一个事件。


0
如果您没有输入高度属性,它将按比例缩放至宽度大小。在IE浏览器中,style="height: 25%; width: 25%; object-fit: contain"无法正常工作。然而,使用style="width: 25%; object-fit: contain"可以在IE浏览器中正常工作,并将高度设为25%。

0

尝试这段代码

border-bottom: 1px solid /* 与您的设计相匹配的任何颜色 */


0

我知道这篇文章已经有些年头了,但它仍然可能对一些人有用。我在IE上遇到了一个图片问题。该图片具有属性“max-width:100%”,在Chrome上可以完美地工作,但在IE上却根本不起作用。

我所做的简单处理是给图片设置一个宽度和最大宽度。 代码应该类似于:

 img.logo {
        width:100%;
        max-width:1600px;
    }

这对我有用 :D

`


-1
我尝试了几个方法,这似乎是解决方案:
在HTML文档的头部添加以下元标记:
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE10" >

看起来IE自动为(IE)版本7打开兼容模式。这个元标记强制IE将兼容模式设置为IE的版本10。此解决方案仅适用于IE版本10或更高版本。

祝好运!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接