如何使具有下拉菜单的导航栏居中



我已经创建了一个带有下拉菜单的简单顶部导航栏,我正在尝试将所有链接(包括下拉菜单(居中获取。

我一直在寻找并尝试了不同的方法来将其全部居中,以便在手机、平板电脑或小分辨率屏幕上使用时自动调整,但我只设法将下拉菜单或其他链接居中,但不是所有内容......我该怎么做?

这是我正在使用的代码



body 
.navbar {
  overflow: hidden;
  background-color: #221a57;
}
.navbar a {
  float: left;
  font-size: 16px;
  color: white;
  text-align: center;
  min-width: 160px;
  padding: 14px 16px;
  text-decoration: none;
}
.dropdown {
  float: left;
  overflow: hidden;
}
.dropdown .dropbtn {
  font-size: 16px;  
  border: none;
  outline: none;
  color: white;
  min-width: 192px;
  padding: 14px 16px;
  background-color: inherit;
  font-family: inherit;
  margin: 0;
}
.navbar a:hover, .dropdown:hover .dropbtn {
  background-color: #F73D4B;
}
.dropdown-content {
  display: none;
  position: absolute;
  background-color: #DDDDDD;
  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;
}
.dropdown-content a:hover {
  background-color: #ddd;
}
.dropdown:hover .dropdown-content {
  display: block;
}
<!DOCTYPE html>
<html>
<head>
<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">
</head>
<body>
<div class="navbar">
  <a href="/home">Home</a>
  <a href="/about">About</a>
  <a href="/articles">Articles</a>
  <div class="dropdown">
    <button class="dropbtn">Freebies 
      <i class="fa fa-caret-down"></i>
    </button>
    <div class="dropdown-content">
      <a href="/eguide">eGuide</a>
      <a href="/wallpapers">Wallpapers</a>
    </div>
  </div> 
  <a href="/contact">Contact</a>
</div>
</body>
</html>
```
你可以

使用display:flex; justify-content:center;然后你可以根据需要修复宽度,你不需要溢出

<!DOCTYPE html>
    <html>
    <head>
    <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">
    <style>
     
    .navbar {
     
      background-color: #221a57;
      display:flex;
      justify-content:center;
    }
     
    .navbar a {
      font-size: 16px;
      color: white;
      text-align: center;
      
      padding: 14px 16px;
      text-decoration: none;
    }
    .dropdown .dropbtn {
      font-size: 16px;  
      border: none;
      outline: none;
      color: white;
      padding: 14px 16px;
      background-color: inherit;
      font-family: inherit;
      margin: 0;
    }
    .navbar a:hover, .dropdown:hover .dropbtn {
      background-color: #F73D4B;
    }
    .dropdown-content {
      display: none;
      position: absolute;
      background-color: #DDDDDD;
     
      box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
      z-index: 1;
    }
    .dropdown-content a {
      color: black;
      padding: 12px 16px;
      text-decoration: none;
      display: block;
      text-align: left;
    }
    .dropdown-content a:hover {
      background-color: #ddd;
    }
    .dropdown:hover .dropdown-content {
      display: block;
    }
    </style>
    </head>
    <body>
    <div class="navbar">
      <a href="/home">Home</a>
      <a href="/about">About</a>
      <a href="/articles">Articles</a>
      <div class="dropdown">
        <button class="dropbtn">Freebies 
          <i class="fa fa-caret-down"></i>
        </button>
        <div class="dropdown-content">
          <a href="/eguide">eGuide</a>
          <a href="/wallpapers">Wallpapers</a>
        </div>
      </div> 
      <a href="/contact">Contact</a>
    </div>
    </body>
    </html>

相关内容

  • 没有找到相关文章

最新更新