0

为了使问题更易于分析,我创建了这个 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 具有灵活的宽度和高度。

4

5 回答 5

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>
于 2012-10-04T09:23:48.553 回答
0

"padding-top: 30px;" 在您的 3 个内部元素上,它们使它们比实际身体的高度高 30 像素,从而产生了您的问题。

从这 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>
于 2012-10-03T20:13:44.300 回答
0

您的问题是您在 and 中使用了不必要的绝对定位headercontainerfootercontainer解决方案

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

回顾你的整个 Fiddle 我注意到你在每个div. 这是飞机错了。

您应该只在以下情况下进行绝对定位:

  1. 您需要一个没有文档通常格式的容器。例如弹出框或浮动框。
  2. 您需要在具有固定定位div的父级内部使用div,但这仅在父级定位设置为时才有效relative
于 2012-10-03T20:37:29.477 回答
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>

我希望这会有所帮助。

于 2012-10-04T00:07:02.953 回答
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>
于 2012-10-04T11:22:14.117 回答