简体   繁体   English

中心响应式导航栏

[英]Center responsive navigation bar

I am following a guide from w3schools to build a responsive top navigation bar for my site: How TO - Responsive Top Navigation我正在按照 w3schools 的指南为我的网站构建响应式顶部导航栏: How TO - Responsive Top Navigation

However, I would like the navigation items to be centered on the page, not aligned to the left or right.但是,我希望导航项以页面为中心,而不是向左或向右对齐。 w3schools even has a second tutorial on a center navigation element link , but as soon as I try to use this code for several navigation elements, they either are all within each other or stack on top of each other! w3schools 甚至有关于中心导航元素 链接的第二个教程,但是一旦我尝试将此代码用于多个导航元素,它们要么全部在彼此内,要么堆叠在彼此的顶部!

Even more to my dismay, there has been a question about this exact problem before ( here ), but it seems the code of the example has been changed a lot in the meanwhile so that the answer is no longer applicable.更让我沮丧的是,之前( 这里)有一个关于这个确切问题的问题,但似乎同时示例的代码已经更改了很多,因此答案不再适用。 :( :(

To center the top navigation in the link you've provided, you would add the following to .topnav :要将您提供的链接中的顶部导航居中,您需要将以下内容添加到.topnav

.topnav {
  …
  display: flex;
  justify-content: center;
}

To address the mobile menu (and not center it), add the following to your @media query:要解决移动菜单(而不是将其居中),请将以下内容添加到您的 @media 查询中:

@media screen and (max-width: 600px) {
  …
  .topnav { display: block; }
}

Before

在此处输入图片说明

After

在此处输入图片说明

One way is to wrap the links inside a div (say, a div with class nav-links ), and then applying to the div:一种方法是将链接包装在一个 div 中(例如,一个带有类nav-links的 div),然后应用到 div:

.nav-links {
    width: fit-content; /* 'margin: auto' alone does not work if the div takes full width */
    margin: auto;
}

Below is a demo based on the tutorial you linked:以下是基于您链接的教程的演示:

 .nav-links { width: fit-content; margin:auto; } /*////////////// W3Schools CSS code //////////////*/ /* Add a black background color to the top navigation */ .topnav { background-color: #333; overflow: hidden; } /* Style the links inside the navigation bar */ .topnav a { float: left; display: block; color: #f2f2f2; text-align: center; padding: 14px 16px; text-decoration: none; font-size: 17px; } /* Change the color of links on hover */ .topnav a:hover { background-color: #ddd; color: black; } /* Add an active class to highlight the current page */ .topnav a.active { background-color: #4CAF50; color: white; } /* Hide the link that should open and close the topnav on small screens */ .topnav .icon { display: none; }
 <!-- Load an icon library to show a hamburger menu (bars) on small screens --> <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"> <div class="nav-links"> <a href="#home" class="active">Home</a> <a href="#news">News</a> <a href="#contact">Contact</a> <a href="#about">About</a> <a href="javascript:void(0);" class="icon" onclick="myFunction()"> <i class="fa fa-bars"></i> </a> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM