You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

下拉菜单(Drop Down Menu)失效排查及所需补充代码咨询

Fixing Your Non-Working Dropdown Menu

Hey there, let's get that dropdown up and running! The two critical pieces you're missing are:

  1. The nested submenu HTML structure for your exams, java, c, c++ options
  2. CSS rules to toggle the dropdown's visibility when hovering over the parent menu item

Step 1: Update Your HTML Structure

First, complete your navigation with a parent list item that contains the dropdown submenu. Here's how your full nav section should look:

<header>
  <div class="nav">
    <ul class="menu">
      <li><a href="#">Home</a></li>
      <!-- Parent item for dropdown -->
      <li class="dropdown">
        <a href="#">Courses &#9662;</a> <!-- Arrow to indicate dropdown functionality -->
        <ul class="submenu">
          <li><a href="#">exams</a></li>
          <li><a href="#">java</a></li>
          <li><a href="#">c</a></li>
          <li><a href="#">c++</a></li>
        </ul>
      </li>
      <!-- Add other top-level menu items here -->
    </ul>
  </div>
</header>

Step 2: Add Required CSS to style.css

Next, add these styles to your external CSS file to enable the dropdown behavior:

/* Base horizontal menu styling */
.nav .menu {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  background-color: #333;
}

.nav .menu li {
  position: relative; /* Positions submenu relative to parent item */
  padding: 1rem 1.5rem;
}

.nav .menu a {
  color: #fff;
  text-decoration: none;
}

/* Submenu - hidden by default */
.nav .submenu {
  list-style: none;
  padding: 0;
  margin: 0;
  position: absolute;
  top: 100%; /* Places submenu directly below parent */
  left: 0;
  background-color: #444;
  display: none; /* Hide submenu initially */
  min-width: 160px;
}

.nav .submenu li {
  padding: 0.8rem 1.2rem;
  white-space: nowrap; /* Prevents text wrapping in submenu items */
}

/* Show submenu on parent hover */
.nav .dropdown:hover .submenu {
  display: block;
}

/* Optional hover effects for better UX */
.nav .menu li:hover {
  background-color: #555;
}

.nav .submenu li:hover {
  background-color: #666;
}

Key Details:

  • position: relative on the parent li ensures the submenu stays anchored to its parent item
  • display: none hides the submenu by default, and display: block triggers it to show when hovering over the parent
  • The flex property makes the top-level menu horizontal
  • The &#9662; arrow is a simple visual cue that the item has a dropdown

With these changes, hovering over the "Courses" menu item will reveal your exams, java, c, c++ options as expected!

内容的提问来源于stack exchange,提问作者prachi sharma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:49:21