:checked
hackTo open the submenus we’ll use checkboxes. If you don’t know how the checkbox hack works, just google it, there are some really good posts that go into much detail. But basically we will depend the visibility of the submenu on the state of the checkbox placed before it.
So we change our markup to look like this:
The opening will be done using this CSS
Now we’ll just have to style it. I went with a pretty standard bar.
And that will look like this:
See the Pen Checkbox-Hack Dropdown (non-responsive) by Myri ( @mynimi) on CodePen.
For the responsive version we’ll have to adjust some more of the markup. We add another checkbox to toggle the whole navigation. And then we just adjust the CSS to create this off-canvas Menu
And that’s what it looks like:
In the example I put the breakpoint at 2000px so that it shows up on all stanard displays. In real life you would have to adjust this, so that the non-responsive version will be shown first.
See the Pen Checkbox-Hack Dropdown (responsive) by Myri ( @mynimi) on CodePen.
And that’s it.
This is the question. If you should actually go with a solution like this. Well, in case you have to solve this problem without JavaScript - Go with this. In Reallife, though, the javascript solution is the one I would go with. Reason for that being the fact, that it is written faster and all-over support is a little better. Still, I like trying to tackle some problems without the use of JavaScript, just to see how far you can come with just HTML and CSS.