下拉导航链接不起作用

时间:2016-07-17 22:33:47

标签: html css navigation

下拉导航链接无效,除非您右键单击并在新标签页中单击“打开”。

HTML

<ul>
    <li class="has-child"><a href="#">Content</a>
      <ul class="dropdown">
        <li><a href="c1.html">Content 1</a></li>
        <li><a href="c2.html">Content 2</a></li>
        <li><a href="c3.html">Content 3</a></li>
      </ul>
    </li>
    <li><a href="images.html">Images</a></li>
    <li><a href="index.html">Home</a></li>
  </ul>

CSS

.has-child {
  position: relative;
  left: 0;
}

.has-child:after {
  position: absolute;
  margin-top: -5px;
  top: 27px;
  right: 5px;
  content: "";
  width: 0px;
  height: 0px;
  border-style: solid;
  border-width: 8px 5px 0 5px;
  border-color: #F09F47 transparent transparent transparent;
}

@media only screen and (max-width: 45em) {
  .has-child:after {
    position: absolute;
    margin-top: -5px;
    top: 25px;
    right: 20px;
    content: "";
    width: 0px;
    height: 0px;
    border-style: solid;
    border-width: 10px 5px 0 5px;
    border-color: #307e5c transparent transparent transparent;
  }
}

.has-child .dropdown {
  position: absolute;
  left: -40px;
  z-index: 99;
  display: none;
  margin-top: 10px;
  width: 200px;
  background: #fffbf5;
  border-radius: 6px;
}

@media only screen and (max-width: 45em) {
  .has-child .dropdown {
    position: relative;
    left: 0;
    z-index: auto;
    margin-top: 0;
    display: block;
    width: 100%;
    border-radius: 0;
  }
}

.has-child .dropdown:after {
  position: absolute;
  top: -10px;
  left: 50%;
  margin-left: -10px;
  content: "";
  width: 0px;
  height: 0px;
  border-style: solid;
  border-width: 0 10px 10px 10px;
  border-color: transparent transparent white transparent;
}

@media only screen and (max-width: 45em) {
  .has-child .dropdown:after {
    border: 0;
  }
}

.has-child .dropdown li {
  float: left;
  width: 100%;
  font-size: 0.875em;
  font-weight: 600;
  color: #3EA377;
  border-bottom: 1px solid #EFEFEF;
}

@media only screen and (max-width: 45em) {
  .has-child .dropdown li {
    padding-left: 20px;
    background: #fffbf5;
    border-bottom-color: #ffebc8;
  }
}

.has-child .dropdown li a {
  display: block;
  padding: 10px;
  z-index: 200;
}

大多数CSS只是定位和设计,但可能会忽略一些阻止我点击链接的东西。

1 个答案:

答案 0 :(得分:0)

W3 Schools有一个很棒的CSS导航栏教程。模板列表包括一个响应式侧导栏,可更改为媒体设备的顶部导航,这是处理媒体大小问题的一种灵巧方式。

提供了一个下拉选项,可以将其用作独立模板,也可以添加到响应模板中以创建二级甚至三级列表。当@media查询与问题代码一起发布时,可能响应性是有价值的。

http://www.w3schools.com/css/css_navbar.asp

为了发布教程中未显示的示例,此处的导航栏是一个固定宽度的下拉侧导航栏,其中包含响应式页面内容,可在浏览器调整大小时缩小/扩展。它是通过调整W3代码构建的。可以根据需要调整/添加@media查询。

希望这有帮助!

CSS

body {margin: 0;}

ul.sidenav {
    list-style-type: none;
    margin: 0;
    padding: 0;
    background-color: #d2d3df;
    position: fixed;
    height: 100%;
    min-width: 150px; 
    overflow: visible; 
    font-family: 'Arial';
    font-size: 11pt;
    }

ul.sidenav li a {
    display: block;
    color: #000000;
    padding: 8px 0 8px 16px;
    text-decoration: none;
    }


ul.sidenav li a.active {
    background-color: #4f6780;
    color: white;
}

ul.sidenav li a:hover:not(.active) {
    background-color: #6c6d76;
    color: white;
}

li a, .dropbtn {
    display: block;
    color: white;
    text-align: left;
    padding: 4px 4px;
    text-decoration: none;
}


li a:hover, .dropdown:hover .dropbtn {
    background-color: #77798b;    

}

li.dropdown {
    display: block;
}

.dropdown-content {
    display: none;
    position: absolute;
    margin-left: 80px; 
    background-color: #f7f7f7;
    min-width: 140px; 
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);    
}

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

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

}

.dropdown-content a {
      border-bottom: 1px solid #000; /*Adds a line under each sublink*/
}



@media screen and (max-width: 400px){
    ul.sidenav li a {
    text-align: left;
    float: none;
}
}


div.content {
margin-left: 160px;
padding: 5px;
font-family: 'Arial';
}  /*Page content styling. Can edit or remove.*/

HTML

 <!-- Navigation -->    

<ul class="sidenav">
  <li><a class="active" href="#home">Top Link</a></li>

  <li class="dropdown">
    <a href="#" class="dropbtn">Content 1</a>
    <div class="dropdown-content">
      <a href="#">content link 1</a>
      <a href="#">content link 2</a>
      <a href="#">content link 3</a>
    </div>
  </li>

  <li class="dropdown">
    <a href="#" class="dropbtn">Content 2</a>
    <div class="dropdown-content">
      <a href="#">content link 4</a>
      <a href="#">content link 5</a>
      <a href="#">content link 6</a>
    </div>
  </li>

    <li><a href="images.html">Images</a></li>
    <li><a href="index.html">Home</a></li>
</ul>

     <!-- End Navigation -->