1

好吧,伙计们,我完全知道这个问题已经在几个网站上被问过好几次了,我已经完成了我的研究并尝试了人们给出的所有解决方案,但我显然错过了一些东西,因为他们没有帮助。我对 HTML 和 CSS 比较陌生,所以我可能忽略了一些简单的东西。

这是我的问题。我有页眉和容器 div,然后是页脚 div,我希望页脚 div 保持在窗口底部,但是当窗口调整大小时,我不希望它与容​​器 div 重叠。

我可以让页脚 div 粘在浏览器的底部,明显的绝对位置和底部 0 CSS 没有问题,但显然这会导致容器 div 的重叠问题,所以我做了我的研究并尝试添加一个相对定位到 body 标签,然后将页脚 div 移动到容器 div 的底部,而不是窗口的底部。我在这里创建了我的问题的迷你模拟:

首先没有身体上的相对位置:

http://www.klstuff.com/test1

然后在身体上的相对位置:

http://www.klstuff.com/test2

基本上我希望框 2 贴在窗口的底部,但是当窗口调整大小时,我不希望它与框 1 重叠。我还尝试将 min-height 和 height 100% 属性添加到 body 和 container 标签,但是似乎什么也没做。这是 test2 的代码(我认为相对位置属性稍微接近正确。)

<?xml version="1.0" encoding ="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<link href="stylesheet.css" rel="stylesheet" type="text/css" />
<head>
<title>HTML/CSS Test Site</title>
<meta name="description" content="HTML/CSS testing page.">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>


<body>


<div id="Box1">
    <p>BOX 1</p>
</div>

<div id="Box2">
    <p>BOX 2</p>
</div>

</body>
</html>

body {
height: 100%;
position: relative;
}

#Box1 {
width: 980px;
background-color: blue;
color: #fff;
margin: auto;
text-align: center;
padding-bottom: 150px;
padding-bottom: 150px;
margin-top: 200px;
}

#Box2 {
width: 100%;
background-color: red;
text-align: center;
padding-bottom: 50px;
padding-bottom: 50px;
position: absolute;
bottom: 0;
left: 0;
}
4

1 回答 1

0

哇呼终于做到了!花了我一段时间。

好的,首先,在两个盒子周围制作一个包含 div。将主体的高度设置为 100%,然后将容器的最小高度设置为 100%。将容器位置设置为绝对位置,并将其宽度设置为 100%。

然后,为您的页脚设置一个高度,并为您的主要内容设置一个相同值的边距底部(即框 1)。根据代码:

HTML:

<div id="container">
   <div id="Box1">
       <p>BOX 1</p>
   </div>

   <div id="Box2">
       <p>BOX 2</p>
   </div>
</div>

CSS:

body {
  height: 100%;
  padding: 0px;
  margin: 0px;
}

#Box1 {
  width: 980px;
  background-color: blue;
  color: #fff;
  margin: auto;
  text-align: center;
  height: 150px;
  margin-bottom: 100px;
  margin-top: 200px;
}

#Box2 {
  width: 100%;
  background-color: red;
  text-align: center;
  position: absolute;
  bottom: 0;
  height: 100px;

}

#container {
  width: 100%;
  min-height: 100%;
  position:absolute;
}

希望这可以帮助。这个对我有用。

于 2012-08-02T17:20:17.007 回答