【问题标题】:HTML/CSS - Drop Down menu's not cascadingHTML/CSS - 下拉菜单不层叠
【发布时间】:2021-09-07 03:38:19
【问题描述】:

目前正在创建一个下拉菜单,在进行样式设置时,我似乎已经破坏了菜单的实际下拉功能,其中其余按钮不下拉,或者只有少数按钮不下拉,我正在绞尽脑汁尝试修复它并且没有任何进展。

如果可能的话,我希望按钮垂直下降,例如,如果您突出显示请求,您会得到:

  • 提交新请求
  • 打开请求
  • 已关闭的请求

任何有助于纠正的帮助将不胜感激。

抱歉,如果代码有点混乱,这是一项早期工作,还有一些整理工作要做!

<html><head><style>body {
  font: normal normal bold 16px trebuchet ms;
  border: 0;
  text-align: center;
}

.nv {
  background: #4d9fb1;
  background: -webkit-gradient(linear, left top, left bottom, from(#4d9fb1), to(#2f626d));
  background: -moz-linear-gradient(top, #4d9fb1, #2f626d);
  background: linear-gradient(to bottom, #4d9fb1, #2f626d);
  padding: 0;
  width: 100%;
  display: flex;
  justify-content: center;
  text-decoration: none;
  margin: 0px;
}

.nv a {
  font-size: 16px;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  display: flex;
}

.snv {
  text-align: center;
  float: left;
}

.snv .snvbtn {
  font-size: 16px;
  border: none;
  outline: none;
  color: white;
  padding: 14px 16px;
  background: inherit;
  font: inherit;
  margin: 0;
}

.nv a:hover,
.snv:hover .snvbtn {
  background: white;
  color: #3B7A8B;
  margin-left: auto;
  padding-left: auto;
  padding-right: auto;
}

.snv-content {
  display: none;
  position: absolute;
  background: #4d9fb1;
  background: -webkit-gradient(linear, left top, left bottom, from(#4d9fb1), to(#2f626d));
  background: -moz-linear-gradient(top, #4d9fb1, #2f626d);
  background: linear-gradient(to bottom, #4d9fb1, #2f626d);
  width: inherit;
  z-index: 1;
  color: white;
}

.snv-content a {
  display: inline-block;
  color: white;
  text-decoration: none;
}

.snv-content a:hover {
  background: white;
  color: #3B7A8B;
}

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

.nv li {
  border-right: 3px solid #ffffff;
  display: inline-block;
  float: left;
  padding: 0px;
  overflow: hidden;
}

.nv li:last-child {
  border-right: 0;
}

.nv li>a {
  color: white;
  font-family: Trebuchet MS;
  font-size: 16px;
  font-weight: bold;
  line-height: 19px;
}

</style></head>
<body>

  <div class="nv">
    <li><a href="#home">Home</a></li>
    <div class="snv">
      <li><button class="snvbtn">Requests
          </button></li>
      <div class="snv-content">
        <li><a href="#company">Submit New Request</a></li>
        <li><a href="#team">Open Requests</a></li>
        <li><a href="#careers">Closed Requests</a></li>
      </div>
    </div>
    <div class="snv">
      <li><button class="snvbtn">Proactive Maintenance</button></li>
      <div class="snv-content">
        <li><a href="#bring">Outstanding Requests</a></li>
        <li><a href="#deliver">Completed Requests</a></li>
        <li><a href="#package">Your Calendar</a></li>
        <li><a href="#express">Export Report</a></li>
      </div>
    </div>
    <div class="snv">
      <li><button class="snvbtn">Admin Tools</button></li>
      <div class="snv-content">
        <li><a href="#link1">VPN Activity</a></li>
        <li><a href="#link2">Backup Reporting</a></li>
        <li><a href="#link3">Asset List</a></li>
        <li><a href="#link4">Licence Management</a></li>
      </div>
    </div>
    <li><a href="#contact">Your Profile</a></li>
  </div>



</body>

</html>

</div>
<div class="push"></div>
</div>
</body>

【问题讨论】:

  • li 标签周围的 HTML 标记无效。 li 标记必须包含在 ulolmenu 的父元素中(参见 developer.mozilla.org/en-US/docs/Web/HTML/Element/li),修复此结构可能是好的第一步。然后,请参阅这篇 CSS 技巧文章,该文章解释了悬停下拉菜单的 HTML/CSS 结构。

标签: html css drop-down-menu navigationbar


【解决方案1】:

在您的 HTML 代码中遵循此代码 sn-ps 样式。

  1. 导航菜单片段

     <ul id="navmenu">
         <li> <a href="#"> Hyperlink 1  </a></li>
         <li> <a href="#"> Hyperlink 2  </a>
             <ul class="sub1">
                 <li> <a href="#"> Hyperlink 2.1  </a></li>
                 <li> <a href="#"> Hyperlink 2.2  </a></li>
                 <li> <a href="#"> Hyperlink 2.3  </a></li>
             </ul>
         </li>
         <li> <a href="#"> Hyperlink 3  </a></li>
         <li> <a href="#"> Hyperlink 4  </a></li>
     </ul>
    

2) 导航菜单样式表 HTML sn-p

//导航菜单编写规则sn-p

          <style>             
                                    
               ul#navmenu, ul.sub1 {
                    list-style-type: none;
               }
               ul#navmenu li {
                    outline: 1px;
                    width: 125px;
                    text-align:center;
                    position: relative;
                    float: left;
                    margin-right: 4px;    
               }
               ul#navmenu a {
                    text-decoration:none;
                    display:block;
                    width:125px;
                    height:25px;
                    line-height: 25px;
                    border:1px;
                    border-radius:5px;
              }   
       </style>    

从上面的HTML代码sn-ps,根据你的要求替换超链接,再次运行。确保为每个标签提供正确的类名,并在 CSS 文件中保持相同,否则将无法正常工作。

【讨论】:

    【解决方案2】:

    所有位置的绝对值

    试试下面的

    body {
      font: normal normal bold 16px trebuchet ms;
      border: 0;
      text-align: center;
    }
    
    .nv {
      background: #4d9fb1;
      background: -webkit-gradient(linear, left top, left bottom, from(#4d9fb1), to(#2f626d));
      background: -moz-linear-gradient(top, #4d9fb1, #2f626d);
      background: linear-gradient(to bottom, #4d9fb1, #2f626d);
      padding: 0;
      width: 100%;
      display: flex;
      justify-content: center;
      text-decoration: none;
      margin: 0px;
    }
    
    .nv a {
      font-size: 16px;
      color: white;
      text-align: center;
      padding: 14px 16px;
      text-decoration: none;
      display: flex;
    }
    
    .snv {
      text-align: center;
      float: left;
    }
    
    .snv .snvbtn {
      font-size: 16px;
      border: none;
      outline: none;
      color: white;
      padding: 14px 16px;
      background: inherit;
      font: inherit;
      margin: 0;
    }
    
    .nv a:hover,
    .snv:hover .snvbtn {
      background: white;
      color: #3B7A8B;
      margin-left: auto;
      padding-left: auto;
      padding-right: auto;
    }
    
    .snv-content {
      display: none;
      position: relative;
      background: !important;
      background: -webkit-gradient(linear, left top, left bottom, from(#4d9fb1), to(#2f626d));
      background: -moz-linear-gradient(top, #4d9fb1, #2f626d);
      background: linear-gradient(to bottom, #4d9fb1, #2f626d);
      width: inherit;
      z-index: 9;
      color: white;
    }
    
    .snv-content a {
      display: inline-block;
      color: white;
      text-decoration: none;
    }
    
    .snv-content a:hover {
      background: white;
      color: #3B7A8B;
    }
    .snv-content li {
        width: 100%;
    }
    .snv:hover .snv-content {
         display: block;
        position: absolute;
        float: left;
        top: 50px;
        left: 49%;
        background: grey;
        z-index: 99999;
       width:30%;
    }
    
    .nv li {
      border-right: 3px solid #ffffff;
      display: inline-block;
      float: left;
      /* width: 100%; */
      /* transition: initial; */
    }
    
    .nv li:last-child {
      border-right: 0;
    }
    
    .nv li>a {
      color: white;
      font-family: Trebuchet MS;
      font-size: 16px;
      font-weight: bold;
      line-height: 19px;
    }
     <div class="nv">
        <li><a href="#home">Home</a></li>
        <div class="snv">
          <li><button class="snvbtn">Requests
              </button></li>
          <div class="snv-content">
            <li><a href="#company">Submit New Request</a></li>
            <li><a href="#team">Open Requests</a></li>
            <li><a href="#careers">Closed Requests</a></li>
          </div>
        </div>
    
        
      </div>

    【讨论】:

      猜你喜欢
      • 2012-12-16
      • 1970-01-01
      • 2016-08-15
      • 1970-01-01
      • 2021-05-03
      • 2021-07-01
      • 2016-08-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多