子元素上的填充移动父元素 - CSS

时间:2016-10-24 13:13:22

标签: html css css3 margin padding

我正在构建一个全屏的画廊和

#cover {
  position: absolute;
  width: 500px;
  height: 500px;
  background: rgba(44, 69, 128, 0.65);
  z-index: 10;
}
.divHolder {
  background: #fff;
  color: #2c4580;
  position: relative;
  box-shadow: 2px 2px 2px #333;
  padding: 10px;
  margin: 20px 20px;
  overflow: auto;
  height: 100%;
}
.divHolder div:hover {
  border: 10px solid #000;
}
.divHolder div {
  width: 100px;
  height: 100px;
  background: black;
  padding: 10px;
  display: inline-block;
  cursor: pointer;
  border: 10px solid white;
}
<div id="cover">
  <div class="divHolder">
    <div>image</div>
    ...
    <div>image</div>
  </div>
</div>

输出: Output

如何在“持有人”内部装入子div“divHolder”持有者。我总是不明白填充和边距与子div的结合方式。

供参考,我添加了代码jsfiddle

1 个答案:

答案 0 :(得分:2)

您可以使用calc()来计算.divHolder的正确高度。由于您的保证金,.divHolder超出了父容器,因为您将其设置为height: 100%;。通过上下边距减少100%,它应该适合:

height: calc(100% - 40px);

#cover {
  position: absolute;
  width: 500px;
  height: 500px;
  background: rgba(44, 69, 128, 0.65);
  z-index: 10;
}
.divHolder {
  background: #fff;
  color: #2c4580;
  position: relative;
  box-shadow: 2px 2px 2px #333;
  padding: 10px;
  margin: 20px 20px;
  overflow: auto;
  height: calc(100% - 40px);
  box-sizing: border-box;
}
.divHolder div:hover {
  border: 10px solid #000;
}
.divHolder div {
  width: 100px;
  height: 100px;
  background: black;
  padding: 10px;
  display: inline-block;
  cursor: pointer;
  border: 10px solid white;
}
<div id="cover">
  <div class="divHolder">
    <div>image</div>
    <div>image</div>
    <div>image</div>
    <div>image</div>
    <div>image</div>
    <div>image</div>
    <div>image</div>
    <div>image</div>
    <div>image</div>
    <div>image</div>
    <div>image</div>
    <div>image</div>
  </div>
</div>