DEV Community

Cover image for Responsive Navbar without Javascript
Shubham Tiwari
Shubham Tiwari

Posted on

Responsive Navbar without Javascript

Greetings to all. Today I'll demonstrate how to make a responsive navbar without Javascript that has a dropdown effect for mobile devices. The major CSS section will be explained, and the remaining code will be provided in the code pen at the end of this blog.

Let's get started...

I'll use the :has() selector for the toggle effect; you can see which browsers support it here.
https://caniuse.com/?search=has

HTML -

<header>
  <div class="logo">
    <h1 class="logo-text">LOGO</h1>
    <button class="hamburger-icon">
      <label for="dropdown">
        <i class="fa-solid fa-bars"></i>
        <i class="fa-solid fa-x"></i>
      </label>
      <input type="checkbox" id="dropdown" />
    </button>
  </div>
  <ul class="navigation">
    <li><a href="#">Home</a></li>
    <li><a href="#">About</a></li>
    <li><a href="#">Contact</a></li>
  </ul>
</header>
Enter fullscreen mode Exit fullscreen mode
  • I'm putting a checkbox field inside the button to create a toggling effect. The bars and cross icons will likewise be toggled by this checkbox.

CSS -

/* hiding the hamburger icon for the desktop view */
.hamburger-icon{
display:none;
border:none;
background-color:transparent;
}

/* Hiding the checkbox default styling */
.hamburger-icon input[type="checkbox"] {
  appearance: none;
}
.
.
.
/* targeting the element for the 
viewport less than 600px*/
@media screen and (max-width: 600px) {
/* Hiding the nav links with height and overflow */
 .navigation {
    height:0;
    overflow:hidden;
    flex-direction: column;
    align-items: center;
    transition:all 0.5s ease-in-out;
  }

/* It will put the Logo text at left end 
and Hamburger at the right end of the header */
.logo {
    display: flex;
    justify-content: space-between;
  }
}

/* Making the hamburger button visible */
.hamburger-icon {
    display: block;
  }

/* Initially hiding the cross icon */
.fa-x{
    display:none;
  }

/*determining whether the header contains a checkbox input that is checked before expanding the navigation's height to 100 pixels to make it visible.*/
  header:has(.hamburger-icon input[type="checkbox"]:checked) .navigation {
    margin-top: 1rem;
    height:100px;
  }

/* Hiding the bars icon on checkbox checked state */
  header:has(.hamburger-icon input[type="checkbox"]:checked) .fa-bars {
    display:none;
  }

/* Showing the cross icon on checkbox checked state */
  header:has(.hamburger-icon input[type="checkbox"]:checked) .fa-x {
    display:inline-block;
  }
Enter fullscreen mode Exit fullscreen mode

Codepen -

THANK YOU FOR CHECKING THIS POST
You can contact me on -
Instagram - https://www.instagram.com/supremacism__shubh/
LinkedIn - https://www.linkedin.com/in/shubham-tiwari-b7544b193/
Email - shubhmtiwri00@gmail.com

^^You can help me by some donation at the link below Thank you👇👇 ^^
☕ --> https://www.buymeacoffee.com/waaduheck <--

Also check these posts as well
https://dev.to/shubhamtiwari909/css-iswherehas-and-not-2afd

https://dev.to/shubhamtiwari909/theme-changer-with-html-and-css-only-4144

https://dev.to/shubhamtiwari909/swiperjs-3802

https://dev.to/shubhamtiwari909/going-deep-in-array-sort-js-2n90

Latest comments (33)

Collapse
 
d7460n profile image
D7460N

Great job! Thanks for sharing.

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
 
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
 
shubhamtiwari909 profile image
Shubham Tiwari

Sure i will check 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
 
janosongithub profile image
JANOSongithub

Thank You! Nice work.

Collapse
 
shubhamtiwari909 profile image
Shubham Tiwari

Welcome

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
 
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
 
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