Bootstrap 4:下拉菜单的宽度与导航栏中的搜索栏相同

时间:2017-12-28 01:37:42

标签: html css twitter-bootstrap bootstrap-4

请参阅下面的HTML。如果单击搜索栏(右上角),则会出现一个下拉菜单。在实际代码中,这当然是搜索结果的菜单。问题是我希望下拉框的宽度与搜索框的宽度相同。

我意识到这里有一个或多个类似的问题,但那里的答案似乎都不适用于我的具体情况。

这是我的HTML:

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous">
    <link rel="stylesheet" href="/static/css/style.css">
    <script src="//cdnjs.cloudflare.com/ajax/libs/less.js/2.7.2/less.min.js"></script>
    <script src="https://use.fontawesome.com/6e0448e881.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>

<body>
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <a class="navbar-brand" href="#">Navbar</a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>

        <div class="collapse navbar-collapse">
            <ul class="navbar-nav mr-auto">
                <li class="nav-item">
                    <a class="nav-link" href="#">Link</a>
                </li>
            </ul>

            <ul class="navbar-nav ml-auto">
                <li class="searchbar"
                    <form class="form-inline my-2 my-lg-0" method="get">
                        <input name="q" id="qbox" data-toggle="dropdown" type="search" class="form-control" placeholder="Search" autofocus="autofocus" autocomplete="off" aria-haspopup="true" aria-expanded="false"/>
                        <div id="search_results" class="dropdown-menu" role="menu" aria-labelledby="qbox">
                            <div>a</div>
                            <div>b</div>
                            <div>c</div>
                        </div>
                    </form>
                </li>

                <li class="usericon">
                    <button class="btn" id="login_dropdown" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        Log in
                    </button>
                    <div class="dropdown-menu dropdown-menu-right" aria-labelledby="login_dropdown">
                        <a class="dropdown-item" href="#">Action</a>
                        <a class="dropdown-item" href="#">Another action</a>
                        <a class="dropdown-item" href="#">Something else here</a>
                    </div>
                </li>
            </ul>

        <style>
        .searchbar .dropdown-menu {
        left: initial;
        }
        </style>

        </div>
    </nav>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js" integrity="sha384-vFJXuSJphROIrBnz7yo7oB41mKfc8JzQZiCq4NCceLEaO4IHwicKwpJf9c9IpFgh" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js" integrity="sha384-alpBpkh1PFOepccYVYDB4do5UnbKysX5WZXm3XxPqe5iKTfUKjNkCk9SaVuEZflJ" crossorigin="anonymous"></script>
</body>
</html>
编辑:这是一张导航栏在我的机器上的样子(Arch Linux,运行Chrome)。请告诉我它是否适合你! https://imgur.com/NcmzStc

1 个答案:

答案 0 :(得分:0)

根据Bootstrap 4文档,最好显示下拉菜单,<div class="dropdown">包围输入/按钮和<div class="dropdown-menu"> https://getbootstrap.com/docs/4.0/components/dropdowns/

<li class="searchbar">
  <form class="form-inline my-2 my-lg-0" method="get">
    <div class="dropdown">
      <input name="q" id="qbox" data-toggle="dropdown" type="search" class="form-control" placeholder="Search" autofocus="autofocus" autocomplete="off" aria-haspopup="true" aria-expanded="false"/>
        <div id="search_results" class="dropdown-menu" role="menu" aria-labelledby="qbox">
           <div>a</div>
           <div>b</div>
           <div>c</div>
        </div>
    </div>
  </form>
</li>

这将确保dropdown-menu的宽度不会超过input。然后确保dropdown-menu始终是input使用的100%,

div#search_results {
  width: 100%;
}

Screenshot short menu item  Screenshot long menu item