bootstrap导航栏嵌套<ul>在父级下无法正确显示

时间:2018-02-07 19:11:11

标签: html twitter-bootstrap navigation

当您将页面视为移动设备时,主导航栏不会在其尊重的父链接下正确显示下拉菜单。我有搜索,似乎无法找到解决方案。目前,下拉菜单显示在主链接的左下方。您可以在http://beta.lofbc.org

看到现场演示
 <nav class="navbar navbar-default">
    <div class="container">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar-collapse-1" aria-expanded="false">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand hidden-lg hidden-md hidden-sm" href="#">Life of Faith Bible Church</a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="navbar-collapse-1">
            <ul class="nav nav-justified">
                <li>@Html.ActionLink("HOME", "index", "Home")</li>
                <li role="presentation" class="dropdown">
                    <a href="/about" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true">ABOUT US <span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <li>@Html.ActionLink("Church Services", "ChurchServices", "About")</li>
                        <li>@Html.ActionLink("What to Expect", "WhatToExpect", "About")</li>
                        <li>@Html.ActionLink("What we Believe", "whatwebelieve", "About")</li>
                        <li>@Html.ActionLink("Leadership", "Leadership", "About")</li>
                    </ul>
                </li>
                <li role="presentation" class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">MINISTRIES <span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <li>@Html.ActionLink("Children", "Children", "Ministries")</li>
                        <li><a href="https://www.facebook.com/Faith-Agents-Academy-FAA-1421388411492935/" target="_blank">FAA</a></li>
                        <li><a href="https://www.facebook.com/GodsGirlzClub/" target="_blank">God's Girlz Club</a></li>
                        <li><a href="http://www.aftershockyouth.lofbc.org" target="_blank">AfterShock Youth</a></li>
                        <li><a href="#">Young Adults</a></li>
                        <li><a href="https://www.facebook.com/menofimpactfellowship" target="_blank">Men of Impact</a></li>
                        <li><a href="http://www.jeannefraser.org" target="_blank">Women</a></li>
                        <li><a href="#">God's Golden Girls</a></li>
                        <li><a href="#">The Great Giveaway</a></li>
                    </ul>
                </li>
                <li>@Html.ActionLink("EVENTS", "", "Events")</li>
                <li role="presentation" class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">MEDIA <span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <li><a href="#">Live Stream</a></li>
                        <li>@Html.ActionLink("Service Archives", "ServiceArchives", "Media")</li>
                        <li><a href="#">TV Broadcast</a></li>
                        <li><a href="#">Photo Album</a></li>
                    </ul>
                </li>
                <li><a href="http://store.lofbc.org" target="_blank">STORE</a></li>
                <li role="presentation" class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">CONTACT <span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <li>@Html.ActionLink("Send us a Message", "index", "contactus") </li>
                        <li><a href="#">Submit Testimony</a></li>
                        <li><a href="#">Prayer Request</a></li>
                        <li>@Html.ActionLink("Give", "Donate", "Giving")</li>
                    </ul>
                </li>
            </ul>
        </div>
        <!-- /.navbar-collapse -->
    </div>
    <!-- /.container-fluid -->
</nav>

1 个答案:

答案 0 :(得分:1)

这种情况正在发生,因为您的嵌套<ul>标记绝对定位。绝对定位的元素无法移动相对定位的元素,因此即使您将其移动到它上面,它也只会显示在现有按钮的顶部。

我的修复方法是将以下css应用于嵌套的<ul>标记:

@media (max-width: 767px) {
    .nav-justified>.dropdown .dropdown-menu {
        position: relative;
        width: 100%;
        background-color: #1c5168;
    }

    .nav-justified>.dropdown .dropdown-menu li {
        color: lightblue;
        text-align: center;
    }
}

将其置于媒体查询中将阻止css应用于桌面布局上的菜单。

您仍然需要添加更多样式以使菜单看起来不错,但这会解决您的定位问题。

这里仅供参考,是一篇关于position属性的好文章及其工作原理示例:https://css-tricks.com/almanac/properties/p/position/