【问题标题】:Dropdown menus are all aligned to left下拉菜单全部左对齐
【发布时间】:2013-05-29 21:43:45
【问题描述】:

我通过 CSS 创建了一个下拉菜单,但奇怪的是所有下拉菜单都向左对齐。我希望所有的下拉菜单都会出现并放在它们的父菜单下。

HTML如下:-

<div id="menu">
        <ul>
          <li ng-class="{selected: $index==currPage}" ng-repeat="page in data.pages" class="ng-scope selected">
                <a href="" ng-click="goToPage($index)" class="ng-binding">Introduction</a>
                <ul>
                    <!-- ngRepeat: smenu in data.subMenu[$index].list --><li ng-class="{$index==currMenu}" ng-repeat="smenu in data.subMenu[$index].list" class="ng-scope">
                        <a href="" ng-click="goToPage($index)" class="ng-binding">Profile</a>

                    </li><li ng-class="{$index==currMenu}" ng-repeat="smenu in data.subMenu[$index].list" class="ng-scope">
                        <a href="" ng-click="goToPage($index)" class="ng-binding">Background</a>

                    </li><li ng-class="{$index==currMenu}" ng-repeat="smenu in data.subMenu[$index].list" class="ng-scope">
                        <a href="" ng-click="goToPage($index)" class="ng-binding">What is KAM</a>

                    </li>
                </ul>
            </li>
            ...
    </div>

以下是 CSS:-

#menu {
    /*border-bottom:4px seagreen solid;*/
    background-color: #2d394d;

    list-style:none;
}

#menu ul {
    list-style: none;
}

#menu ul li {
    display: inline-block;
    float: none;
    /*width: 20%;*/
}


#menu ul li a{
    font-size: 10pt;
    padding:12px 24px 12px 24px;
    /*border-right:1px white solid;*/
    display:block;
    text-decoration: none;
    text-align: center;
    color:#fff;
    text-transform: uppercase;
}

#menu ul li a:hover{
}

#menu ul li.selected a {
    background-color: #1b86c2;
    color:#fff;
}


/* DropDown Menus */

#menu ul ul{
    background:#fff; /* Adding a background makes the dropdown work properly in IE7+. Make this as close to your page's background as possible (i.e. white page == white background). */
    background:rgba(255,255,255,0); /* But! Let's make the background fully transparent where we can, we don't actually want to see it if we can help it... */
    list-style:none;
    position:absolute;
    left:-9999px; /* Hide off-screen when not needed (this is more accessible than display:none;) */
}
#menu ul li ul li{
    padding-top:1px; /* Introducing a padding between the li and the a give the illusion spaced items */
    float:none;
    display: block;
}
#nav ul ul a{
    white-space:nowrap; /* Stop text wrapping and creating multi-line dropdown items */
}
#menu li:hover ul{ /* Display the dropdown on hover */
    left:0; /* Bring back on-screen when needed */
}
#menu li:hover a{ /* These create persistent hover states, meaning the top-most link stays 'hovered' even when your cursor has moved down the list. */
    background:#1b86c2;
    text-decoration:underline;
}
#menu li:hover ul a{ /* The persistent hover state does however create a global style for links even before they're hovered. Here we undo these effects. */
    text-decoration:none;
}
#menu li:hover ul li a:hover{ /* Here we define the most explicit hover states--what happens when you hover each individual link. */
    background:#333;
}

您可以在此处看到显示它的图片(图片显示“案例”的下拉菜单,它应该在案例下方,但它向左移动。“介绍”子菜单也显示在同一位置):-

【问题讨论】:

  • 悬停时的左侧设置为 0。最好将显示设置为无,直到将鼠标悬停在它上面。国际海事组织。

标签: html css drop-down-menu alignment


【解决方案1】:

这是因为left:0定位,父li的位置默认为static。哟可以通过将其标记为相对来修复它,以便子 ul 的 left:0 将相对于父 li。

#menu ul li {
    display: inline-block;
    float: none;
    /*width: 20%;*/
    position:relative; /*Add this*/
}

#menu li:hover ul{ /* Display the dropdown on hover */
     /* Bring back on-screen when needed */
    left:0;
    padding:0; /*Add this if you are not using any reset*/
}

Fiddle

【讨论】:

    【解决方案2】:

    尝试添加#menu ul li {position: relative;}

    【讨论】:

      【解决方案3】:

      更正:

      仔细观察后,我的最后一个答案不太正确。这可能不是修复它的最佳方法。但它是一种方式(或者无论如何都可以在球场上获得它)。

      在您的 CSS 中执行以下操作:

      #menu {
          /*border-bottom:4px seagreen solid;*/
          background-color: #2d394d;
          height:40px;
          list-style:none;
      }
      
      #menu ul li ul{
          position:relative;
      }
      

      这是一个演示:

      http://jsfiddle.net/2mtt8/1/

      【讨论】:

        【解决方案4】:

        看起来您的每个下拉菜单的位置都绝对在 0px:

        left:0; /* Bring back on-screen when needed */
        

        我建议将每个都相对于其父级进行定位。 您可以考虑使用display:none 隐藏下拉菜单,而不是将它们放置在屏幕外。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-01-28
          • 2017-01-17
          • 1970-01-01
          • 1970-01-01
          • 2018-05-03
          • 2014-12-01
          相关资源
          最近更新 更多