Home

WayneCarl Barker's Portfolio/ Blog

Go Back

Center absolutely positioned element

WayneCarl

September 19, 2022

Center absolutely positioned element

Please note the parent element must have position relative you are trying to center the element in the center of the parent

Method 1:

.somelement {
  inset: 0;
  margin: auto;
}

Method 2:

Please note this won't work on very old browsers

.somelement {
  position: absolute;
  left: 50%;
  -webkit-transform: translateX(-50%);
  transform: translateX(-50%);
}