0
<div style="height:100px;width:100px;border:1px solid black"></div>

How can I position this div in the center?

Michael Mior
  • 28,107
  • 9
  • 89
  • 113

3 Answers3

1
.parent {
  position: relative;
}

.child {
  width: 300px;
  height: 100px;
  padding: 20px;

  position: absolute;
  top: 50%;
  left: 50%;

  margin: -70px 0 0 -170px;
}

More you find here.

Jan Giacomelli
  • 1,299
  • 11
  • 23
0

On the parent of that div:

div.parent {
  display: flex;
  justify-content: center;
  align-items: center;
}
Pytth
  • 4,008
  • 24
  • 29
0

You can absolutely position the element with top: 50%; left: 50%; then use translate() to move it back/up 50% of it's own width to put it in the absolute center.

div {
  position: absolute;
  top: 50%; left: 50%;
  transform: translate(-50%,-50%);
}
<div style="height:100px;width:100px;border:1px solid black"></div>
Michael Coker
  • 52,626
  • 5
  • 64
  • 64