Rails 5 / Bootstrap 4:我的导航栏在桌面上显示移动下拉菜单

时间:2018-02-23 20:59:00

标签: html css ruby-on-rails bootstrap-4 navbar

这是我第一次使用Bootstrap 4.使用Bootstrap 3时,我很少使用导航类。

我正在使用Bootstrap 4模板。我按原样复制了整个样式表。我能够让它工作,除了导航栏。当我直接打开模板时,它按预期工作。但是,我的版本在下拉菜单中显示css按钮,而不是在桌面上水平显示。

以下是菜单的样子:

enter image description here

以下是我的菜单:

enter image description here

我尝试过最新版本的Safari,Chrome,Firefox和Opera。所有这些看起来完全一样。

的Gemfile

gem 'bootstrap', '~> 4.0.0'
gem 'sass-rails', '~> 5.0'
gem 'coffee-rails', '~> 4.2'
gem 'jquery-rails'

的application.js

//= require jquery
//= require rails-ujs
//= require turbolinks
//= require jquery3
//= require popper
//= require bootstrap-sprockets
//= require_tree .

application.scss

 *= require_tree .
 *= require_self

标题(更新前)

<header>
  <div class="tm-header">
      <div class="container-fluid">

        <div class="tm-header-inner">
                <%= link_to "Classic", locale_root_path, class: "navbar-brand tm-site-name" %>

          <!-- navbar -->
          <nav class="navbar tm-main-nav">

              <button class="navbar-toggler hidden-md-up" type="button" data-toggle="collapse" data-target="#tmNavbar">&#9776;</button>

              <div class="collapse navbar-toggleable-sm" id="tmNavbar">
                <ul class="nav navbar-nav">
                <li class="nav-item"><%= link_to "Home", locale_root_path, class: "nav-link" %></li>
                <li class="nav-item"><%= link_to "About", about_path, class: "nav-link" %></li>
                <li class="nav-item"><%= link_to "Blog", blog_path, class: "nav-link" %></li>
                <li class="nav-item"><%= link_to "Contact", contact_path, class: "nav-link" %></li>
                </ul>                        
              </div>

          </nav>  

        </div>     

    </div>                                  
  </div> 
</header>

CSS

.container-fluid {
    margin-left: auto;
    margin-right: auto;
    max-width: 1390px;
    overflow-x: hidden;
}

.navbar-nav .nav-link {
    border-radius: 6px;
    color: black;
    padding: 10px 40px;
}

.nav-item.active .nav-link,
.nav-link:hover,
.nav-link:focus {
    color: white;
    background-color: #cc9900;

}

.tm-header-inner {
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;
    -webkit-align-items: center;
        -ms-flex-align: center;
            align-items: center;
    -webkit-justify-content: space-between;
        -ms-flex-pack: justify;
            justify-content: space-between;
    height: 100px;
}

.tm-site-name {
    color: #cc9900;
    display: block;
    font-size: 2.6rem;
    font-weight: 400;
}

.tm-main-nav { font-size: 1.2rem; }

当我继续看时,我看到该模板使用的是Bootstrap 4的alpha版本。我将所有hidden-xx-up类更改为相应的d-xx-none d-yy-block,其中yy是下一个更高的响应级别。例如,在标题中,我将hidden-md-up更改为d-md-none d-lg-block

<button class="navbar-toggler d-md-none d-lg-block" type="button" data-toggle="collapse" data-target="#tmNavbar">&#9776;</button>

我今天做了很多网络搜索,包括在Bootstrap网站上,但找不到有关替换navbar-toggleable-xx的内容的任何信息。我找到的导航栏示例都没有使用navbar-toggleable-xx,因此我假设他们使用的是当前的稳定版本。

<div class="collapse navbar-toggleable-sm" id="tmNavbar">

如何修改此设置以使我的导航栏正常工作?我会继续寻找解决方案。

1 个答案:

答案 0 :(得分:1)

Bootstrap 4中已经没有navbar-toggleable-*类组了。

navbar-toggleable-sm替换为navbar-expand-sm

这是从所需断点开始扩展导航栏的类。因此,sm中的navbar-expand-sm表示:导航栏将从较小的(sm)屏幕尺寸向上扩展。

重要:

使用Bootstrap 4时,你根本无法使用当前的代码。

因此,即使您在代码中将navbar-toggleable-sm替换为navbar-expand-sm,它仍然无法正常工作,因为您的代码不是 Bootstrap 4代码。

这是一个有效的Bootstrap 4导航栏:

&#13;
&#13;
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

<nav class="navbar navbar-expand-lg navbar-light bg-light">
    <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" id="navbarSupportedContent">
        <ul class="navbar-nav mr-auto">
            <li class="nav-item active">
                <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">Link</a>
            </li>
            <li class="nav-item dropdown">
                <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    Dropdown
                </a>
                <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                    <a class="dropdown-item" href="#">Action</a>
                    <a class="dropdown-item" href="#">Another action</a>
                    <div class="dropdown-divider"></div>
                    <a class="dropdown-item" href="#">Something else here</a>
                </div>
            </li>
            <li class="nav-item">
                <a class="nav-link disabled" href="#">Disabled</a>
            </li>
        </ul>
        <form class="form-inline my-2 my-lg-0">
            <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
            <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
        </form>
    </div>
</nav>
&#13;
&#13;
&#13;

下一步:阅读Bootstrap 4的文档:

https://getbootstrap.com/docs/4.0/components/navbar/