在Internet Explorer 7中对绝对div的填充进行了不同的解释

时间:2018-09-25 13:53:21

标签: html css internet-explorer-8 internet-explorer-7 absolute

在Internet Explorer 8及更高版本以及所有其他浏览器中,绝对div元素的填充不会在页边距为负数时将内容向下推到页面上。

但是在Internet Explorer 7中,填充还是将内容压低了。

此代码不使用JavaScript。

下面是它在Internet Explorer 8,Firefox和Chrome中运行的屏幕截图(没有垂直溢出):

enter image description here

这是在Internet Explorer 7中运行的相同代码的gif:

enter image description here

这是我的代码:

p {
  margin-top: 0;
  margin-bottom: 0;
}

* {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
} 

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
}
#container {
  min-height: 100%;
  position: relative;
}
#header {
  background: yellow;
  height:50px;
}
#body {
  padding-bottom: 50px; /* Height of the footer */
}
#footer {
  position: absolute;
  bottom: 0;
  width: 100%;
  height: 50px;         /* Height of the footer */
  background: green;
}

#body {
  height: 100%;
  width: 100%;
  position: absolute;
  margin-top: -50px;
  padding-top: 50px;
}
#main-content-container {
  height: 100%;
}
.inset-boxshadow-and-background {
  background-color: red;
  height: 100%;
  width: 100%;
}
</style>

<!--[if lt IE 7]>
<style media="screen" type="text/css">
#container {
  height: 100%;
}
</style>
<![endif]-->
<body>

<div id="container">
  <div id="header">

  </div>

    <div class="body-parent">
      <div id="body">
        <div id="main-content-container">
          <div class="inset-boxshadow-and-background">
          <!-- Body start -->
          <p>In this demo the footer is pushed to the bottom of the screen in all standards compliant web browsers even when there is only a small amount of content on the page. This with work with IE 6 and IE 5.5 too. Non-standards compliant browsers degrade gracefully by positioning the footer under the content as per normal. Read the <a href="https://matthewjamestaylor.com/bottom-footer">full article</a> for all the details.</p>
          <!-- Body end -->
          </div>
        </div>
      </div>
    </div>
  <div id="footer">

  </div>
</div>

</body>

请不要出现“ Internet Explorer 7没用”的情况,因为我知道它很臭!

3 个答案:

答案 0 :(得分:1)

尝试使用以下代码:

<style type="text/css">
    p {
        margin-top: 0;
        margin-bottom: 0;
    }

    * {
        -webkit-box-sizing: border-box;
        -moz-box-sizing: border-box;
        box-sizing: border-box;
    }
    html,
    body {
        margin: 0;
        padding: 0;
        height: 100%;
    }
    /* Core styles */
    .header {
        position: absolute; /* needed for stacking */
        height: 50px;
        width: 100%;
        background-color: yellow;
    }

    .content {
        position: absolute;/* needed for stacking */
        top:50px;
        width: 100%;
        height: 100%;
        background-color:red;
    }
    .footer {
        position: absolute;/* needed for stacking */
        width: 100%;
        height: 50px;
        bottom:-50px;
        background-color:aqua;
    }
    #main-content-container {
        height: 100%;
    }

    .inset-boxshadow-and-background {
        background-color: red;
        height: 100%;
        width: 100%;
    }
</style>
<div class="header">
    <div class="header-inner"></div>
</div>
<div class="content">
    <div class="content-inner">
        <div id="body">
            <div id="main-content-container">
                <div class="inset-boxshadow-and-background">
                    <!-- Body start -->
                    <p>In this demo the footer is pushed to the bottom of the screen in all standards compliant web browsers even when there is only a small amount of content on the page. This with work with IE 6 and IE 5.5 too. Non-standards compliant browsers degrade gracefully by positioning the footer under the content as per normal. Read the <a href="https://matthewjamestaylor.com/bottom-footer">full article</a> for all the details.</p>
                    <!-- Body end -->
                </div>
            </div>
        </div>
    </div>
</div>
<div class="footer">
    <div class="footer-inner"></div>
</div>

更新:以下代码对我而言效果很好,请参考。

<style type="text/css">
    p {
        margin-top: 0;
        margin-bottom: 0;
    }

    * {
        -webkit-box-sizing: border-box;
        -moz-box-sizing: border-box;
        box-sizing: border-box;
    }

    html,
    body {
        margin: 0;
        padding: 0;
        height: 100%;
    }

    #container {
        height: 100%;
        position: relative;
        background-color:red;
    }
    #header {
        position: absolute;
        top:0;
        width:100%;
        background-color: yellow;
        height: 50px;
    }
    .body-parent {
        margin-top: 50px;
        position: absolute;
    }
    #footer {
        position: absolute;
        bottom: 0;
        width: 100%;
        height: 50px; /* Height of the footer */
        background-color: green;
    }
    #body {
        height: 100%;
        width: 100%;
        padding-bottom: 50px; 
    }

    #main-content-container {
        height: 100%;
    }

    .inset-boxshadow-and-background {
        background-color: red;
        height: 100%;
        width: 100%;
    }

</style>
<div id="container">
    <div id="header">
    </div>
    <div class="body-parent">
        <div id="body">
            <div id="main-content-container">
                <div class="inset-boxshadow-and-background">
                    <!-- Body start -->
                    <p>In this demo the footer is pushed to the bottom of the screen in all standards compliant web browsers even when there is only a small amount of content on the page. This with work with IE 6 and IE 5.5 too. Non-standards compliant browsers degrade gracefully by positioning the footer under the content as per normal. Read the <a href="https://matthewjamestaylor.com/bottom-footer">full article</a> for all the details.</p>
                    <!-- Body end -->
                </div>
            </div>
        </div>
    </div>
    <div id="footer">
    </div>
</div>

结果如下: enter image description here

答案 1 :(得分:0)

我使用IE 7进行了测试,可以产生此问题。

当静态元素在顶部/左侧被赋予负边距时,它将沿该指定方向拉动元素。

但是,如果将其应用于底部/右侧,则不会像您所想的那样向下/向右移动元素。取而代之的是,它将所有后续元素拖入主元素,使其重叠。

因此,由于这个原因,我认为您的页脚沿顶部方向拉了50像素。 IE 7可能会在负边距下工作。

您可以尝试将其删除,以帮助您解决此问题。

参考:

The Definitive Guide to Using Negative Margins

答案 2 :(得分:0)

通过仅更改任何绝对定位的div(负负距和相同位数的正填充),可以修复仅IE7这种奇怪的外观(IE6和IE8 +可以正常显示)。

这是JS:

window.onload = function(){
  $('body').height( $(window).height() - 100 );
};
p {
  margin-top: 0;
  margin-bottom: 0;
}

* {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
} 

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
}
#container {
  min-height: 100%;
  position: relative;
}
#header {
  background: yellow;
  height:50px;
}
#body {
  padding-bottom: 50px; /* Height of the footer */
}
#footer {
  position: absolute;
  bottom: 0;
  width: 100%;
  height: 50px;         /* Height of the footer */
  background: green;
}

#body {
  height: 100%;
  width: 100%;
  position: absolute;
  margin-top: -50px;
  padding-top: 50px;
}
#main-content-container {
  height: 100%;
}
.inset-boxshadow-and-background {
  background-color: red;
  height: 100%;
  width: 100%;
}
</style>

<!--[if lt IE 7]>
<style media="screen" type="text/css">
#container {
  height: 100%;
}
</style>
<![endif]-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<body>

<div id="container">
  <div id="header">

  </div>

    <div class="body-parent">
      <div id="body">
        <div id="main-content-container">
          <div class="inset-boxshadow-and-background">
          <!-- Body start -->
          <p>In this demo the footer is pushed to the bottom of the screen in all standards compliant web browsers even when there is only a small amount of content on the page. This with work with IE 6 and IE 5.5 too. Just remove the javascript to make it work in every browser except ie7, and keep the javascript to only run successfully it in IE7.</p>
          <!-- Body end -->
          </div>
        </div>
      </div>
    </div>
  <div id="footer">

  </div>
</div>

</body>