0

How to expand the parent to have the same height as the child (the content) when the child has position: absolute ?

  • The height of the child is not static
  • Solution without javascript

.parent {
  position: relative;
  background: red;
}

.child {
  position: absolute;
  right: 10px;
  width: 100px;
  background: blue;
}
<div class="parent">
  <div class="child">
    test<br>test<br>test<br>test
  </div>
</div>
<div>
  some text below parent
</div>

(JsFiddle)

Cédric
  • 2,239
  • 3
  • 10
  • 28
clarkk
  • 27,151
  • 72
  • 200
  • 340
  • Do you need your child to be `position:absolute` or you just want it to be on the right side ? Because I don't think you can set the parent size depending on a absolutely positioned child – Cédric Dec 26 '22 at 14:18
  • The child must be absolute – clarkk Dec 26 '22 at 14:24

1 Answers1

-1
.parent {
    position: relative;
    background: red;
    width: max-content;
}

on the parent

Yosi Leibman
  • 386
  • 3
  • 16