简体   繁体   English

如何在Rails中用自定义样式覆盖引导样式

[英]How to override bootstrap styles with custom ones in Rails

I'm trying to override the default theme for the navbar in my rails page but it only seems to work with !important most of the time. 我正在尝试在Rails页面中覆盖导航栏的默认主题,但大多数情况下似乎只与!important一起使用。

Is their any way I could have my custom stylesheet loaded last in the precompiler? 他们有什么办法可以在预编译器中最后加载我的自定义样式表吗?

It seems to only be a problem when changing the navbar styles. 更改导航栏样式时,这似乎只是一个问题。 I have my navbar in a separate header partial. 我的导航栏位于单独的标题部分中。 Could that be it? 可以吗?

custom.scss custom.scss

@import 'bootstrap';
@import 'bootstrap-sprockets';

$light-blue: #01579b;
$light-grey: #f5f5f5;

/* main-nav-bar */
.navbar {
  background-color: white;
}
.nav.navbar-nav a, .navbar-brand {
  color: $light-blue !important;
  transition: all 0.3s;
}
.navbar-nav .active a {
  background-color: $light-grey !important;
}
.navbar-nav a:hover {
  background-color: $light-grey !important;
}

application.scss application.scss

/*
 *= require_tree
 *= require_self
*/

html html

<nav class="navbar navbar-default navbar-static-top">
    <div class="container-fluid">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar-collapse-1">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="#">OBOOK</a>
            </div>
            <div class="collapse navbar-collapse" id="navbar-collapse-1">
                <ul class="nav navbar-nav">
                    <li class="active">
                        <%= link_to 'Home', root_path %>
                    </li>
                    <% if user_signed_in? %>
                    <li><%= link_to 'Users', users_path %></li>
                    <li class="dropdown">
                        <%= link_to 'Profile', current_user, class: "dropdown-toggle", data: {toggle: "dropdown"} %></li>
                    <li><%= link_to 'Settings', edit_user_registration_path %></li>
                    <li><%= link_to 'Sign Out', destroy_user_session_path, method: :delete %></li>
                </ul>
                <p class="navbar-text navbar-right">Signed in as: <%= link_to current_user.email, current_user %></p>
                <% else %>
                <ul class="nav navbar-nav">
                    <li><%= link_to 'Sign in', new_user_session_path %></li>
                </ul>
                <% end %>
                <ul class="nav navbar-nav">
                    <li><%= link_to 'About', about_path %></li>
                </ul>
            </div>
        </div>
    </div>
</nav>

需要在application.css中使用下面的自定义css文件或文件夹。如果这样做无济于事,请查看Chrome webdeb中完整的类链以及过去的自定义文件。

I'd recommend you include bootstrap as a gem (If you aren't already). 我建议您将Bootstrap包含为gem (如果尚未添加)。 Then you could drill down into the gem files and customise the existing CSS in there for instance. 然后,您可以深入到gem文件并在其中自定义现有CSS。

You'll find the styles here: 您将在这里找到样式:

app/vendor/bundle/gems/bootstrap-sass-3.3.5/assets/stylesheets/.

Keep in mind that if you update your gems, these custom changes will in all likelihood be overwritten. 请记住,如果您更新宝石,则这些自定义更改很可能会被覆盖。

Alternative Approach 替代方法

If you go to getbootstrap.com , and click on the “Customise” link, you'll be taken to the page where you can customise Bootstrap to do exactly what you need it to do, this includes messing with the default color scheme to a certain extent. 如果您访问getbootstrap.com并单击“自定义”链接,您将转到可自定义Bootstrap以完全执行所需操作的页面,其中包括将默认配色方案弄乱。在一定程度上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM