javascript - 试图将我的导航栏和汉堡包图标居中

标签 javascript html css navigation

所以基本上我有一个向左浮动的导航栏。该页面是响应式的,因此当屏幕低于某个尺寸时,导航栏会消失(主页链接除外),并且会在右侧弹出一个汉堡包下拉菜单。我想做的是弄清楚如何使导航栏居中并在屏幕尺寸减小时让它完全消失,并且汉堡包图标出现并在屏幕上居中。

HTML:

<div class="topnav" id="myTopnav">  
<nav>

  <a href="home.html">Home</a>  
  <a href="history.html">History</a>  
  <a href="information.html">Information</a>  
  <a href="gallery.html">Gallery</a>
  <a href="videos.html">Videos</a>
  <a href="exercises.html">Exercises</a>
  <a href="contact.html">Contact</a> 
  <a href="links.html">Links</a>  
  <a href="javascript:void(0);" class="icon" onclick="myFunction()">&#9776;</a>

</nav>
</div>

CSS:

/* Add a black background color to the top navigation */
.topnav {
background-color: #000000;
overflow: hidden;
}

/* Style the links inside the navigation bar */
.topnav a {
float: left;
display: block;
color: #FFFFFF;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 2.25em;
margin-bottom: 1.25%; 
}

/* Change the color of links on hover */
.topnav a:hover {
background-color: #2C3539;
color: #FFFFFF;
}

/* Add an active class to highlight the current page */
.active {
background-color: #4CAF50;
color: white;
}

/* Hide the link that should open and close the topnav on small screens */
.topnav .icon {
display: none;
}

/* When the screen is less than 600 pixels wide, hide all links, except for 
the first one ("Home"). Show the link that contains should open and close 
the topnav (.icon) */
@media screen and (max-width: 600px) {
.topnav a:not(:first-child) {display: none;}
.topnav a.icon {
float: right;
display: block;
}
}

/* The "responsive" class is added to the topnav with JavaScript when the 
user clicks on the icon. This class makes the topnav look good on small 
screens (display the links vertically instead of horizontally) */
@media screen and (max-width: 600px) {
.topnav.responsive {position: relative;}
.topnav.responsive a.icon {
position: absolute;
right: 0;
top: 0;
}
.topnav.responsive a {
float: none;
display: block;
text-align: left;
}
}

Javascript:

<script>
/* Toggle between adding and removing the "responsive" class to topnav when 
the user clicks on the icon */
function myFunction() {
var x = document.getElementById("myTopnav");
if (x.className === "topnav") {
    x.className += " responsive";
} else {
    x.className = "topnav";
}
}
</script>

如有任何帮助,我们将不胜感激!

最佳答案

Removing floats and display: block, and using inline-block may fix your problems

display: inline-block;

您的新代码:

/* Add a black background color to the top navigation */
.topnav {
background-color: #000000;
overflow: hidden;
}

/* Style the links inside the navigation bar */
.topnav a {
display: inline-block;
color: #FFFFFF;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 2.25em;
margin-bottom: 1.25%; 
}

/* Change the color of links on hover */
.topnav a:hover {
background-color: #2C3539;
color: #FFFFFF;
}

:D

关于javascript - 试图将我的导航栏和汉堡包图标居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48819580/

相关文章:

jquery - CSS 混合模式将两个背景图像相乘替换为 jQuery

css - 移除 Material Stepper header

javascript - HTML+JavaScript GUI 建议

java - native 应用程序将字符串传递到网站的简单示例?

javascript - 我的 process.php 有什么问题导致我的表单无法发送?

html - 图标悬停减少过渡

html - 我可以在 PrimeNG 下拉列表的突出显示项中包含一个图标吗?

javascript - 如何通过 Javascript 检索跨域 RSS(xml)

javascript - 制作了一个适用于 JSFiddle 但不适用于 Github Pages 的 eclipse 刻草图

html - 在移动 View (CSS) 中将内容和图像居中