0

I am building a gallery that is full screen and the

#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: Output

How to fit in the child div "divHolder" holder inside the "holder". I always do not understand the way the padding and margins work with child divs.

for reference, I have added the code jsfiddle

andreas
  • 16,357
  • 12
  • 72
  • 76
Yellow and Red
  • 695
  • 9
  • 31

1 Answers1

2

You can just use calc() to calculate the proper height for .divHolder. Because of your margin, the .divHolder exceeds the parent container because you set it to height: 100%;. Reduce the 100% by your top and bottom margin and it should fit:

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>
andreas
  • 16,357
  • 12
  • 72
  • 76
  • this code works fine.. but how to get it working for lower versions? I cant want to use JS too.. – Yellow and Red Oct 24 '16 at 13:34
  • 1
    Support isn't that bad, see http://caniuse.com#search=calc . You can use `box-sizing: border-box;` as alternative, see this post: http://stackoverflow.com/questions/16034397/css-calc-alternative – andreas Oct 24 '16 at 13:39