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
Brandon Stiles
12,305 PointsHow can I use flexbox in a nav-bar to display vector logo in the left, menu to the right, with white space between
Hey guys!
I'm trying to code a responsive horizontal nav bar using flexbox (see attached pic for final project), but I can't get the vector logo sized down and to the left.
When the screen width is phone-size, I've hidden the big menu, and am showing a mobile menu, which works great. Then, when the screen width expands, I hide the mobile menu and show the big menu, but I can't get the vector logo sized down and to the left. Here's my code:
HTML:
<nav> <!-- this wraps the navigation, logo and list items -->
<ol id="navList">
<li><img id="menuLogo" src="img/fullLogo.png"></li> <!-- here's the logo -->
<li><a href="index.html">HOME</a></li>
<li><a href="approach.html">APPROACH</a></li>
<li><a href="services.html">SERVICES</a></li>
<li><a href="portfolio.html">PORTFOLIO</a></li>
<li><a href="meetUs.html">MEET US</a></li>
<li><a href="blog.html">BLOG</a></li>
<li><a href="contact.html">CONTACT</a></li>
</ol>
</nav>
As you can see, I've tried putting the logo (the img file) in the to try and line it up. And here's my CSS (I have no idea how to properly code the logo. I want the whole nav bar to be about 15% of the screen at all times, with the logo resizing to fit inside it).
CSS:
nav{
width: 100%;
margin: auto;
background-color: white;
}
.navbar{
display: none;
}
.menuBig, #navList{
display: flex;
flex-direction: row;
justify-content: space-between;
width: 95%;
margin: auto;
}
#navList{
list-style: none;
font-family: 'Open Sans', sans-serif;
}
Basically, I want the navbar to look like Apple's website -- logo on the far, and then the menu to the far right.
Thanks so much for the help!
4 Answers
Steven Parker
243,318 PointsI looked at apple.com and I saw the header spaced out exactly like what you have now.
But, to do what you're describing, you could try this:
#navList li {
margin-left: 1em;
}
#navList li:first-child {
margin: 0 auto 0 0;
}
Or, you could do something like this:
#navList li {
flex: 1;
}
#navList li:first-child {
flex: 5;
}
Addendum
This goes with your revised issue below:
It looks like you lost some of the desktop settings when you created your mobile CSS, and everything unique for the destop needs to be inside a media query to work only when needed.
Here's a summary of the things I noticed:
- you turned off your logo in the mobile CSS and forgot to turn it back on for desktop
- you forgot to enclose your desktop settings in a media query (@media)
- you added some vertical mobile settings that must be overridden for horizontal
- you left out the
justify-contentsetting you had in the previous version - you forgot to use the margin settings that helped you last time
Here's a revised version of the desktop CSS file that addresses these issues (must be included last):
@media (min-width: 1024px) { /* this section only for larger screens */
/*------------------------ NAV -------------------------------*/
#navList {
display: flex;
flex-direction: row;
justify-content: space-between; /* this got left out from the original CSS */
width: 95%;
margin: auto;
}
#navList li {
margin: 0; /* the mobile setting forces space to be divided */
padding: 0; /* the mobile setting is not needed for desktop */
width: auto; /* the mobile setting takes up an entire line */
list-style: none;
border-bottom: none;
margin-left: 1em; /* a little space between items */
}
#navList li:first-child {
display: initial; /* show the logo (hidden for mobile) */
margin: 0 auto 0 0; /* maximize space to menu */
}
nav {
width: 100%;
margin: auto;
background-color: white;
}
}
Brandon Stiles
12,305 PointsGuil Hernandez , I've gone through your flex tutorial a few times, but still can't get this. What am I doing wrong?
HTML:
<nav>
<div class="menuBar">
<ol id="navList">
<li><img id="menuLogo" src="img/fullLogo.png"></li>
<li><a href="index.html">HOME</a></li>
<li><a href="approach.html">APPROACH</a></li>
<li><a href="services.html">SERVICES</a></li>
<li><a href="portfolio.html">PORTFOLIO</a></li>
<li><a href="meetUs.html">MEET US</a></li>
<li><a href="blog.html">BLOG</a></li>
<li><a href="contact.html">CONTACT</a></li>
</ol>
</div>
</nav>
Mobile CSS (1st):
/*------------------------ MENU -------------------------------*/
nav{
display: flex;
flex-direction: column;
text-align: center;
}
#navList li{
margin: auto;
padding: 4%;
font-size: 1.2em;
border-bottom: 1px solid rgba(87, 87, 87, .3);
width: 95%;
list-style: none;
}
#navList li:first-child{
display: none;
}
and desktop CSS:
/*------------------------ NAV -------------------------------*/
#navList {
display: flex;
flex-direction: row;
width: 95%;
margin: auto;
}
#navList li {
list-style: none;
border-bottom: none;
}
nav {
width: 100%;
margin: auto;
background-color: white;
}
When I code it this way, my logo doesn't show up at all, and when it does, it's not set left with the text menu set right. I was going for a horizontal menu like this on a desktop: http://scrapd.nl/old.html Thanks for any advice!
Steven Parker
243,318 PointsWhile you're waiting for Guil, I added a section to my answer above that you may find helpful.
Brandon Stiles
12,305 PointsSteven Parker good call. I've loaded it in and linked it here (let me know if it works or not):
Steven Parker
243,318 PointsThat's your private workspace URL, you can't share it and it doesn't exist when you're not using it.
Take a look at the instructional video at this link for how to make a snapshot. click that word
Brandon Stiles
12,305 PointsAh, I see. Very good! This should work then:
Steven Parker
243,318 PointsIt looks like when you added my width constraint suggestion, you put in an extra line forcing the height to be as tall as the window. If you use my suggestion without the addition, the height will automatically scale to look natural.
You have a few other odd effects going on resulting from mixed dimensional constraints between elements and their containers.
Brandon Stiles
12,305 PointsBrandon Stiles
12,305 PointsHey Steven! Thanks again for working with me on this. When I copy and paste that exact code, it gives me the following on my screen (all I did was change the MQ breakpoint from 1024 to 990 for my screen).
The logo stays big and won't compress to the size of the nav bar.
Steven Parker
243,318 PointsSteven Parker
243,318 PointsThat thing must be huge! I guess it needs to be constrained - try something like this:
#menuLogo { max-width: 100px; }For future issues, particularly when graphics are involved, it might be helpful to put your project into a workspace and then make a snapshot of it and post the link here.