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

导航栏搜索框与下拉菜单位置偏移 CSS/HTML问题排查求助

导航栏偏移问题修复方案

错误定位

  • HTML结构错误
    1. <ul> 直接子元素只能是 <li>,现有代码直接在 <ul class="navbar"> 内嵌套 <div>、放置 <input> 属于非法结构,是布局错乱的核心原因
    2. 存在多余未闭合的 <div class="navbaritems" class="dropdown"> 标签,同时单个元素重复声明 class 属性,语法不生效
    3. 下拉菜单的 <li class="dropdown"> 未添加 navbaritems 类,无法继承左浮动样式
    4. 搜索框直接作为 <ul> 的子元素,没有被 <li> 包裹,不符合列表结构规范
  • CSS样式错误
    1. li.dropdown 仅设置了 display: inline-block,未添加 float: left 属性,无法和其他导航项保持同排
    2. 搜索框的类名是 searchbar,现有响应式样式中针对 .search-container 的规则完全没有生效场景
    3. 链接全局样式会覆盖下拉菜单内的链接颜色,导致下拉项文字不可见

修正后代码

HTML部分

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>News</title>
    <link rel="stylesheet" href="./css/index.css">
</head>
<body>
    <header>
        <h1>News</h1>
        <nav>
            <ul class="navbar">
                <li class="navbaritems"><a href="#home">Home</a></li>
                <li class="navbaritems"><a href="#Page2">Page 2</a></li>
                <li class="navbaritems"><a href="#Page3">Page 3</a></li>
                <li class="navbaritems"><a href="#Page4">Page 4</a></li>
                <li class="navbaritems dropdown">
                    <a href="javascript:void(0)" class="dropbtn">Dropdown</a>
                    <div class="dropdown-content">
                        <a href="#DropdownPage1">Dropdown Page 1</a>
                        <a href="#DropdownPage2">Dropdown Page 2</a>
                        <a href="#DropdownPage3">Dropdown Page 3</a>
                    </div>
                </li>
                <li class="navbaritems"><a href="#Contact">Contact</a></li>
                <li class="navbaritems search-container">
                    <input type="text" class="searchbar" placeholder="Search.." name="searchbar">
                </li>
            </ul>
        </nav>
    </header>
    <main>
        <!--Article Feed Goes Here-->
        <!--Need to add Advertising mechanism to this section of the website-->
    </main>
    <footer>
        <!--Put "Terms of Service" and "Privacy Policy" here-->
    </footer>
</body>
</html>

CSS部分

/*nav bar*/
.navbar .searchbar {
    float: right;
    padding: 6px;
    border: none;
    font-size: 17px;
}
  
.navbar .search-container button:hover {
    background: #ccc;
}

@media screen and (max-width: 600px) {
    .navbar .search-container {
      float: none;
    }
    .navbar a, .navbar .searchbar, .navbar .search-container button {
      float: none;
      display: block;
      text-align: left;
      width: 100%;
      margin: 0;
      padding: 14px;
    }
    .navbar .searchbar {
      border: 1px solid #ccc;  
    }
}

.navbar {
    list-style:none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    background-color: #333;
}

.navbaritems {
    float: left;
    display: inline;
    color: white;
    text-align: center;
    padding: 8px 16px;
    text-decoration: none;
}

h1 {
    text-align: center;
}

/*导航栏链接样式*/
.navbaritems a:link {
  color: white;
  background-color: transparent;
  text-decoration: none;
}

.navbaritems a:visited {
  color: white;
  background-color: transparent;
  text-decoration: none;
}

.navbaritems a:hover {
  color: whitesmoke;
  background-color: transparent;
  text-decoration: none;
}

.navbaritems a:active {
  color: white;
  background-color: transparent;
  text-decoration: none;
}

/* 下拉菜单样式 */
li.dropdown {
  display: inline-block;
  float: left;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
}

.dropdown-content a {
  color: black !important;
  padding: 12px 20px;
  text-decoration: none;
  display: block;
  text-align: left;
}

.dropdown-content a:hover {background-color: #f1f1f1;}

.dropdown:hover .dropdown-content {
  display: block;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 11:18:01