DEV Community

Discussion on: Center Element Using CSS

Collapse
 
alohci profile image
Nicholas Stimpson • Edited

Some other old-school methods from the time before flexbox that spring to mind.

Use position:absolute with margins.

.child {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    margin: auto;
    width: -moz-fit-content;
    width: fit-content;
    height: 0;
}

And the ghost ::before inline-block box;
.parent {
  text-align: center;
  font-size:0;
}
.parent::before {
  content: '';
  height: 100vh;
  vertical-align: middle;
  display: inline-block;
}
.child {
  vertical-align: middle;
  display:inline-block;
  font-size:1rem;
}

And the line-height method
.parent {
  line-height:100vh;
  text-align:center;
}
.child {
  display:inline-block;
  line-height:1.2rem;
}