The hierarchy of styles in Rails CSS/SCSS

I'm currently exploring the concept of css precedence rules in rails.

In my application, I am looking to apply different css rules based on different controllers. However, when I add css to one of the css.scss files, it ends up affecting all controller pages.

Here are snippets from admin.css.scss and rsv_ps.css.scss:

body {
    background: #fff;
}
body {
    background: url("DSC_1581.JPG") no-repeat center center fixed;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
    filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='DSC_1581.JPG', sizingMethod='scale');
    -ms-filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='DSC_1581.JPG', sizingMethod='scale')";
}

Strangely, when I visit both http://localhost:3000/admin or http://localhost:3000/rsvps/new, they both display the background image.

Shouldn't URLs starting with http://localhost:3000/admin use the admin.css.scss stylesheet instead of the rsv_ps.css.scss stylesheet?

Answer №1

@meagar and @catfish have made valid points, although it's curious why they haven't posted their answers.

The reason for having separate pages is simply for organization purposes, not to keep the CSS separated. The asset pipeline will merge and compress all the individual CSS files into one large file.

Therefore, you must segregate your styles by utilizing specific selectors.

For instance:

# For admin
body.admin {

}

# HTML
<body class='admin'>


# For everything else
body.default {

}

# HTML
<body class='default'>

To define the markup in your layout:

<body class="<%= @admin ? 'admin' : 'default' %>">

Answer №2

To customize which files are compiled into application.css, you can remove the line *= require_tree . from your manifest file and set it up manually. Alternatively, you could rename application.css to application.css.scss and use @import to include specific stylesheets. Remember to add any new .css/.scss files in

config/environments/production.rb
.

Another option is to apply a unique css class to the body for admin views, overriding the default styling:

<body class='<%= 'admin' if @admin %>'>

You can then set the value of @admin using a filter in your controller.

Answer №3

When using the Rails assets pipeline
, a single minified version is generated for all included css files and added to every page.

In order to apply different styles to different pages, it is important to use separate selectors. Another option is to use layouts to differentiate styles, although creating multiple layouts just for this purpose may not be ideal.

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

3-Column Layout with Left-Floated Columns Displaying Incorrect Order

Currently, I am in the process of converting a 3-column website to be responsive by using percentages and em's instead of negative pixel margins. This will ensure that the site functions well on various devices. I am facing a similar issue as discuss ...

Tips for styling jqgrid header columns with CSS

Currently, I am creating a table using jqgrid. One of my objectives is to modify the background color and text color of the header. In my attempts to achieve this, I am experimenting with applying a class to the header. How can I effectively implement th ...

Transform preexisting Vue UI library measurements into pixels

I was curious about converting all existing units (em / rem) to pixels. How can I easily convert the entire output units to px? Are there any tricks or methods available? Currently, I am utilizing Vuesax UI to create a plugin for various websites and temp ...

Make sure to add 'normalize-scss' to your Vue CLI 3 configuration

Recently integrated the normalize-scss package into my brand new Vue project, but unfortunately, none of the styles seem to be taking effect... I've experimented with both approaches: @import 'normalize-scss' in my styles.scss import &apos ...

I successfully created an animation with jQuery for an image, but unfortunately, it seems to malfunction during the second

$( "#btn1" ).click(function() { var $this = $(this); var clickCount = ($this.data("click-count") || 0) + 1; $this.data("click-count", clickCount); if (clickCount%2 == 0) { $("#img1").hide('clip').animate({ margi ...

What could be causing my getAsFile() method to return null?

Below is the code I have been working on: document.getElementById("Image_Panel").addEventListener('paste', (event) => { console.log("Initiating image paste - Step 1"); const clipboardData = event.clipboardData; // Checking ...

Customize Your Wordpress Category Title with a Background Image

Within my wordpress template, there is a module known as new_boxs. The code snippet below shows that this module displays on the page six times, with two columns wide and three rows down: foreach($data_cat as $cat){ ?> <div class="span6"> ...

What can I do to prevent Masonry from floating all of my grid items to the left?

Is there a way to center justify the masonry grid items? I have noticed that my Masonry layout is aligning all the grid items to the left or justifying them to the left side: <div class="wrap"> <div class="parent grid"> <div class=" ...

Tips for creating a div element with a header and scrollable section

Greetings! I am currently developing a code to manage a sprinkler system interface with a modern and visually appealing design. At the moment, I have two columns on the page, each containing boxes. Unfortunately, the alignment of these boxes is not as desi ...

What is the best way to align my grid cells to the center?

I'm facing an issue where the grid cells do not move closer together as I resize the window, instead they just stay in their place. I want to achieve a similar layout to . To share my project code, I have uploaded it on codepen: https://codepen.io/Ale ...

Is there a way to prevent Bootstrap 4's grid system from splitting column content when resizing the browser window?

Just delving into Bootstrap's grid system for the first time here, so bear with me if this sounds silly. My goal is to create a 3 column page that is responsive, featuring an image and a Spotify player in each column. Everything looks good on a full ...

Dynamic Navigation Menu: Subcategories Crossing Over Menubar Items

Task Clarification Lately, I've been working on developing a left-side navigation bar for my webpage. This bar consists of two levels of items. When viewed on a smaller device, only the icons of the first level appear. Hovering over these icons trigg ...

In Firefox, selecting the <option disabled> feature causes the corresponding option item to vanish

<select class="form-control" id="service_select"> <option disabled="disabled"></option> </select> It works fine in Chrome, but is there an alternative method to disable it without hiding it? Thank you. ...

CSS - Ensuring the second line of text in ul/ol li does not extend further left than the first line

My website features several lists that have this format: > lorem ipsum I am trying to style them like this: > lorem ipsum If anyone can provide guidance on how to rephrase the question I am asking, I would greatly ap ...

Design a background image that is optimized for both high-resolution retina displays and standard non-ret

Scenario I am working on a web page where I want the background image to be fixed, covering the entire screen or window (excluding tablets and smartphones). The background image has been created using ImageShack. Everything is running smoothly so far. T ...

Issue with align-items-* class in Bootstrap 4 causing alignment problems

Currently in the process of constructing a website utilizing the Bootstrap 4 framework and encountering difficulties with aligning items properly. For instance: Refer to Bootstrap 4 official documentation for row positioning Compare to my row positionin ...

What is the reason for the background image not appearing while utilizing a bootstrap template designed for it?

I recently started working on an asp.net application and decided to integrate a pre-made bootstrap template for the frontend. After some searching, I came across this one. The original site showcasing the template looked really appealing with a background ...

Two interdependent select fields

I am currently working on creating two select fields where, upon selecting an option in the first field, some options in the second field should be hidden. I have almost achieved this, but my script is unable to locate certain options in the first select f ...

Center and align pictures within a stationary container

I am working on a simple webpage that has a fixed footer. In this footer, I want to have 5 images centered with equal distances between them. Below is my HTML/CSS : Footer div : div.fixed { position: fixed; bottom: 0; right: 0; width: ...

Save this text in HTML format to the clipboard without including any styling

When using this code to copy a htmlLink to the clipboard: htmlLink = "<a href='#'>link</a>"; var copyDiv = document.createElement('div'); copyDiv.contentEditable = true; document.body.appendChild(copyDiv); ...