HTML 下拉框

标签 html css

我的下拉框有问题。我以某种方式设法创建了一个导航栏,然后向其中添加了键。我在“关于”窗口上做了一个下拉框。它似乎有效。但我有与下拉框相关的问题。我在创建第二个下拉框时遇到问题。它有时会改变导航栏。 放错位置和其他类型的错误。你能给我看一个“主页”按钮上的下拉框的例子吗?这是我的代码。请帮助我。

/* Building navigation bar */

ul {
  text-align: center;
  list-style-type: none;
  margin: 0;
  padding-bottom: 5px;
  background-color: black;
  display: block;
}
li a {
  text-decoration: none;
  padding: 14px 20px;
  color: white;
}
li a:hover {
  background-color: red;
}
li {
  display: inline;
}
/* Building about dropdown and the dropdown content */

.container {
  width: 15em;
}
.dropdown {
  position: relative;
  display: inline-block;
}
.dropdown-content {
  position: absolute;
  display: none;
  background-color: black;
  color: white;
}
.dropdown:hover .dropdown-content {
  display: block;
}
/*added lines*/

.dropdown-content a {
  text-decoration: none;
  color: white;
  text-align: center;
}
.dropdown-content a:hover {
  background-color: red;
  /*font-color will be red*/
}
/*added lines ends here*/

.paragraphs {
  text-align: center;
}
<!DOCTYPE html>
<html>

<head>
</head>

<body>
  <ul>
    <li><a href="#file:///C:/Users/Nabil/Desktop/web/YouBox/main.html#style.html">Home</a>
    </li>
    <li><a href="news.html">News</a>
    </li>
    <div class="dropdown">
      <li><a href="#about.html">About</a>
      </li>
      <div class="dropdown-content">
        <ul>
          <a href="#Server.html">Server</a>
          <a href="#hahahaha.html">Origins</a>
        </ul>
      </div>
    </div>
  </ul>
  <hr>
  <p class="paragraphs"><strong>mymash.com<br> 
    Top vs games - pictures in Bahrain!  <br>
     Who looks mind blowing? Lets see, Vote your favourite photo!</strong>
    <br>or you can add new pictures of yourself to challenge someone <a href="#test">HERE!</a>
  </p>
  <hr>

</body>

</html>

最佳答案

在这里你看到 '.dropdown-content css 隐藏了 <a> 的显示标签

.dropdown-content {  
          position: absolute;  
          display: none;       <-----------------  hide  contents  
          background-color: black;
          color: white;           
}  

然后悬停

.dropdown:hover .dropdown-content { 
          display: block;  <----  make visible the contents  
}  
li a:hover { 
             background-color: red; <---- change the background color to red
}

试试这段代码

<!DOCTYPE html>
    <html>
    <head>
    <style>
    /* Building navigation bar */ 

    ul {
        text-align: center;
        list-style-type: none;
        margin: 0;
        padding-bottom: 5px;
    	 background-color: black;
    	 display: block;
    }

    li a { 
             text-decoration: none;
    		 padding: 14px 20px;
    		 color: white;
    		
    }

    li a:hover { 
                 background-color: red;
    }

    li { 
             display: inline;
    }

    /* Building about dropdown and the dropdown content */ 

    .container { 
               width: 15em;
    }

    .dropdown { 
            position: relative;
    		 display: inline-block;
    }

    .dropdown-content { 
              position: absolute;
    		  display: none;
    		  background-color: black;
    		  color: white;
    	      
    } 

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

     /*added lines*/
    .dropdown-content a{
      text-decoration:none;
      color: white;
      text-align: center;
        position: relative;
       display: inline-block;
     
    }
    .dropdown-content a:hover{
      background-color:red;/*font-color will be red*/
    }
    /*added lines ends here*/

    .paragraphs { 
        text-align:center;
    }

    </style>
    </head>

    <body>
      <ul>
       <div class="dropdown">
        <li><a href="#file:///C:/Users/Nabil/Desktop/web/YouBox/main.html#style.html">Home</a></li>
      <div class="dropdown-content"><ul>
    	      <a href="#page1.html">item1</a>
    		  <a href="#page2.html">item2</a>
    		  </ul>
    	     </div>
        </div>
        <li><a href="news.html">News</a></li>
    	<div class="dropdown">
        <li><a href="#about.html">About</a></li>
    	 <div class="dropdown-content"><ul>
    	      <a href="#Server.html">Server</a>
    		  <a href="#hahahaha.html">Origins</a>
    		  </ul>
    	     </div>
    	</div>
      </ul>
      <hr>
      <p class="paragraphs"><strong>mymash.com<br> 
        Top vs games - pictures in Bahrain!  <br>
         Who looks mind blowing? Lets see, Vote your favourite photo!</strong><br>
         or you can add new pictures of yourself to challenge someone <a href="#test">HERE!</a></p>
    	 <hr>
       
    </body>

    </html>

关于HTML 下拉框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39992573/

相关文章:

javascript - 在 JavaScript 中存储和返回 HTML 表单的输入变量

html - 如何在div的底部制作一个三 Angular 形

c# - 如何在 MVC 5 中的 Bootstrap 3 导航中将链接左右对齐

jquery - htmlpurifier 删除以 rgb 定义但不以十六进制定义的边框颜色

jquery - 溢出-x : hidden is breaking jquery scroll event

javascript - webkit 过渡在 mozilla 和 opera 中结束了吗?

c# - 如何停止在 visual studio 2015 中打开的每个文件上弹出编码

javascript - 覆盖按钮的 Jquery Mobile Css

html - 下拉菜单问题

javascript - 如何使用 jQuery 比较 tr 中的几个 td.text()