Arif Arif - 2 years ago 172
CSS Question

Change CSS based on current page in Rails?

So I have a bootstrap navbar that I want to have one color in the pages /users/sign_in and /users/sign_up, and another color in the rest. How would I do that?

Answer Source

Add a specific CSS class to your <body> tag and use CSS to style your navbar:

# layout.html.erb
<body class="<%= body_class_helper(params) %>">

# application.css
body .navbar {
  background-color: yellow;
body.signin .navbar {
  background-color: black;
body.signup .navbar {
  background-color: red;

PS. Move out the logic of defining the right CSS class into a helper (body_class_helper).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download