Arif Arif - 5 months ago 16
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

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).

Comments