d
Amit DhamuSoftware Engineer
 

3 ways to center a div

1 minute read 00000 views

HTML element

<div class="center-me">Hello world!</div>

Method 1

html,
body {
  height: 100%;
}

.center-me {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

Method 2

.center-me {
  display: grid;
  place-items: center;
  min-height: 100vh;
}

Method 3

.center-me {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}