【问题标题】:Common overlapping issue but usual answer do not seem to work常见的重叠问题,但通常的答案似乎不起作用
【发布时间】:2012-08-03 01:20:17
【问题描述】:

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

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

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

首先没有body上的相对位置:

http://www.klstuff.com/test1

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

http://www.klstuff.com/test2

基本上我希望框 2 贴在窗口底部,但是当窗口调整大小时,我不希望它与框 1 重叠。我还尝试将 min-height 和 height 100% 属性添加到正文和容器标签,但这似乎根本没有做任何事情。这是 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;
}

【问题讨论】:

标签: html css


【解决方案1】:

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

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

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

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;
}

希望这会有所帮助。它对我有用。

【讨论】:

  • 是的,这行得通,唯一的问题是必须删除填充,否则会有一些重叠。但这对于这些基本站点来说是可以的。然而,它仍然不能完全适用于我正在实际工作的网站,但我认为这是因为这个确切的填充问题,我现在要解决这个问题。我注意到在已发布网站上使用检查元素时,其中很多都有一个围绕整个网站的“包装”类型 div,我从来没有真正看到这些的意义,因为我认为 body 标签已经做到了,但我想它没有这就是为什么这么多人拥有它。非常感谢你的努力 Mackey :)
  • 哈哈,我所做的只是忘记在页脚上方的 div 上放入 margin-bottom :) 再次感谢 Mackey,您的解决方案似乎工作得很好
  • 没问题,我很高兴你最终得到了它。只需单击绿色勾号即可将其标记为已接受的解决方案。谢谢!
猜你喜欢
  • 2022-01-03
  • 1970-01-01
  • 2013-02-26
  • 2014-08-14
  • 1970-01-01
  • 1970-01-01
  • 2011-02-26
  • 1970-01-01
  • 2012-12-31
相关资源
最近更新 更多