Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.


Responsive Menu made with Jquery

This is a two-part question.

Part One: For a site I'm working on I'm trying to optimize the mobile navigation using jquery. I've got it pretty much how I want the functionality but there is one weird thing that I have noticed.

While testing on my desktop I noticed that if I resize the browser window to look at the mobile view, and I open / close the nav and then resize the window back to normal I'm getting a weird glitch in how the desktop navigation displays.

Part Two: For some reason my phone isn't pulling up the mobile view but the desktop version of the site instead. Am I doing something wrong in the css here(see below)? I'm obviously still working on the styling of the site for smaller screens as things are broken.

To view the issue I've uploaded the site to my server: http://mattstewart.co/caferegular/

Since this is not live I have a password set up for viewing: Username: mattsguest Password: welcome

Here is the code I'm using:

        <a href="index.php"><img id="logo" alt="Cafe Regular Logo" src="img/Logo.png"></a>
                <li><a href="#">Design</a></li><li>
                <li class="divider">&#8226;</li><li>
                <a href="#">Shop</a></li><li>
                <li class="divider">&#8226;</li><li>
                <a href="spotlight.php">Spotlight Regular</a></li><li>
                <li class="divider">&#8226;</li><li>
                <a href="#">Press</a></li><li>
                <li class="divider">&#8226;</li><li>
                <a href="#">Videos</a></li>

/* //Modify CSS to hide links on smaller widths and show button and select
//Also hides select and button on larger widths and show links */

@media (min-width: 320px) and (max-width: 640px) {
    .divider {
        display: none !important; 
    nav {
        max-width: 100%;
        margin: auto;
        background: #fff;
        text-align: center;
        li {
            display: none;

@media (min-width: 640px) {
    header {
        #logo {
            width: 275px;
            padding: 50px 0;
    nav {
        max-width: 100%;
        margin: auto;
        background: #fff;
        text-align: center;
            li {
                display: inline-block;
                a {
                    padding: 0 50px;
            button {
                display: none !important;

//Create Menu button to show nav links
var $menu = $("<button class='mobileMenu'>Menu</button>");
$("nav ul").append($menu);

//Create close button to hide nav links again
var $close = $("<button class='close'>Close</button>");
$("nav ul").append($close);

//Hide close until menu is clicked

//Click menu button to reveal nav links // Hide Menu btn // Show Close btn
$menu.click( function() {
    $("nav ul li").slideDown();

//Click close button to hide nav links again // Hide Close btn // Show Menu btn
$close.click( function() {
    $("nav ul li").slideUp();

Any help here would be much appreciated! Thanks!!

2 Answers

Hey man,

I can answer part two with some confidence..

In your head section, have you included a meta tag for viewport?

<meta name="viewport" content="width=device-width, initial-scale=1.0" />

There are a couple of different things you can pass in, but I think the most basic (above) should get you started.

Hey Tom!

Thanks for the help! That solved part 2 for me.

I've just taken a look at your site, and it appears that you need to add some window.width listeners.

In a noted form:

on window.resize

run check to see if window.width is above/below 700px if it is above 700px

  • you're on desktop so change back to desktop navigation
  • close anything that's open
  • unappend anything that's appended etc

if it is below 700px

  • you're on mobile so change to mobile navigation
  • reappend
  • do other things

You can achieve exactly the same without so much javascript. It it was me, I wouldn't change any html elements between desktop and mobile. Instead I would keep the same html and change only the css to create an inline/normal list.

All you need to do then is show/hide with javascript, but again you'll need to make sure that when you resize javascript checks to see if it should be hiding/unhiding the navigation.

I just dug up some old script I used when I did it.. It's not work showing it all because it was a long time ago and pretty ugly!

        if(window.innerWidth <= 960) {

Hey Tom! You're the man — thanks so much for taking the time to look at my site / provide me with great feedback and answers. I'll look into adding the window.width listeners to trim down the code.

No worries, just one more thing that kept on getting me - if you have a function for the navigation which is something along the lines of "whatShouldILookLikeRightNow" make sure you call it no only on resize but also on page load. You could play with doing it via css classes of hide/show, using javascript to append the classes where applicable. If you choose to do it this way, make sure you don't hide anything by default in case javascript is turned off. Otherwise the user won't be able to see any links to click on!

I honestly have no idea which option is best.. I imagine there's not much in it!