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

Flexbox help

Hey guys,

I'm looking to create a complete layout using flexbox however I constantly run into issues with elements not centering. In this particular example I am looking to make the header center inside of the the "main" div. It seems when I use flexbox with in more than one element I run into issues. The weird thing is that "header" will align-self: center on the Y-axis but for some reason will not center with justify content on the X-axis. Any help with this or what im doing wrong in flexbox would be greatly appreciated!

heres my ``` html <body> <div class="container"> <div class="nav"> <center> Navigation </center> </div> <div class="main"> <div class="header"> <h1> New York</h1> </div> </div> </div>

here is my ``` css

.container,
.main,
.header {
  display: flex;
  flex-direction: row;
}
.nav {
  width: 250px;
  height: 100vh;
  margin-right: 10px;
  background: blue;
  flex: 0 20%;
}
.main {
  background: green;
  flex: 1;
}
.header {
  justify-content: center;
} ```

Here is a better display of your code, I'm gonna take a look at it in my browser and see what is going on:

HTML

<body> 
    <div class="container"> 
        <div class="nav"> 
            <center> Navigation </center> 
        </div> 
        <div class="main"> 
            <div class="header"> 
                <h1> New York</h1> 
            </div> 
        </div> 
    </div>
</body>

CSS

.container, 
.main, 
.header { 
    display: flex; 
    flex-direction: row; 
}

.nav { 
    width: 250px; 
    height: 100vh; 
    margin-right: 10px; 
    background: blue; 
    flex: 0 20%; 
}

.main { 
    background: green; 
    flex: 1; 
}

.header { 
    justify-content: center; 
}

5 Answers

Try this, is this what you are looking to do? I added flex: 1; to your .header class. This will center it at the top.

.header {
    flex: 1;
    justify-content: center;
}

This will center header on the x and y axis:

.header {
    flex: 1;
    justify-content: center;
    align-items: center;
}

I ran into that problem with my site!

Have you tried setting the margins to auto?

Hey Melisa,

thanks for your help. Taking the "center" tags off of header and setting the header to margin: 0 auto worked.

However I'd still like to know how to control flexbox a little better.

Were you able to get a good handle on flexbox? and how if so?

Thanks

Hey melisa,

I found this link too seems to be pretty good so far!

http://flexbox.io

Your welcome. :)

I honestly have not. I'm still trying to figure it out myself.

The best resource I have found though is here: https://css-tricks.com/snippets/css/a-guide-to-flexbox/.

Let me know if you have found anything!

I'll second Melisa's link to the css-tricks site. I just took notes on that and if you want to practice use this:

Flexbox Froggy

That is amazing! Thanks for the link, Mike!

That was a great demonstration!

I'll second Melisa's link to the css-tricks site. I just took notes on that and if you want to practice use this:

Flexbox Froggy