如何让右对齐下拉列表在320 x 480的Bootstrap导航栏中工作?

时间:2016-04-21 14:16:40

标签: html css twitter-bootstrap

我需要更改为我的Bootstrap导航栏,以便右侧的下拉菜单在较小的屏幕上工作,就像在较大的屏幕上一样:

enter image description here

完整代码:

<!DOCTYPE html>
<html>
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>SHOWCASE: Bootstrap Navbar</title>              
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
        <link href="http://netdna.bootstrapcdn.com/font-awesome/4.0.0/css/font-awesome.css" rel="stylesheet"/>     
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>   
        <script src="https://netdna.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
        <style type="text/css">
            .pagecontent {
                margin: 0 18px;
            }
            /* bootstrap override */
            .container {
                width: 100%;
                padding: 0;
            }
            .navbar {
                border-radius: 0px;
            }
            .container a {
                color: yellow;
            }
            .navbar-text {
                float: left !important;
                margin-right: 10px;
            }
            .navbar-right {
                float: right!important;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <nav class="navbar navbar-inverse">
                <div class="container-fluid">
                    <div class="navbar-header pull-left">
                        <a class="navbar-brand" href="#">Company Name</a>
                    </div>                
                    <ul class="nav navbar-nav visible-sm-block visible-md-block visible-lg-block">
                        <li class="active"><a href="#">Page 1</a></li>
                        <li><a href="#">Page 2</a></li>
                        <li><a href="#">Page 3</a></li>
                    </ul>                    
                    <ul class="nav navbar-nav navbar-right">
                        <li class="dropdown pull-right">
                            <a href="#" data-toggle="dropdown" class="dropdown-toggle"><span class="glyphicon glyphicon-menu-hamburger"></span></a>
                            <ul class="dropdown-menu">
                                <li><a href="#">Menu 1</a></li>
                                <li><a href="#">Menu 2</a></li>
                                <li class="divider"></li>
                                <li><a href="#">Menu A</a></li>
                                <li><a href="#">Menu B</a></li>
                                <li><a href="#">Menu C</a></li>
                            </ul>                        
                        </li>
                        <li class="dropdown pull-right">
                            <a href="#" data-toggle="dropdown" class="dropdown-toggle"><span class="glyphicon glyphicon-cog"></span></a>
                            <ul class="dropdown-menu">
                                <li><a href="#">Settings 1</a></li>
                                <li><a href="#">Settings 2</a></li>
                                <li><a href="#">Settings 3</a></li>
                                <li class="divider"></li>
                                <li><a href="#">Settings A</a></li>
                                <li><a href="#">Settings B</a></li>
                            </ul>
                        </li>
                    </ul>                      
                </div>
            </nav>
            <div class="pagecontent">
                <p class="visible-xs-block">seen on xs screens.</p>
                <p class="visible-sm-block">seen on sm screens.</p>
                <p class="visible-md-block">seen on md screens.</p>
                <p class="visible-lg-block">seen on lg screens.</p>
            </div>
        </div>
    </body>
</html>

Dasith解决方案的问题:

enter image description here

解决此问题的方法是简单地添加背景颜色:

.navbar-nav .open .dropdown-menu {
       background-color: #fff;

问题#2与Dasith的解决方案:

enter image description here

1 个答案:

答案 0 :(得分:1)

通过chrome dev工具进行快速检查让我做出了这个修复:

    @media (max-width: 767px){
    .navbar-nav .open .dropdown-menu {
           border: 1px solid rgba(0,0,0,.15);
           position:absolute;
           box-shadow: 0 6px 12px rgba(0,0,0,.175);
    }
    .navbar-inverse .navbar-nav .open .dropdown-menu>li>a:focus, .navbar-inverse .navbar-nav .open .dropdown-menu>li>a:hover {
        color: #000;

    }
   }

这是一个带有上述编辑的调整后的版本,以便更好地了解它是如何发挥作用的 here..