javascript - 宽度 : 100% is not working on children div if parent div has width: calc(100% - 300px)?

标签 javascript html css

我有以下安排: 我有父 divcontainerwidth: 100%; 还有 htmlbody width: 100%,到目前为止它按预期工作。 但是在 divcontainer 里面,我有两个 div,一个 divwidth: 300px , 和其他具有 width: calc(100% - 300px),

的 div

mainpage 类的父类有 width: calc(100% - 300px),子类 divpage宽度:100%; 但是 width: 100% 在这个 div 上不起作用?

即使父 div width 是行列式。

代码:https://jsfiddle.net/tj8k0nnw/1/

.container {
  width: 100%;
  background-color: #d5d5d5;
}

.sidebarcontainer {
  width: 300PX;
  height: 2000px;
  display: inline-block;
  box-sizing: border-box;
  padding: 5px;
  padding-right: 2px;
}

.innersidebarcontainer {
  position: relative;
  width: 100%;
  height: 100%;
}

.sidebar {
  width: 293px;
  background-color: white;
  height: 700px;
  top: 1px;
  position: absolute;
}

.mainpage {
  width: calc(100% - 300px);
  padding: 5px;
  padding-right: 2px;
  height: 3000px;
  display: inline-block;
  box-sizing: border-box;
  background-color: teal;
}

.page {
  width: 100%;
  width: 100%;
  background-color: white;
}

.footer {
  height: 500px;
  width: 100%;
  margin: 0 auto;
  background-color: purple
}

.test1 {
  background-color: red;
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  height: 200px;
}

.test2 {
  background-color: red;
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 200px;
}
<div class="container">
  <div class="sidebarcontainer">
    <div class="innersidebarcontainer">
      <div class="sidebar">
        <div class="test1"></div>
        <div class="test2"></div>
      </div>
    </div>
  </div>
  <!--
    -->
  <div class="mainpage">
    <div class="page"></div>
  </div>
</div>
<div class="footer"></div>

最佳答案

提供的代码有效。您尚未为 page 指定高度,因此高度为 0。为其指定高度会使其可见:https://jsfiddle.net/kvjw9vhm/

.container{
  width: 100%;
  background-color: #d5d5d5;
}
.sidebarcontainer{
  width: 300PX;
  height: 2000px;
  display: inline-block;
  box-sizing: border-box;
  padding: 5px;
  padding-right: 2px;
}
.innersidebarcontainer{
  position: relative;
      width: 100%;
    height: 100%;
}
.sidebar{
  width: 293px;
  background-color: white;
  height: 700px;
  top: 1px;
  position: absolute;
}
.mainpage{
  width: calc(100% - 300px);
  padding: 5px;
  padding-right: 2px;
  height: 3000px;
  display: inline-block;
  box-sizing: border-box;
  background-color: teal;
}
.page{
  width: 100%;
  height: 100%; /* was width: 100%; */
  background-color: white;
}
.footer{
  height: 500px;
  width: 100%;
  margin: 0 auto;
  background-color: purple
}
.test1{
  background-color: red;
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  height: 200px;
}
.test2{
  background-color: red;
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 200px;
}
<body>
  <div class="container">
    <div class="sidebarcontainer">
      <div class="innersidebarcontainer">
        <div class="sidebar">
          <div class="test1"></div>
          <div class="test2"></div>
        </div>
      </div>
    </div>
    <div class="mainpage">
      <div class="page"></div>
    </div>
  </div>
  <div class="footer"></div>
</body>

关于javascript - 宽度 : 100% is not working on children div if parent div has width: calc(100% - 300px)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45370880/

相关文章:

javascript - 有没有更简洁的方法在javascript中编写css边距字符串

javascript - 尽管 API 已激活,但尝试使用嵌入 map API 时出现 ApiNotActivatedMapError

html - 如何在 Bootstrap 3 中将 n 行居中?

html - IE 中 div 中未对齐的垂直分页器

javascript - 重新排序单选按钮

javascript - 使用CSS在容器内移动元素

html - 编辑 "alt"文本

javascript - MeanJS - 将 Angular 集成到 index.html 文件中

javascript - ExtJS ComboBox(配置了 queryMode remote 和 forceSelection true)自动清除输入字段

javascript - 如何将字符串渲染为自定义 React 组件?