0

I want the blue div to not overflow the red div but always fill parent. I check on stackoverflow and they advised using height:100% but since the child have padding, it overflows. How to do that without changing the style of class parent ?

.parent {
  background: red;
  height: 150px;
  width:300px;
}
.child{
  height: 100%;
  padding:20px;
  width:100px;
 background: blue; 
}
<div class='parent'>
  <div class='child'>
  
  </div>
  </div>
TSR
  • 17,242
  • 27
  • 93
  • 197

2 Answers2

1

Add box-sizing: border-box; more info

* {
    box-sizing: border-box;
}

* {
    box-sizing: border-box;
}
.parent {
  background: red;
  height: 150px;
  width:300px;
}
.child{
  height: 100%;
  padding:20px;
  width:100px;
 background: blue; 
}
<div class='parent'>
  <div class='child'>
  
  </div>
  </div>
Lalji Tadhani
  • 14,041
  • 3
  • 23
  • 40
1

You can use box-sizing: border-box; or you can calculate height of children like height: calc(100% - 40px */ Your padding */

Solution1

* {
  box-sizing: border-box;
}

.parent {
  background: red;
  height: 150px;
  width: 300px;
}

.child{
  height: 100%;
  padding: 20px;
  width: 100px;
  background: blue;
}

Solution2

.parent {
  background: red;
  height: 150px;
  width: 300px;
}

.child{
  height: calc(100% - 40px);
  padding: 20px;
  width: 100px;
  background: blue;
}
TOMBA
  • 205
  • 1
  • 11