中心徽标div与用于导航的浮动



我在尝试使徽标与导航链接正确对齐时遇到困难。我想到的解决方案是为徽标div添加填充和边距,但这仍然不起作用。 我一直在试图找到解决这个问题的方法,但无济于事。有关如何解决此问题的任何帮助将不胜感激。非常感谢!

页面预览: 链接

法典:

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;
 }
 
.center {
    width:10%;
 text-align: center;
  margin-left: auto;
    margin-right: auto;
 
  }
.logo {
      margin-left: auto;
    margin-right: auto;
}
.topnav a {
    color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}
.active {
  background-color: #4CAF50;
  color: white;
}
.right {
 float:right;
 }
.left {
 float:left; 
}
.topnav .icon {
  display: none;
}
.dropdown {
     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;
  }
}
<div class="topnav" id="myTopnav">
  <a href="#home" class="active right">Home</a>
  <a href="#news" class="right">News</a>
  <a href="#contact" class="right">Contact</a>

  <div class="dropdown right">
    <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> 
<div class="center">
<div class="logo">
<a href="">
My Logo here</a>
</div>
</div>
  <a href="#about" class="left">About</a>
    <a href="#about" class="left">About</a>
   
  <div class="dropdown left">
    <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="javascript:void(0);" style="font-size:15px;" class="icon" onclick="myFunction()">&#9776;</a>
</div> 
 

您需要包装左部分、中心部分和右部分,而不是使用 flex,您可以获得所需的结果。

你的 HTML 会是这样的

<div class="topnav" id="myTopnav">
  <!-- Left Portion -->
  <div>
    <a href="#home" class="active right">Home</a>
  <a href="#news" class="right">News</a>
  <a href="#contact" class="right">Contact</a>
  <div class="dropdown right">
    <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> 
  </div>
<!-- Center Portion -->
<div class="center">
  <div class="logo">
    <a href="">My Logo here</a>
  </div>
</div>

<!-- Right Portion -->
<div>
  <a href="#about" class="left">About</a>
  <a href="#about" class="left">About</a>
  <div class="dropdown left">
    <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> 
</div>

  <a href="javascript:void(0);" style="font-size:15px;" class="icon" onclick="myFunction()">&#9776;</a>
</div> 

修改 .topnav 类的 css

.topnav {
   overflow: hidden;
  background-color: #333;
  display: flex;
  justify-content: space-between;
  align-items: center;
 }

这是工作小提琴

相关内容

  • 没有找到相关文章

最新更新