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
Shawn Benn
8,070 PointsFlexbox 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;
} ```
5 Answers
Jeffery Austin
8,128 PointsTry 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;
}
Melisa Hamilton
11,729 PointsI ran into that problem with my site!
Have you tried setting the margins to auto?
Shawn Benn
8,070 PointsHey 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
Shawn Benn
8,070 PointsMelisa Hamilton
11,729 PointsYour 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!
Mike West
9,163 PointsI'll second Melisa's link to the css-tricks site. I just took notes on that and if you want to practice use this:
Melisa Hamilton
11,729 PointsThat is amazing! Thanks for the link, Mike!
David Wood
5,705 PointsThat was a great demonstration!
Mike West
9,163 PointsI'll second Melisa's link to the css-tricks site. I just took notes on that and if you want to practice use this:
Jeffery Austin
8,128 PointsJeffery Austin
8,128 PointsHere 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
CSS