Full CSS Responsive Navigation Toggle, No Javascript


A short tutorial to do responsive/mobile navigation toggle with only CSS (no javascript/jquery needed).

I do understand, to disable javascript in browser is not common anymore, and more website rely on javascript to work properly. But it’s interesting subject isn’t it? We can use this as a fallback if javascript is disabled.


the navigation html is very simple and basic navigation in WordPress.

<nav id="menu-primary">

    <div class="menu-container">

        <div id="menu-toggle-primary">
            <a class="open-menu-primary" href="#menu-primary"><span>Open this menu</span></a>
            <a class="close-menu-primary" href="#"><span>Close this menu</span></a>
        </div><!-- .menu-toggle -->

        <div class="wrap">
            <ul class="menu-items" id="menu-primary-items">
                <li class="menu-item"><a href="#">Menu 1</a></li>
                <li class="menu-item"><a href="#">Menu 2</a></li>
                <li class="menu-item"><a href="#">Menu 3</a>
                    <ul class="sub-menu">
                        <li class="menu-item"><a href="#">Menu 3.1</a></li>
                        <li class="menu-item"><a href="#">Menu 3.2</a></li>
                <li class="menu-item"><a href="#">Menu 4</a></li>
        </div><!-- .wrap -->

    </div><!-- .menu-container -->


as you can see, before menu items, we create a div for navigation toggle, and inside the div we have 2 link, one for opening the menu, and one for closing the menu.

        <div id="menu-toggle-primary">
            <a class="open-menu-primary" href="#menu-primary"><span>Open this menu</span></a>
            <a class="close-menu-primary" href="#"><span>Close this menu</span></a>
        </div><!-- .menu-toggle -->

CSS (before toggle, closed state)

Default css is simple, we just need to hide our menu ul#menu-primary-items and hide the link to close the navigation because it’s already closed (hidden).

/* Hide Menu */
    display: none;
#menu-toggle-primary a.close-menu-primary{
    display: none;

CSS (toggle open)

As you can see in the demo/html source, in the link to open the toggle linked to #menu-primary element. So when we click the link, the browser will go to http://yourpageurl/#menu-primary.

Now the interesting part, we can actually style the targeted element using CSS :target selector.

URLs with an # followed by an anchor name, link to a certain element within a document. The element being linked to is the target element.

The :target selector can be used to style the current active target element.

So, if we add this CSS it will only do stuff for selected element when it’s active (targeted using #element in url).

    /* do stuff */

we are going to use :target CSS selector and we can do something like this:

/* Display Menu Items */
#menu-primary:target #menu-primary-items{
    display: block;
/* Hide Open Toggle Link */
#menu-primary:target #menu-toggle-primary a.open-menu-primary{
    display: none;
/* Show Close Toggle Link */
#menu-primary:target #menu-toggle-primary a.close-menu-primary{
    display: block;

At this state, the menu items will be displayed, and the link to open the menu will be hidden.


Using only CSS will make the page “jump”

It’s unavoidable, using javascript/jquery we can prevent this using event.preventDefault(). in the demo I add the navigation at the top of the page to prevent the “jumping” experience. If you create the navigation toggle in other position (for example, after the header), you can prevent/make it less jumpy by linking the to the right/nearest div/element id.

Browser Support

:target CSS is supported by all major browser, but not supported in Internet Explorer 8, and earlier versions.

I hope this simple explanation is enough 🙂
If you have any question feel free to ask in the comment.


  1. Tanja lee


    thanks for a great tuturial. I have one question though. How do I make the navigation fixed? When I add ‘position:fixed’ to the navigation, I can’t scroll on my mobile.



    • David

      maybe you can try:

      #menu-primary {
      	position: fixed;
      	top: 0;
      	width: 100%;
      	max-width: 600px;
      #container {
      	position: relative;
      	margin: 0 auto;
      	width: 100%;
      	max-width: 600px;
  2. Anna

    This is great, is there a way of making the list horizontal instead? so it would open across the top?

  3. Santana

    Hi Shellcreeper, I like your tutorial very much and I am trying to addapt it to my template, but I have one problem that is when I move the open & close buttons to a other div I can’t get them to target the menu anymore.

    Here is a Fiddle of what I am trying to do http://jsfiddle.net/SanMoll/k8ej4fmo/2/

    Hope you can give some advice, thanks

  4. Elias

    Very useful implementation, but I have an observation.
    Probably has been fixed but here goes:
    If you open and close the menu, say 7 times, it ‘ll take you equal amount of times pressing the BACK browser button just to go to the previous website.

  5. Tonguc

    Thank you so much for this tutorial! I like it because, it’s so easy & without JS. Even though one question. On my Side if I open the menu, it jumps to top. I tried with “position:fixed;” and “onclick=return false;” No chance! Do you have any Idea, how can I fix the problem?
    Thanks again


Leave a Reply

Your email address will not be published. Required fields are marked *