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

JavaScript

Responsive Navigation: Having issues Implementing toggle menu to display

Hey guys

So Ive been following this pretty 'simple' tutorial by smashing magazine [http://mobile.smashingmagazine.com/2013/09/11/responsive-navigation-on-complex-websites/ link]

It shows two options, I'm referring to the second responsive implementation the "toggle to display". I feel like im pretty close and I looked at the original source code from the site he references in his tutorial to fill in any blanks the tutorial doesn't explain [http://www.senecacollege.ca/ link]

The reason why I think its a javascript issue is because that script should apply a style tag to the nav element initially but when I inspect the page that isn't the case.

HTML

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Responsive Navigation Test</title>
        <link rel="stylesheet" type="text/css" href="css/style.css">
    </head>
    <body>
        <div id="mobileNav">
            <a id="mobileNavIcon" href="#"><img src="assets/3bar-menu.png"   width="20px" height="100%" alt="menu-icon"/></a>
        </div>
        <nav>
            <ul>
                    <li><a href="#">Profile</a></li>
                    <li><a href="#">Porfolio</a></li>
           </ul>
        </nav>
        <script src="js/toggle.js"></script>
    </body>
</html>

CSS:

#mobileNav {
    display: none;
}

@media only screen and (max-width: 600px) {
    #mobileNav {
        display:block;
    }

    nav {
        display: none;
        width: 100%;
        background: #000;
        margin-left: -20px;
        padding-right: 40px;
    }

    nav li {
        display: block;
        margin: 20px 0 20px 20px;
        width: 100%;
    }

    nav a {
        color: #FFF;
        margin: 0;
        width: 100%;
    }
}

JAVASCRIPT

$('#mobileNavIcon').click(function () {
    $('nav').slideToggle();
    return false;
});

If you guys can help me understand what I'm missing would be greatly appreciated!

Thanks in advance :)

What seems to be the issue you are having, the code works. Try creating a codepen and pointing out any issues you see with it.

The issue I'm having is when I click on link to get the menu to drop down it doesn't drop down at all.

I made a codepen account and hopefully marked it up right so it makes sense. I replaced the png I was using for the 3 line menu icon with the unicode instead

[http://cdpn.io/dKjeh link]

Just taking a quick look it seems like you are overriding your ''' #mobileNav { display:block; } ''' with a more specific rule in ''' nav { display: none; <----- more specific width: 100%; background: #000; margin-left: -20px; padding-right: 40px; } '''

Just taking a quick look it seems like you are overriding your

#mobileNav { 
    display:block; } 

with a more specific rule in

nav { 
display: none; <----- more specific 
width: 100%; 
background: #000; 
margin-left: -20px; 
padding-right: 40px; 
}

I would really love to be able to edit my replies :) sorry for the triple post. If the code formatting doesn't work this time I will leave it be.

Just taking a quick look it seems like you are overriding your

#mobileNav { display:block; } 

with a more specific rule in

nav { 
  display: none; <----- more specific 
  width: 100%;
  background: #000;
  margin-left: -20px;
  padding-right: 40px;
} 

Nah thats cool man.

Though I am still confused. Why would my 'mobileNav' be overwritten in my css by my 'nav' when there two separate elements? Also I'm under the assumption that the javascript will change the css property of nav to display block when clicking on the #mobileNav element.

Cheers man

it wouldn't....I need to stop looking at code on my way out the door :)

it looks like you got it. I made a code pen of it and minus the 3-bar graphic it appears to be working.

http://codepen.io/Bluediv/pen/pChlc

Could you explain to me what you changed to make it work, I must be blind! :p

nothing! just cut and pasted your code :)

Dumb question here, but were you sizing your browser window down below 600px?

wow thats super strange! sort of feel like I didn't learnt anything with this problem but im glad that I got your codepen at least as reference

1 Answer

Yeah I was! This is weird.

I can't think of anything else apart from my javascript not linking properly in sublime text 2. I feel like I didn't really learn anything in this situation but oh well! hahaha. Thanks a bunch guys :)