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
  • 307,417
  • 93
  • 525
  • 644
Raymond the Developer
  • 1,476
  • 5
  • 23
  • 55

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,958
  • 2
  • 13
  • 27