DEV Community

Cover image for Responsive Navbar without Javascript

Responsive Navbar without Javascript

Shubham Tiwari on December 22, 2022

Greetings to all. Today I'll demonstrate how to make a responsive navbar without Javascript that has a dropdown effect for mobile devices. The majo...
Collapse
 
violacase profile image
violacase

When you incorparate a polyfill when deploying you can be certain legacy browsers will work. Best way that work for me is to use PostCSS-plugins in the workspace.

Collapse
 
shubhamtiwari909 profile image
Shubham Tiwari

I have to learn about PostCSS

Collapse
 
violacase profile image
violacase

Make it one of your near future learning goals. Developing is SO much more fun with a good eco system. (VSCode, Vite, PostCSS)
PostCCS is not too difficult. It does the hard work fór you!

Thread Thread
 
shubhamtiwari909 profile image
Shubham Tiwari

I checked PostCSS and it's cool
Also I found it is used with TailwindCss as well.
I worked with Webpack but will check vite too.

Thread Thread
 
violacase profile image
violacase

Vite is GREAT for MANY good reasons. Check it out!
You'll never want to use webpack anymore. Webpack is old school. Don't believe me. Believe wat you'll encounter with Vite!

Thread Thread
 
shubhamtiwari909 profile image
Shubham Tiwari

Yeah i would use it for sure
Thanks for the guidance

Thread Thread
 
violacase profile image
violacase

Merry Christmas and a Happy New Year by the way,

Thread Thread
 
shubhamtiwari909 profile image
Shubham Tiwari

Merry Christmas and Happy New Year to you too

Collapse
 
lissy93 profile image
Alicia Sykes • Edited

Nice article, but the burger button doesn't seem to work in the demo.
~Possibly it's a Chrome-only feature you're using, in which case adding a fallback would be good.~
Ah just seen the can i use link, probably best to avoid using :has as it's not at all well supported.

Collapse
 
shubhamtiwari909 profile image
Shubham Tiwari

Are you using Firefox as your browser?

Collapse
 
lissy93 profile image
Alicia Sykes

Yeah

Collapse
 
lissy93 profile image
Alicia Sykes

You can use the @supports selector to add the functionality for the remaining browsers.

This would be important, as looks like :has isn't supported on many mobile browsers - and that's the only time the burger menu will be visible. So using it alone in this situation, is pretty much useless!

Collapse
 
shubhamtiwari909 profile image
Shubham Tiwari

Yeah for the fallback i have to provide something as i didn't have used javascript, i have to build the logic in css for the same

Collapse
 
zoppatorsk profile image
Zoppatorsk

As seen on caniuse this do not work on firefox... and not on firefox mobile either. My own testing also shows it does not work on firefox but do work in chrome.

Similar way of doing it is possible but without using ":has". Would suggest using that or JS until browser support is up to snuff if going to put a site online. But yeah, interesting to see some new CSS stuff being used.

Collapse
 
shubhamtiwari909 profile image
Shubham Tiwari • Edited

Yeah that's why i put the caniuse link because the browser support is not vast for has selector But the power it gives in css ,
firefox and other browser will make it supportable in near future

Collapse
 
mstewartgallus profile image
Molly Stewart-Gallus

This isn't accessible.

You want to set aria-expanded and other attributes properly.

A details element almost works but still has problems.

Also please use a nav labelled by a heading and do not put interactive elements inside a button. Interactive elements inside a button are ignored by the accessibility tree.

Something like the following might be a better start towards a better nav menu.

<nav aria-labelledby=""menu-title">
  <header>
    <h2 id="menu-title">Menu</h2>
    <button id="menu-button" aria-expanded="false" aria-controls="navigation">Open/Close Menu</button>
  </header>
  <ul id="navigation">
    <li><a href="#">Home</a></li>
    <li><a href="#">About</a></li>
    <li><a href="#">Contact</a></li>
  </ul>
</nav>
~~~
Enter fullscreen mode Exit fullscreen mode
Collapse
 
shubhamtiwari909 profile image
Shubham Tiwari

Sure i will take care of it

Collapse
 
raibtoffoletto profile image
Raí B. Toffoletto

Nicely done! Kudos 🎉 Hopefully Firefox will stop this silliness of not supporting :has out of the box

Collapse
 
shubhamtiwari909 profile image
Shubham Tiwari

Yeah right 😂

Collapse
 
stevedev profile image
Stephen Gachoki

Great creation 👍👌

Collapse
 
shubhamtiwari909 profile image
Shubham Tiwari

Thank you

Collapse
 
immayurpanchal profile image
Mayur Panchal

Interesting! Using checkbox to create the toggle 🔥

Collapse
 
shubhamtiwari909 profile image
Shubham Tiwari

Thank you

Collapse
 
janosongithub profile image
JANOSongithub

Thank You! Nice work.

Collapse
 
shubhamtiwari909 profile image
Shubham Tiwari

Welcome

Collapse
 
yoquiale profile image
Alex

You should use min-width media queries instead, it makes coding responsive stuff easier.

Collapse
 
shubhamtiwari909 profile image
Shubham Tiwari

Yeah that's also good

Collapse
 
telegramtracker profile image
Telegram Tracker

good idea!

Collapse
 
shubhamtiwari909 profile image
Shubham Tiwari

Thanks

Collapse
 
shubhamtiwari909 profile image
Shubham Tiwari

Sure i will check it

Collapse
 
jolebowski profile image
jolebowski

You can use media queries

Collapse
 
shubhamtiwari909 profile image
Shubham Tiwari

I have used it in the example you check

Collapse
 
d7460n profile image
D7460N

Great job! Thanks for sharing.