页眉和页脚DIV与中间内容重叠

时间:2012-10-03 20:03:56

标签: css html dynamic

为了使问题更易于分析,我创建了this jsFiddle

代码:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <style>
        body {margin:0; }
        #mainContainer { position: absolute; right: 4%; left: 4%; height: 100%; }
        #headerContainer { width: 100%; z-index: 10; position: absolute; background: #323232; color: white; height: 30px; }
        #middleContainer { height: 100%; }
        #leftSection { position: absolute; float: left; width: 175px; background: #71ABD1; height: 100%; overflow: auto; color: black; padding-top: 30px; }
        #middleSection { position: absolute; height: 100%; background-color: yellow; left: 175px; right: 175px; color: black; padding-top: 30px; }
        #rightSection { float: right; height: 100%; width: 175px; border-left: 1px dotted black; background: red; color: black; padding-top: 30px; }
        #footerContainer { position: absolute; bottom: 0; width: 100%; height: 30px; background: #323232; color: white; }
    </style>
</head>
<body>
    <div id="mainContainer">
        <div id="headerContainer">
            headerContainer
        </div>
        <div id="middleContainer">
            <div id="leftSection">
                leftSection
            </div>
            <div id="middleSection">
                middleSection
            </div>
            <div id="rightSection">
                rightSection
            </div>
        </div>
        <div id="footerContainer">
            footerContainer
        </div>
    </div>
</body>
</html>

使用顶部,中部和底部部分的标记,问题是:

1-正如您可以看到,尽管页脚div上有position:absolutebottom:0px,但页面底部的页脚并不是真正的黑色

2-更重要的是,leftSection,middleSection和rightSection DIV与页眉和页脚DIV重叠,事实上,在这个小提琴中,查看3个中间部分显示的文本的唯一方法是放置填充以避免它显示在标题DIV下面。

我尝试在middleContainer上放置30px的顶部和底部值以修复重叠问题,但这并没有解决问题,我想要的是将headerContainer保持在顶部,将footerContainer保持在底部,而所有3个中间部分都调整为100%高度。 leftSection和rightSection具有固定的宽度,但middleSection具有灵活的宽度和高度。

5 个答案:

答案 0 :(得分:1)

http://jsfiddle.net/grc4/XTQuT/2/在没有指定实体高度值的情况下完全按照我想要的那样做。

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
      <style>
    body {
    margin: 0;
    height:100%;
    }
#mainContainer {
    position: absolute;
    right: 4%;
    left: 4%;
    height: 100%;
}
#headerContainer {
    width: 100%;
    position: relative;
    background: #323232;
    color: white;
    height: 30px;
}
#middleContainer {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    margin: 30px 0;
}
#leftSection {
    float: left;
    width: 175px;
    background: #71ABD1;
    height: 100%;
    overflow: auto;
    color: black;
}
#middleSection {
    position: absolute;
    background-color: yellow;
    left: 175px;
    right: 175px;
    top: 0;
    bottom: 0;
    color: black;
}
#rightSection {
    float: right;
    height: 100%;
    width: 175px;
    border-left: 1px dotted black;
    background: red;
    color: black;
}
#footerContainer {
    position: absolute;
    bottom: 0;
    width: 100%;
    height: 30px;
    background: #323232;
    color: white;
}​
</style>
    </head>
    <body>
        <div id="mainContainer">
            <div id="headerContainer">
                headerContainer
            </div>
            <div id="middleContainer">
                <div id="leftSection">
                    <div style="margin-top: 30px;">leftSection</div>
                </div>
                <div id="middleSection">
                    <div style="margin-top: 30px;">middleSection</div>
                </div>
                <div id="rightSection">
                    <div style="margin-top: 30px;">rightSection</div>
                </div>
            </div>
            <div id="footerContainer">
                footerContainer
            </div>
        </div>
    </body>
    </html>

答案 1 :(得分:0)

“填充顶部:30px;”在你的3个内部元素上,它们比实际身体的高度高30px,造成你的问题。

从这3个元素中删除顶部填充,然后使页眉和页脚相对定位,而不是绝对,并且应该设置。

像这样:http://jsfiddle.net/BPJxD/28/

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <style>
        body {margin:0; }
        #mainContainer { position: absolute; right: 4%; left: 4%; height: 100%; }
        #headerContainer { width: 100%; z-index: 10; position: relative; background: #323232; color: white; height: 30px; }
        #middleContainer { height: 100%; }
        #leftSection { position: absolute; float: left; width: 175px; background: #71ABD1; height: 100%; overflow: auto; color: black; }
        #middleSection { position: absolute; height: 100%; background-color: yellow; left: 175px; right: 175px; color: black;  }
        #rightSection { float: right; height: 100%; width: 175px; border-left: 1px dotted black; background: red; color: black;  }
        #footerContainer { position: relative; width: 100%; height: 30px; background: #323232; color: white; }
    </style>
</head>
<body>
    <div id="mainContainer">
        <div id="headerContainer">
            headerContainer
        </div>
        <div id="middleContainer">
            <div id="leftSection">
                leftSection
            </div>
            <div id="middleSection">
                middleSection
            </div>
            <div id="rightSection">
                rightSection
            </div>
        </div>
        <div id="footerContainer">
            footerContainer
        </div>
    </div>
</body>
</html>

答案 2 :(得分:0)

您的问题是您在headercontainerfootercontainersolution

中使用了不必要的绝对定位

HTML:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
    <div id="mainContainer">
        <div id="headerContainer">
            headerContainer
        </div>
        <div id="middleContainer">
            <div id="leftSection">
                leftSection
            </div>
            <div id="middleSection">
                middleSection
            </div>
            <div id="rightSection">
                rightSection
            </div>
        </div>
        <div id="footerContainer">
            footerContainer
        </div>
    </div>
</body>
</html>

CSS:

body { margin:0; }
#mainContainer
{ 
    position: absolute; 
    right: 4%; left: 4%; 
    height: 100%; 
}
#headerContainer
{ 
    width: 100%; 
    z-index: 10; 
    position: relative; 
    background: #323232; 
    color: white; 
    height: 30px;
}
#middleContainer { height: 100%; }
#leftSection
{
    position: absolute; 
    float: left; 
    width: 175px; 
    background: #71ABD1; 
    height: 100%; 
    overflow: auto; 
    color: black; 
    padding-top: 30px;
}
#middleSection
{ 
    position: absolute; 
    height: 100%; 
    background-color: yellow; 
    left: 175px; 
    right: 175px; 
    color: black; 
    padding-top: 30px; 
}
#rightSection
{
    float: right; 
    height: 100%; 
    width: 175px; 
    border-left: 1px dotted black; 
    background: red; 
    color: black; 
    padding-top: 30px; 
}
#footerContainer 
{  
    position: relative; 
    bottom: 0; width: 100%; 
    height: 30px; 
    background: #323232; 
    color: white;
}

回顾整个小提琴我注意到你在每个div使用绝对定位。这是错误的。

你应该只在绝对定位时:

  1. 您需要一个没有文档常用格式的容器。如弹出框或浮动框。
  2. 您需要在具有固定定位的父div内使用div,但这只有在父定位设置为relative时才有效。

答案 3 :(得分:0)

您可以删除所有3个

padding-top: 30px;

#leftSection { position: absolute; float: left; width: 175px; background: #71ABD1; height: 100%; overflow: auto; color: black;  }
#middleSection { position: absolute; height: 100%; background-color: yellow; left: 175px; right: 175px; color: black; }
#rightSection { float: right; height: 100%; width: 175px; border-left: 1px dotted black; background: red; color: black;  }

并像这样更改你的HTML

<div id="mainContainer">
    <div id="headerContainer">
        headerContainer
    </div>
    <div id="middleContainer">
        <div id="leftSection">
            <div style="margin-top:30px;">leftSection</div>
        </div>
        <div id="middleSection">
            <div style="margin-top:30px;">middleSection</div>
        </div>
        <div id="rightSection">
            <div style="margin-top:30px;">rightSection</div>
        </div>
    </div>
    <div id="footerContainer">
        footerContainer
    </div>
</div>

我希望这会有所帮助。

答案 4 :(得分:0)

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <style>
        body {margin:0; }
        #mainContainer { position: relative; right: 4%; left: 4%; height: 100%; width:1000px; }
        #headerContainer { width: 1000px; z-index: 10; position: absolute; background: #323232; color: white; height: 30px; }
        #middleContainer { height: 100%; width:1000px; position:relative; display: table-cell;}
        #leftSection { float: left; width:25%; background: #71ABD1;  overflow: auto; color: black; padding-top: 30px;  height: 100%;}
        #middleSection { float: left;  height:100%; width:50%; background-color: yellow;  color: black; padding-top: 30px; }
        #rightSection { float:left; height:100%; width: 25%; background: red; color: black; padding-top: 30px; }
        #footerContainer {  bottom: 0; width:1000px; height: 30px; background: #323232; color: white; float:left;}
    </style>
</head>
<body>
<div id="mainContainer">
  <div id="headerContainer"> headerContainer </div>
  <div id="middleContainer" >
    <div id="leftSection"> leftSection </div>
    <div id="middleSection"> middleSection </div>
    <div id="rightSection"> rightSection
      rightSection            rightSection            rightSection            rightSection            rightSection            rightSection            rightSection </div>
  </div>
  <div id="footerContainer" > footerContainer </div>
</div>
</body>
</html>