Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unable to add height on child when I add position relative on parent

I want to add a height to child div but when I add position: relative to parent then it doesn’t work.

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.parent-box {
  position: relative;
  background-color: red;
  width: 100%;
  height: 100%;
  padding: 2rem;
  top: 0;
  left: 0;
}

.child-box {
  position: absolute;
  width: 100%;
  height: 100%;
  background-color: green;
  top: 0;
  left: 0;
}
<div class="parent-box">
  <div class="child-box">
    <h4>child box</h4>
  </div>
</div>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You need to set your root element and body to take up 100% of the height.

Explanation:
Your parent box had a height of 100%, that is the full height of its parent – the body. You hadn’t set a height on the body, and its initial height is auto. Therefore your height was auto, only taking up the space needed to fit the content and padding.

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

.parent-box {
  position: relative;
  background-color: red;
  width: 100%;
  height: 100%;
  padding: 2rem;
  top: 0;
  left: 0;
}

.child-box {
  position: absolute;
  width: 100%;
  height: 100%;
  background-color: green;
  top: 0;
  left: 0;
}
<div class="parent-box">
  <div class="child-box">
    <h4>child box</h4>
  </div>
</div>

Here is an improved solution with less code:

Width 100% is default for block elements, inset can be used for the child div instead of height and top/left, top and left have no effect on relative positioned elements so remove them from the parent.

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

.parent-box {
  position: relative;
  background-color: red;
  height: 100%;
  padding: 2rem;
}

.child-box {
  position: absolute;
  background-color: green;
  inset: 0;
}
<div class="parent-box">
  <div class="child-box">
    <h4>child box</h4>
  </div>
</div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading