Mobile Menu Dropdown Without Javascript
By: James Auble
Tags: Mobile Nav, Css Only Dropdown, Css Checkbox Toggle, Checked Psuedo Class, Adjacent Sibling

Our finished hamburger button tl;dr at step 6.

I had learned a while back how to toggle state using a <input type="checkbox"> and styling things by use of the :checked pseudo-class. At that time, I used what I had just learned to create a button that when clicked would get a drop-shadow.

I just dug it up if you wanna check it out:

Using only CSS to toggle state.

Well today I needed to build out a mobile menu for a hobby site i’m working on (www.hourofhistory.com) and decided I would employ this vanilla CSS technique for achieving it.

Here are the steps I took to get a fairly smooth functioning CSS-only mobile dropdown:

First, let’s write out our basic structure

<nav> comes after checkbox

It’s key that we add our <nav> after our checkbox because as of this writing, there are no selectors that target previous siblings and our styles to display/hide the <nav> will be made relative to our checkbox.

2. Next I’ll add some basic styling so that this demo doesn’t hurt my eyeballs so much.

Note: I’m embedding CSS selectors using SASS

The important thing to note about this technique is that we’re employing the use of a checkbox that has two states; checked and unchecked. We need the functionality of the checkbox without having its appearance on our page.

If you click anywhere on the hamburger button now your checkbox will become checked.

Sweet.

3. Now I want to add a box-shadow to the .hamburger-container when the button is clicked. Here’s the thing though — the button container is above the checkbox in the DOM-tree — and we’re only able to style siblings that come after our checkbox and any descendants of said siblings.

Then I realized — wait — the checkbox is now the same size as our button container and I can definitely toggle the checkbox’s styles.

So let’s now take advantage of the :checked psuedo-class and add a box-shadow and transition to target the box-shadow .

Our input[type="checkbox"] CSS will now look like this:

Now clicking on our hamburger menu anywhere should check the checkbox and trigger our depressed button box-shadow effect (if it doesn’t checkout the finished demo at the bottom of this article and compare).

4. We got a problem though — our checkbox is still visible and we want to hide that sucker. If we use the visibility: hidden or display: none properties, our checkbox box-shadow wont be visible, and with display: none it will become inoperable and completely useless. So instead let’s just remove any visible attributes of it.

Our CSS rule for our checkbox now hides the browser styles with -webkit-appearance: none; and appearance: none; and is now invisible but retains state — which is critical for our purpose.

5. Now we pretty much have our hamburger icon (with state now) squared away and we can turn our focus to the <nav> and how we’re gonna unhide it.

I forgot to add some actual content to my nav items so let’s do that first. My markup for my nav now looks like this:

6. We’re now ready to hook up our checkbox state to our <nav> element. I will provide the SCSS in a second, but let’s look at the compiled CSS which is really the crux of this walkthrough:

header .hamburger-container input[type=”checkbox”]:checked + nav {

display: block;

}

The CSS rule says “Hey, so like when the checkbox is checked I want to apply the following rules to the element that follows it” (we add nav for semantics but you could use + * just the same).

Now, when you reload your page there will be your <nav> element obscuring the view of your button. Not ideal, but we’re not done yet and you should notice that if you click where the button would be if you could see it, the <nav> should disappear.

Perfect! It’s working!

7. Okay, now let’s address this ugly <nav> that’s overlapping our hamburger button.

If we leave this thing in the normal document flow, it will linger in-or-around the .hamburger-container which is its parent. We don’t want that.

We want it to be full-width and positioned immediately below the header bar.

So let’s pop it out of the normal flow with position: fixed; .

In our example, the header always remains a height of 60px so let’s use top: 60px; to position our <nav> just below our <header> .

Expand the width of the <nav> to fill its container (the viewport) with width: 100%; and position it to the left edge with left: 0; .

Finally, center text with text-align: center; and get rid of the ugly list item discs with

ul {

list-style: none;

}

Add a yellow background to dropdown to visually associate it with the header and we should be good to go.

Here’s my CodePen of the result:

Hope this was helpful! Until next time…