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

时间:2019-05-15 12:24:49

标签: html css

这是我试图制造出这个问题的小提琴: http://jsfiddle.net/BPJxD/1/

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

1-如您所见,尽管页脚div上具有position:absolute和bottom:0px,但是黑色的页脚并没有真正位于页面底部

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

我已经尝试在MiddleContainer上放置30px的top和bottom值来解决重叠问题,但这并不能解决问题,我想要的是将headerContainer放在顶部,footerContainer放在底部,而所有3个中间部分都调整为100%的高度。 leftSection和rightSection具有固定的宽度,而middleSection具有灵活的宽度和高度。

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

1 个答案:

答案 0 :(得分:2)

您所有的div都是填充前30px和100%的填充-使得容器高度为100%+ 30px

尝试

height:  calc(100% - 30px);

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:  calc(100% - 30px);
  overflow: auto;
  color: black;
  padding-top: 30px;
}

#middleSection {
  position: absolute;
  height:  calc(100% - 30px);
  background-color: yellow;
  left: 175px;
  right: 175px;
  color: black;
  padding-top: 30px;
}

#rightSection {
  float: right;
  height:  calc(100% - 30px);
  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;
}
<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>