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
  • 346,931
  • 104
  • 581
  • 701
Raymond the Developer
  • 1,576
  • 5
  • 26
  • 57

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
  • 2,027
  • 3
  • 16
  • 30