如何使下拉菜单水平

时间:2017-05-04 05:30:06

标签: javascript html css drop-down-menu

我不确定这是否可行但是,我有一个名为fruits的按钮。当我点击它时,4个水果按钮垂直向下,但我希望它们水平显示?

关于如何做到这一点的任何想法或提示?

请不要说“自己尝试一下,不是来这里为你做这件事”..我一直都是,而且我一无所知。我正在寻找的只是一个暗示,不要指望任何人为我编码。谢谢:))

HTML

<div id="myNav" class="overlay">
        <!-- Button to close the overlay navigation -->
        <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
        <!-- Overlay content -->
        <div class="overlay-content"> 
            <button class="dropbtn btn-circle overlay-icon" onclick='myFunction()'>Fruits</button>
            <div id="myDropdown" class="dropdown-content">
                <div class="row">
                    <div class="col-xs-12">
                        <button class="dropbtn btn-circle overlay-icon">Apple</button>
                        <button class="dropbtn btn-circle overlay-icon">Banana</button>
                        <button class="dropbtn btn-circle overlay-icon">Orange</button>
                        <button class="dropbtn btn-circle overlay-icon">Pear</button>
                    </div>
                </div>
            </div>

        </div>
    </div>

JS

 <script>
    /* 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');
          }
        }
      }
    }
    </script>

CSS

.overlay-icon { 
  float: center;
  width: 120px;
  height: 120px;
  text-align: center;
  padding: 600px 0;
  font-size: 12px;
  line-height: 1.428571429;
  border-radius: 36px;
  margin-left: 660px;
  margin-bottom: 20px;
}

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

.dropdown-content {
    display: none;
    position: relative;
    min-width: 10px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    z-index: 1;
    font-size: 10px;  
    margin-left: 0px;
}

.dropdown-content dropbtn {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
    font-size: 10px;
}

2 个答案:

答案 0 :(得分:0)

我认为您需要将按钮显示为内联块。 这是css代码:

.dropdown-content dropbtn {
  display: inline-block;
}

答案 1 :(得分:0)

更改了您的css,我已移除margin-left并将padding60px 0更改为600px 0

CSS:

.overlay-icon { 
  float: center;
  width: 120px;
  height: 120px;
  text-align: center;
  padding: 60px 0;
  font-size: 12px;
  line-height: 1.428571429;
  border-radius: 36px;
  margin-bottom: 20px;
}

.show {
   display: initial;
}