Skip to main content

A Dropdown Menu With Zero JavaScript: 2 Lines of CSS Explained

Updated 1 min readMarkdown
A Dropdown Menu With Zero JavaScript: 2 Lines of CSS Explained

SUBTITLE: Nested lists, the :hover selector and a transparent look over a photo.

INTRO: Dropdown menus look like they need JavaScript, but CSS can do the basic version on its own. Here is how a nested list and one hover rule build a multi-level navigation menu, plus its limits and how to fix them.

OUTLINE:

  1. The HTML: nesting lists inside list items
  2. Hiding the sub-menu with display: none
  3. Revealing it with ul li:hover ul li
  4. Building the horizontal bar with floats
  5. The transparent look: opacity vs rgba()
  6. Limits: keyboard and touch users
  7. Fixing it with :focus-within

KEY SNIPPET (for Hashnode): ul li ul li { display: none; }

ul li:hover ul li { display: block; }

Demo: https://muhammaduzairkhalid15.github.io/Transparent-Dropdown-Navigation-Menu-HTML-CSS-Uzi-Solutions/ Code: https://github.com/MuhammadUzairKhalid15/Transparent-Dropdown-Navigation-Menu-HTML-CSS-Uzi-Solutions

TAGS: CSS, Web Development, Frontend, HTML, Navigation

About the author

  • Documenting my journey from beginner to full-stack developer

No comments yet. Be the first to comment.