How can You horizontally center an element?

Ankit Verma - Feb 27 - - Dev Community

With flex it is very easy to style the div horizontally and vertically centered.

#centered {  
  border: 0.05em solid black;
}

#main {
  border: 0.05em solid red;
  width:100%;
  display: flex;
  justify-content: center;
}
Enter fullscreen mode Exit fullscreen mode
<div id="main">
  <div id="centered">Foo foo</div>
</div>
Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . .