Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS

Centering a Div Horizontally and Vertically using CSS

Hello,

I am trying to center a div horizontally and vertically using CSS. I have tried several methods methods but it didnt work

1 Answer

Steven Parker
Steven Parker
243,228 Points

There are a number of different alignment methods that can be used. One example is flexbox:

<div class="container">
  <div class="center"></div>
</div>
.container {
  height: 100vh;
  display: flex;
}
.center { 
  background: blue; 
  height: 200px;
  width: 200px;
  margin: auto;
}

If you are having trouble with a specific method, be sure to show your code and perhaps someone can point out a solution.