DEV Community

Ankit Verma
Ankit Verma

Posted on

How can You horizontally center an element?

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

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more