0

How can I center/middle a child element with position absolute. On a parent that has display flex?

<div class="parent">
  <div class="child">
    Object
  </div>
</div>

.parent{
  display: flex;
  align-items: center;
}

.child{
  position: absolute;
}

The child element has to overlap the parent.

Michael Benjamin
  • 265,915
  • 79
  • 461
  • 583
Raymond the Developer
  • 1,324
  • 2
  • 18
  • 50

1 Answers1

1

Try this

.parent{
display: flex;
   justify-content: center;
   align-items: center;
   position: absolute;
   width:100%;
   height: 100%;
}

.child{
  position: absolute;
  background-color:#f00;
  border:1px solid #333;
  
}
<div class="parent">
  <div class="child">
    Object
  </div>
</div>
CodeZombie
  • 1,754
  • 12
  • 22