#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
CSS3 Fundamentals

CSS Dropdowns

A CSS dropdown is a menu or panel that stays hidden until the user hovers over or focuses on a trigger, such as a navigation item. It is built with nothing more than positioning and a selector that reveals the hidden part. You use it for navigation sub-menus, account menus and small "more options" lists when you do not need JavaScript.

The structure: a wrapper, a trigger and a hidden panel

Every CSS dropdown has three pieces. A wrapper element with position: relative becomes the anchor. Inside it sits the trigger, usually a button or link. The panel is positioned absolute just below the trigger and starts hidden. When the wrapper is hovered or contains focus, the panel is shown.

<nav>
  <div class="dropdown">
    <button class="dropdown-toggle" aria-haspopup="true">Courses</button>
    <ul class="dropdown-menu">
      <li><a href="/html">HTML</a></li>
      <li><a href="/css">CSS</a></li>
      <li><a href="/js">JavaScript</a></li>
    </ul>
  </div>
</nav>
.dropdown {
  position: relative;
  display: inline-block;
}

.dropdown-menu {
  position: absolute;
  top: 100%;          /* directly under the trigger, no gap */
  left: 0;
  min-width: 180px;
  margin: 0;
  padding: 8px 0;
  list-style: none;
  background: #fff;
  border: 1px solid #ddd;
  border-radius: 6px;
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.12);
  z-index: 10;
  display: none;
}

.dropdown:hover .dropdown-menu,
.dropdown:focus-within .dropdown-menu {
  display: block;
}

.dropdown-menu a {
  display: block;
  padding: 8px 16px;
  color: #222;
  text-decoration: none;
}

.dropdown-menu a:hover,
.dropdown-menu a:focus { background: #f2f4f7; }

Why the wrapper gets the hover, not the button

If you wrote .dropdown-toggle:hover + .dropdown-menu, the menu would disappear the moment the pointer left the button to move onto a link. Putting :hover on the wrapper keeps the menu open while the pointer is anywhere inside the whole group, trigger and panel together.

:focus-within matches when any element inside the wrapper has keyboard focus. That means users who press Tab reach the button, the menu opens, and they can tab through each link. Hover alone would leave keyboard users unable to reach the sub-menu.

Fading instead of popping

display cannot be animated smoothly in most browsers. For a gentle fade, keep the panel in the layout and toggle opacity and visibility instead, adding transition: opacity 0.15s. visibility: hidden also stops hidden links from receiving clicks.

You can also slide the panel slightly with transform: translateY(-4px) in the hidden state and translateY(0) when open. Transforms are cheap for the browser to animate and do not shift surrounding content, so the rest of the navigation bar stays still while the menu appears.

For multi-level menus, the same pattern nests. A second-level wrapper inside the first panel gets position: relative, and its own panel uses top: 0; left: 100% so it opens to the side instead of below.

Bugs you will probably hit

  • The menu closes as you move toward it. A margin-top on the panel creates a gap where the pointer is outside the wrapper. Use top: 100% with no margin, or add padding inside the panel instead.
  • The menu is cut off. If a parent has overflow: hidden, the absolutely positioned panel gets clipped. Remove the overflow rule or move the dropdown outside that parent.
  • The menu appears behind other content. Give it a z-index, and remember it only works on positioned elements.
  • Touch screens have no hover. A tap usually triggers focus, so :focus-within helps, but complex menus often need a small script to toggle a class.

Takeaway

Anchor the panel with a relatively positioned wrapper, place it at top: 100%, and reveal it with both :hover and :focus-within on the wrapper. That combination works for mouse and keyboard users without a line of JavaScript.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course