如何限制导航栏中 Bootstrap 3 下拉菜单的宽度 [英] How To Restrict Width of Bootstrap 3 Dropdown-Menu in Navbar

查看:19
本文介绍了如何限制导航栏中 Bootstrap 3 下拉菜单的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想防止 Bootstrap .dropdown-menu 比主导航栏更宽(我限制为 .container 的宽度)班级).我希望下拉菜单占据容器类的宽度,但它不应超出任何一侧的宽度.

任何想法如何实现这一点,最好使用 CSS?

以下是它目前的样子以及我希望它的样子的示例:

这是一个带有下拉菜单的样板导航栏,其中有一个非常长的菜单项:

<link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.css"><script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><script type="text/javascript" src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script><div class="容器"><nav class="navbar navbar-default" role="navigation"><!-- 品牌和切换被分组以获得更好的移动显示--><div class="navbar-header"><button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1"><span class="sr-only">切换导航</span><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span><a class="navbar-brand" href="#">Brand</a>

<!-- 收集导航链接、表单和其他用于切换的内容--><div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"><ul class="nav navbar-nav"><li class="active"><a href="#">Link</a></li><li><a href="#">链接</a></li><li class="下拉菜单"><a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown <span class="caret"></span></a><ul class="dropdown-menu" role="menu"><li><a href="#">动作</a></li><li><a href="#">另一个动作是一个非常长的菜单项,它超出了容器"的限制,甚至可能超出了右侧的视口区域,使一些文本无法阅读.</a></li><li><a href="#">这里还有别的东西</a></li><li class="divider"></li><li><a href="#">分隔链接</a></li><li class="divider"></li><li><a href="#">一个单独的链接</a></li></div><!--/.navbar-collapse --></nav>

解决方案

说明

Bootstrap 的

.full-width.dropdown {位置:静态;}.full-width.dropdown >.下拉式菜单 {右:0;}.full-width.dropdown >.下拉菜单>立>一个 {空白:正常;}.fill-width.dropdown {位置:静态;}.fill-width.dropdown >.下拉式菜单 {左:自动;}.fill-width.dropdown >.下拉菜单>立>一个 {空白:正常;}

<link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.css"><script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><script type="text/javascript" src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script><div class="容器"><div class="navbar navbar-inverse"><!-- 标题--><div class="navbar-header"><button type="button" class="navbar-toggle"数据切换=折叠"data-target=".navbar-collapse"><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span><a class="navbar-brand" href="#">Bootstrap 3 骨架</a>

<!-- 导航栏链接--><div class="collapse navbar-collapse"><ul class="nav navbar-nav"><li class="下拉菜单"><a href="#" class="dropdown-toggle"数据切换=下拉">正常 <span class="caret"></span></a><ul class="dropdown-menu" role="menu"><li><a href="#">动作</a></li><li><a href="#">另一个动作</a></li><li><a href="#">真的真的真的真的真的真的真的真的真的真的真的真的真的真的真的真的真的真的真的真的真的真的真的真的真的真的真的很长</a></li><li class="divider"></li><li><a href="#">分隔链接</a></li><li class="divider"></li><li><a href="#">一个单独的链接</a></li><li class="下拉全宽"><a href="#" class="dropdown-toggle"数据切换=下拉">全宽<span class="caret"></span></a><ul class="dropdown-menu" role="menu"><li><a href="#">动作</a></li><li><a href="#">另一个动作</a></li><li><a href="#">真的真的真的真的真的真的真的真的真的真的真的真的真的真的真的真的真的真的真的很长</a></li><li class="divider"></li><li><a href="#">分隔链接</a></li><li class="divider"></li><li><a href="#">一个单独的链接</a></li>

  • <a href="#" class="dropdown-toggle"数据切换=下拉">填充宽度<span class="caret"></span></a><ul class="dropdown-menu" role="menu"><li><a href="#">动作</a></li><li><a href="#">另一个动作</a></li><li><a href="#">真的真的真的真的真的真的真的真的真的真的真的真的真的真的真的真的真的真的真的很长</a></li><li class="divider"></li><li><a href="#">分隔链接</a></li><li class="divider"></li><li><a href="#">一个单独的链接</a></li>
  • I would like to prevent the Bootstrap .dropdown-menu from ever being wider than the main nav bar (which I have restricted to be the width of the .container class). I'd like for the dropdown menu to take up the width of the container class, but it shouldn't extend beyond that on either side.

    Any ideas how to accomplish this, preferably using CSS?

    Here's an example of what it currently looks like and what I'd like it to look like:

    Here's a boiler plate navbar with a dropdown menu that has one really long menu item:

    <link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script type="text/javascript" src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
    
    <div class="container">
      <nav class="navbar navbar-default" role="navigation">
        <!-- 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="#bs-example-navbar-collapse-1">
            <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" href="#">Brand</a>
        </div>
    
        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
          <ul class="nav navbar-nav">
            <li class="active"><a href="#">Link</a></li>
            <li><a href="#">Link</a></li>
            <li class="dropdown">
              <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown <span class="caret"></span></a>
              <ul class="dropdown-menu" role="menu">
                <li><a href="#">Action</a></li>
                <li><a href="#">Another action a really long menu item that extends way beyond the limits of "container" and may even extend beyond the view port area to the right, making some of the text unreadable.</a></li>
                <li><a href="#">Something else here</a></li>
                <li class="divider"></li>
                <li><a href="#">Separated link</a></li>
                <li class="divider"></li>
                <li><a href="#">One more separated link</a></li>
              </ul>
            </li>
          </ul>
        </div><!-- /.navbar-collapse -->
      </nav>
    </div>

    解决方案

    Explanation

    Bootstrap's adds position: absolute to the .dropdown-menu class. As you may know, all absolutely positioned elements are positioned in relation to the first parent element they find with position: relative. In Bootstrap, this is provided by the .dropdown wrapper

    So if you want to position the element relative to the container, and not the nav item, we'll have to remove relative positioning from the .dropdown wrapper. You can do this by resetting the value to the initial value for the position property, which is static.

    Congratulations! The menu is no longer constrained by the .dropdown element, but we still have some work to do.

    Because bootstrap was not intending to space constrain the menu, menu items are given the property white-space: nowrap so they'll extend as long as they need. Think lines of code inside code blocks here on stack overflow (1 line = 1 line). Since we want the line to eventually end, this won't do. So we'll reset the anchor tags back to white-space: normal.

    At this point the .dropdown-menu should take up the full size of the .navbar (which itself takes up the full size of the .container). This is where yamm3 is doing something really cool. It sets left: auto on the dropdown-menu.

    According to MDN on the left property:

    auto is a keyword that represents:
    for absolutely positioned elements, the position of the element based on the right property and treat width: auto as a width based on the content.

    So setting the .dropdown-menu to left:auto will cause the menu to start in its current location and extend all the way to the right of the container.

    Just Codes

    Just add the .fill-width class to your .dropdown element and include the following CSS:

    .fill-width.dropdown {
        position: static;
    }
    .fill-width.dropdown > .dropdown-menu {
        left: auto;
    }
    .fill-width.dropdown > .dropdown-menu > li > a {
       white-space: normal; 
    }
    

    Working Demo in jsFiddle

    .full-width.dropdown {
        position: static;
    }
    .full-width.dropdown > .dropdown-menu {
        right: 0;
    }
    .full-width.dropdown > .dropdown-menu > li > a {
       white-space: normal; 
    }
    
    
    .fill-width.dropdown {
        position: static;
    }
    .fill-width.dropdown > .dropdown-menu {
        left: auto;
    }
    .fill-width.dropdown > .dropdown-menu > li > a {
       white-space: normal; 
    }

    <link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script type="text/javascript" src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
    
    
    <div class="container">
        <div class="navbar navbar-inverse">
    
            <!-- Header -->
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" 
                        data-toggle="collapse" 
                        data-target=".navbar-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="#">
                    Bootstrap 3 Skeleton
                </a>
            </div>
            <!-- Navbar Links -->
            <div class="collapse navbar-collapse">
                <ul class="nav navbar-nav">
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" 
                        data-toggle="dropdown">
                            Normal <span class="caret"></span>
                        </a>
                        <ul class="dropdown-menu" role="menu">
                            <li><a href="#">Action</a></li>
                            <li><a href="#">Another action</a></li>
                            <li><a href="#">Really Really Really Really Really Really Really Really Really Really Really Really Really Really Really Really Really Really Really Really Really Really Really Really Really Really Long
                                </a></li>
                            <li class="divider"></li>
                            <li><a href="#">Separated link</a></li>
                            <li class="divider"></li>
                            <li><a href="#">One more separated link</a></li>
                        </ul>
                    </li>   
                    <li class="dropdown full-width">
                        <a href="#" class="dropdown-toggle" 
                        data-toggle="dropdown">
                            Full Width <span class="caret"></span>
                        </a>
                        <ul class="dropdown-menu" role="menu">
                            <li><a href="#">Action</a></li>
                            <li><a href="#">Another action</a></li>
                            <li><a href="#">Really Really Really Really Really Really Really Really Really Really Really Really Really Really Really Really Really Long
                                </a></li>
                            <li class="divider"></li>
                            <li><a href="#">Separated link</a></li>
                            <li class="divider"></li>
                            <li><a href="#">One more separated link</a></li>
                        </ul>
                    </li>          
                    <li class="dropdown fill-width">
                        <a href="#" class="dropdown-toggle" 
                        data-toggle="dropdown">
                            Fill Width <span class="caret"></span>
                        </a>
                        <ul class="dropdown-menu" role="menu">
                            <li><a href="#">Action</a></li>
                            <li><a href="#">Another action</a></li>
                            <li><a href="#">Really Really Really Really Really Really Really Really Really Really Really Really Really Really Really Really Really Long
                                </a></li>
                            <li class="divider"></li>
                            <li><a href="#">Separated link</a></li>
                            <li class="divider"></li>
                            <li><a href="#">One more separated link</a></li>
                        </ul>
                    </li>   
                </ul>
            </div>
        </div>
    </div>

    这篇关于如何限制导航栏中 Bootstrap 3 下拉菜单的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    前端开发最新文章
    热门教程
    热门工具
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆