集中响应下拉导航栏项

时间:2019-08-10 22:52:15

标签: javascript html css

尝试将简单的响应式导航栏中的链接元素居中,并在https://www.w3schools.com/howto/howto_js_responsive_navbar_dropdown.asp的w3schools上列出下拉菜单

应用居中边距:0自动; div上的所有内容仍保留在左侧。其他CSS似乎都没有将其锁定在左侧,但我无法让它退缩。

还尝试将div转换为具有LI元素的UL,但一切都消失了

<div class="topnav" id="myTopnav">
<a href="#news" class="active">Fest 2020</a>
<a href="#news">Schedule</a>
<a href="#news">About</a>
<a href="#news">Tickets</a>
<a href="#contact">Contact</a>
<div class="dropdownmenu">
<button class="dropbtn">Past Events
  <i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
  <a href="#">2019</a>
  <a href="#">2018</a>
  <a href="#">2017</a>
 </div>
 </div>
 <a href="javascript:void(0);" class="icon" onclick="expandMenu()">&#9776;</a>
</div>

有什么建议吗?谢谢

2 个答案:

答案 0 :(得分:0)

如果我误解了,请原谅我,但我想你想让顶部的“按钮”居中吗?

如果是这样,则topnav的以下CSS将适用。

.topnav {
  overflow: hidden;
  background-color: #333;
  display: flex;
  flex-direction: row;
  justify-content: center;
}

答案 1 :(得分:0)

只需将display:flexjustify-content:center添加到topnav类。

display:flex-将元素显示为块级弹性容器,并且justify-content:center将弹性容器的项目在中心对齐。

function myFunction() {
  var x = document.getElementById("myTopnav");
  if (x.className === "topnav") {
    x.className += " responsive";
  } else {
    x.className = "topnav";
  }
}
body {margin:0;font-family:Arial}
.topnav {
  overflow: hidden;
  background-color: #333;
  
  display: flex;   
  justify-content: center; 

  }
.topnav a {
  float: left;
  display: block;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}
.active {
  background-color: #4CAF50;
  color: white;
}

.topnav .icon {
  display: none;
}

.dropdown {
  float: left;
  overflow: hidden;
}

.dropdown .dropbtn {
  font-size: 17px;    
  border: none;
  outline: none;
  color: white;
  padding: 14px 16px;
  background-color: inherit;
  font-family: inherit;
  margin: 0;
}

.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 {
  float: none;
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}

.topnav a:hover, .dropdown:hover .dropbtn {
  background-color: #555;
  color: white;
}

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

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

@media screen and (max-width: 600px) {
  .topnav a:not(:first-child), .dropdown .dropbtn {
    display: none;
  }
  .topnav a.icon {
    float: right;
    display: block;
  }
}

@media screen and (max-width: 600px) {
  .topnav.responsive {position: relative;}
  .topnav.responsive .icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  .topnav.responsive a {
    float: none;
    display: block;
    text-align: left;
  }
  .topnav.responsive .dropdown {float: none;}
  .topnav.responsive .dropdown-content {position: relative;}
  .topnav.responsive .dropdown .dropbtn {
    display: block;
    width: 100%;
    text-align: left;
  }
}
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<div class="topnav" id="myTopnav">
  <a href="#home" class="active">Home</a>
  <a href="#news">News</a>
  <a href="#contact">Contact</a>
  <div class="dropdown">
    <button class="dropbtn">Dropdown 
      <i class="fa fa-caret-down"></i>
    </button>
    <div class="dropdown-content">
      <a href="#">Link 1</a>
      <a href="#">Link 2</a>
      <a href="#">Link 3</a>
    </div>
  </div> 
  <a href="#about">About</a>
  <a href="javascript:void(0);" style="font-size:15px;" class="icon" onclick="myFunction()">&#9776;</a>
</div>