如何对齐这些导航栏元素?

时间:2015-12-18 19:47:12

标签: html css

所以我希望我的导航栏看起来像:

http://postimg.org/image/nc2lt2x63/

但我的导航栏看起来像:

http://postimg.org/image/f8e1qa7w3/

这是我的Html& Css代码:

HTML:

<nav class="navigation-main">

        <div class="container">

        <div class="navigation-right">

        <ul> 

            <li><a href="">Menu</a></li>
            <li><a href="">Menu</a></li>
            <li><a href="">Menu</a></li>
            <div class="dropdown">
                 <button onclick="myFunction()" class="dropbtn">Test</button>
                <div id="myDropdown" class="dropdown-content">
                    <a href="#home">Home</a>
                    <a href="#about">About</a>
                    <a href="#contact">Contact</a>
                </div>
            </div>
        </ul>

        <div class="navigation-left">

            <ul>
            <li><a href=""><img id="navigation-image" src="img/Logos/full.png"></a></li>
            <li><a href="">Menu</a></li>
            <li><a href="">Menu</a></li>
            <li><a href="">Menu</a></li>
            <li><a href="">Menu</a></li>
            </ul>

        </div>
    </nav>

的CSS:

    .navigation-main{
    background-color: rgba(0,0,0,0.75);

}
.navigation-main li{
  display:inline;
  vertical-align:middle;
  line-height:85px;
}
.navigation-lefr, .navigation-right{
    position: relative;
}
.navigation-left ul{
    position: bottom;
}
#navigation-image{
    height: 45px;
    margin-top:-10px;
    display:inline;
    vertical-align:middle;
}
.social-media{
    float: right;
}

.dropbtn {
    background-color: #4CAF50;
    color: white;
    padding: 16px;
    font-size: 16px;
    border: none;
    cursor: pointer;
}

.dropbtn:hover, .dropbtn:focus {
    background-color: #3e8e41;
}

.dropdown {
    display: inline-block;
}

.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f9f9f9;
    min-width: 160px;
    overflow: auto;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}

.dropdown-content a {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
}

.dropdown a:hover {background-color: #f1f1f1}

.show {display:block;}

.navigation-right{
    float: right;
}
.navigation-left{
    float: left;
}
.navigation-main{
    background-color: #000;
    width: 100%;
    height: auto;
    overflow: hidden; // Add this
}
.navigation-main li{
    display: inline-block;
    padding: 10px;
}
.navigation-main a{
    color: #F2F2F2;
    text-decoration: none;

}

JS:

    /* When the user clicks on the button, 
toggle between hiding and showing the dropdown content */
function myFunction() {
    document.getElementById("myDropdown").classList.toggle("show");
}

// Close the dropdown if the user clicks outside of it
window.onclick = function(event) {
  if (!event.target.matches('.dropbtn')) {

    var dropdowns = document.getElementsByClassName("dropdown-content");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
}

我如何解决问题?

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:1)

<nav class="navigation-main">

        <div class="container">
 <div class="navigation-left">

            <ul>
            <li><a href=""><img id="navigation-image" src="img/Logos/full.png"></a></li>
            <li><a href="">Menu</a></li>
            <li><a href="">Menu</a></li>
            <li><a href="">Menu</a></li>
            <li><a href="">Menu</a></li>
            </ul>

        </div>
        <div class="navigation-right">

        <ul> 

            <li><a href="">Menu</a></li>
            <li><a href="">Menu</a></li>
            <li><a href="">Menu</a></li>
            <div class="dropdown">
                 <button onclick="myFunction()" class="dropbtn">Test</button>
                <div id="myDropdown" class="dropdown-content">
                    <a href="#home">Home</a>
                    <a href="#about">About</a>
                    <a href="#contact">Contact</a>
                </div>
            </div>
        </ul>

    </nav>

只需将导航左侧置于导航右侧

即可