DEV Community

Discussion on: The Ultimate Guide to Center Align - CSS

Collapse
 
benpai profile image
Benji Grant

I do want to add that instead of negative margins, if the size of the element is known, calc is a much better system.

.center {
  position: absolute;
  height: 300px;
  width: 300px;
  top: calc(50% - 150px);
  left: calc(50% - 150px);
}
Enter fullscreen mode Exit fullscreen mode

Of course you can use an extra calc to make it a bit more generic if you're using variables for your height and width, but this method allows you to use margins on your element as well, which can be useful if you don't want it touching the edges.

Collapse
 
venkyakshaya profile image
Akshaya Venkatesh

Useful tip! Thanks for sharing!