Modify the background color of a single page while keeping the other pages unchanged

Is there a way to have a blue background on only one page of my rails application, while keeping all other pages with a white background? I attempted using the following code:

<%= javascript_include_tag params[:controller] %> or 

<%= stylesheet_link_tag params[:controller] %>

I followed this tutorial, but unfortunately, it did not work as expected.

Answer №1

One way to achieve this is by implementing a different layout for that specific page.

In your controller:

def something
  render :layout => 'new_layout'
end

You can also use a yield block to override the CSS on that page.

In your layout file:

<%= stylesheet_link_tag %>
<%= yield(:head) %>

For the view you wish to modify:

<% content_for :head do %>
  <style>
    body {
      background-color: #b0c4de;
    } 
  </style>
<% end %>

Answer №2

Assigning a specific class to the body based on the controller is a helpful technique.

In the layout file, you can do the following:

<body class="<%= params[:controller] %>">

Then, in your CSS stylesheet, you can target this class like so:

body.controller_name {
    background-color: blue;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Switch from using pixels to using rems by utilizing the search and replace feature in VS

I have an older CSS project that primarily uses px as the default unit of measurement. Now, I am looking to revamp the project and need to change all instances of px to rem using the search and replace feature in VSCode. The conversion ratio is set at 10p ...

What is the reason that elements with % properties totaling 100% do not align next to each other?

I've been attempting to arrange two blocks side by side within another block, but they just don't seem to fit together smoothly. What could be causing this issue? .container {height: 200px; width: 400px; background:darkgrey;} .left {height: 100% ...

Learn the steps to update PHP headers on a specific website using AJAX

contactfrm.php <?php // Server side validation $name = $_POST['name']; $surname = $_POST['surname']; $bsubject = $_POST['subject']; $email= $_POST['email']; $message= $_POST['message']; $to = " ...

Disable touch interactions on the body, only allow pinch-zoom on specific elements

I have been attempting to implement the following code: body { touch-action: none; } .left-side { touch-action: pinch-zoom; } <div class="left-side"><img src="image.jpg" /></div> The touch-action: none is functioning properly, but ...

Utilizing jQuery to implement a CSS style with a fading effect, such as FadeIn()

I have a jQuery script that applies a CSS style to an HTML table row when the user clicks on a row link: $(this).closest('tr').css("background-color", "silver"); Is there a way to soften this color change effect, such as by gradually fading in ...

How to effectively handle submenus within a Bootstrap 4 mobile menu?

I am currently updating our website from Bootstrap 3.3.7 to Bootstrap 4. The header contains a navigation bar with dropdown items. https://i.sstatic.net/n2P6U.png <header> <nav class="navbar fixed-top navbar-expand-lg navbar-light bg-white bg- ...

Limiting the width of the child UL within an LI element in a dropdown navigation menu

I am trying to find a way to limit the width of a <ul> tag within a dropdown menu in CSS to match the width of its parent <li> element without setting a fixed width. Despite my efforts, I have not been successful in achieving this and I am seek ...

Experiencing issues with the redirect button on the navigation bar of my website

Video: https://youtu.be/aOtayR8LOuc It is essential that when I click a button on my navigation bar, it will navigate to the correct page. However, since the same nav bar is present on each page, it sometimes tries to redirect to the current page multiple ...

After examining the width properties in the CSS code, it was discovered that one particular icon is larger in

Is there a way to adjust the width of the first icon in my container using CSS? The first icon appears larger than the others, and I'm having trouble making it the right size. #features { margin-top: 35px; } .grid { display: flex; } #feat ...

What are the steps to create a responsive form using CSS?

I have a screenshot below that needs to be recreated using HTML/CSS. This design should be consistent in both desktop and mobile views. https://i.stack.imgur.com/cnfHt.png Currently, I have managed to replicate this in a JSFiddle which looks good on desk ...

Combining color and typography classes using Tailwind merge is currently not supported

In my custom styling setup, I have created a custom color class called text-green (colors) and a custom typography class called text-card-highlight (utilities), which includes font size and weight attributes. However, when using tailwind-merge, only one of ...

Inconsistencies in spacing between shapes bordering an SVG Circle using D3.js are not consistent across platforms

After creating a SVG circle and surrounding it with rectangles, I am now attempting to draw a group of 2 rectangles. The alignment of the rectangle combo can either be center-facing or outside-facing, depending on the height of the rectangle. However, I am ...

Display my search box when the button is activated

I have a button labeled "Search" that I want to click in order for my search field (which is currently hidden with display:none) to become visible. However, my current setup is not working as intended. I have created a jsfiddle to illustrate my issue. Al ...

Is there a way to ensure that spans are inline with a div?

My structure is pretty straightforward: <div></div><span></span><span></span> However, I'm looking to have them all appear on a single line. Currently, they are rendering like this: <div /> <span />&l ...

Is there a way for JavaScript to generate an image and smoothly transition it from its initial state to its final style?

I am currently working on a JavaScript game that involves moving IMG elements by adjusting their x/y/width/height/opacity properties and utilizing CSS transitions to smoothly move them to their target locations, triggering an event upon arrival. Everything ...

Content misaligned vertically inside a DIV

I'm struggling to understand why the browser is not allowing me to apply margin-top or padding-top to a DIV in order to center the text. HTML - <div id="header"> <div id="Nav"> <div id="navright"> ...

Ways to Randomly Flip Divs

I have developed an application where all divs flip vertically on hover. I am looking for a way to make the flipping random without requiring a hover. Any ideas on how to achieve this? .vertical.flip-container { position: relative; float: left; ma ...

How can I resize an element using jQuery resizable and then revert it back to its original size with a button click?

I need help figuring out how to revert an element back to its original size after it has been modified with .resizable. I attempted the following: <!DOCTYPE html> <html> <head> <link rel="stylesheet" href="//code. ...

How can I customize the tooltip placement in Bootstrap 5 while utilizing the 'text-truncate' attribute?

In my HTML table, I have long text in a cell that I don't want to display fully. To truncate the text, I am using the 'text-truncate' CSS attribute. Additionally, I am considering using Bootstrap tooltip to show the full text when needed. He ...