中心标志div与浮动导航

时间:2018-03-22 12:58:22

标签: javascript html css navigation

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

页面预览:  LINK

代码:



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> 
 
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您需要包裹左侧部分,中央部分和右侧部分,而不是使用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;
 }

这是工作fiddle