导航栏上的下拉菜单

时间:2017-05-15 18:01:50

标签: html css nav dropdown

我想在此导航菜单上执行一些下拉菜单,但它不起作用,我也希望将其置于中心位置。我试着用display:inline;命令,但它没有帮助。

https://jsfiddle.net/fLdasLv4/



private void button4_Click(object sender, EventArgs e)
{
    var fileA = textBox1.Text;
    var fileB = textBox2.Text;
    var fileC = "result.txt";

    // Read the alpha file contents into a list
    var alphaFileContents = File.ReadAllLines(fileA);

    // Read each line of the beta file, and select the first part
    // (up to the first comma) into a list
    var betaFilePrefixes = File.ReadAllLines(fileB).Select(line => line.Split(',')[0]);

    // Get all alpha lines that start with an item in the beta list
    var similarItems = alphaFileContents
        .Where(alpha => betaFilePrefixes.Any(alpha.StartsWith));

    // Write the lines to our result file
    File.WriteAllLines(fileC, similarItems);
}

ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    background-color: #333;
	position: absolute;
	left: 0%;
	top: 0%;
	width: 100%;
	height: 8%;
	
	
}

li {
    float: left;
}

li a {
    display: block;
    color: white;
    text-align: center;
    padding: 20px 25px;
	text-decoration: none;
	font-size: 100%;
	font-family:Lucida Sans Unicode;

	
}

li a:hover {
    background-color: #111;
}
li a:active{
    background-color: grey;
}

ul li:hover > ul
{
	display:block
}




2 个答案:

答案 0 :(得分:0)

可以这样做吗?

使用flexbox将其居中,然后隐藏子菜单首次使用

ul li ul {
  display: none;
}

悬停时,菜单显示子菜单使用:

/* Sub menu item */
ul li ul li {
  width: 100%;
  display: block;
}

/* Show Sub menu on hover */
ul li:hover > ul {
  position: absolute;
  display: block;
  background: green;
  width: 30%; /* Sub menu width */
}

ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: #333;
  width: 100%;
}

li {
  float: left;
}

li a {
  display: block;
  color: white;
  text-align: center;
  padding: 20px 25px;
  text-decoration: none;
  font-size: 100%;
  font-family: Lucida Sans Unicode;
}

li a:hover {
  background-color: #111;
}

li a:active {
  background-color: grey;
}

ul li ul {
  display: none;
}

/* Sub menu item */
ul li ul li {
  width: 100%;
  display: block;
}

/* Show Sub menu on hover */
ul li:hover > ul {
  position: absolute;
  display: block;
  background: green;
  width: 30%; /* Sub menu width */
}

.container {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
}
<ul class="container">
  <li><a class="active" href="#home" font size="16">Home</a></li>
  <li><a class="kaires" href="#news">Dropd</a>
    <ul>
      <li><a href="#">1</a></li>
      <li><a href="#">2</a></li>
      <li><a href="#">3</a></li>
      <li><a href="#">4</a></li>
    </ul>
  </li>
  <li><a href="#about">Something</a></li>
  <li><a href="#about">Contact us</a></li>
</ul>

答案 1 :(得分:0)

首先,子菜单的html标记未正确完成。如果您希望显示子菜单,也不能在主容器上放置溢出。您可以使用flebox将导航器居中,而无需更改标记或编写更多代码。见下面的示例..

&#13;
&#13;
nav {}
ul {
    display: flex;
    justify-content: center;
    list-style-type: none;
    margin: 0;
    padding: 0;
    /** overflow: hidden; Removed this(this wouldn't allow the submenu show) **/
    background-color: #333;
	  position: absolute;
	  /**left: 0%; **/
	  top: 0%;
	  width: 100%;
	  /** height: 8%;Removed This**/
}
ul ul {
    top: 100%;
    display: block;
    height: auto;
    visibility: hidden;
}
li {
   /** float: left; **/
    position: relative;
}
li a {
    display: block;
    color: white;
    text-align: center;
    padding: 20px 25px;
	  text-decoration: none;
	  font-size: 100%;
	  font-family:Lucida Sans Unicode;
}
li a:hover {
    background-color: #111;
}
li a:active{
    background-color: grey;
}
ul li:hover >  ul {
	display:block;
  visibility: visible;
}
li > ul li {
  float: none;
}
&#13;
<ul>
  <li><a class="active" href="#home" font size="16">Home</a></li>
  <li><a class="kaires" href="#news">Dropd</a>
      <ul>
        <li><a href="#">1</a></li>
        <li><a href="#">2</a></li>
        <li><a href="#">3</a></li>
        <li><a href="#">4</a></li>
	    </ul>
    </li>
  <li><a  href="#about">Something</a></li>
  <li><a  href="#about">Contact us</a></li>
</ul>
&#13;
&#13;
&#13;