Inquiries regarding CSS consolidation in the latest version of Rails, Rails 7

Starting a new Rails 7 application with Bootstrap CSS and JS bundling, along with CSS bundling, has resulted in the following structure:

app/assets/builds,
app/images/foo.jpg,
app/assets/stylesheets/application.bootstrap.scss

I have utilized Yarn to add Bootstrap to package.json, ESBuild for JS build, and Sass for CSS build.

Everything functions properly until I attempt to add a simple CSS class to the application.bootstrap.scss file:

.bg {
  background-image: url("foo.com");
}

What I actually desire is for the asset in app/assets/images/foo.jpg to be referenced correctly. Since it is a scss file, using sass commands like:

background-image: image-url("foo.com");

or

background-image: url(image-path("foo.com"));

do not seem to work in development or production, resulting in errors related to syntax issues. This indicates that the sass compile is not generating a valid css build.

What should I do to ensure a simple class with an image asset is included in my delivered application.css bundle? Do I need to create a separate css file and include it in the sprockets manifest, or is there a simpler solution?

The only workaround that worked for me was adding an inline style to my ERB layout, which is less than ideal but allowed me to proceed for the time being.

Additionally, I would like to reference an image added to the Rails App so that is accessible. If I decide to use /app/assets/images/my-image.jpg instead of /public/my-image.png, what would the correct link be? Would it be fingerprinted and therefore inaccessible, or am I mistaken? Is there any benefit to referencing an asset as opposed to storing it in /public, or am I obligated to use /public?

Answer №1

take a look at this link for more information: https://github.com/rails/cssbundling-rails/issues/102

were you able to experiment with the following code?

.test { background-image: url("foo.jpg"); }

check out this link for initializer options: https://github.com/rails/sprockets-rails#initializer-options

config.assets.resolve_assets_in_css_urls

If you enable this option, sprockets-rails will utilize a CSS postprocessor to resolve assets referenced in url() function calls and substitute them with digested paths by default.

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

The issue with the page width is occurring due to a bug related to the

When pages contain the AddThis code, they tend to become very wide due to the code itself, resulting in a horizontal scroll bar appearing. However, by removing the code or changing the page direction to LTR, the issue is resolved and the page width return ...

Looping through required fields in jQuery based on a CSS class

Is there a way to loop through required fields marked with <input class="required">? Currently, only the first input in the loop is being evaluated. How can I traverse the DOM using a loop to check all input boxes? Thank you for your help. While I ...

Create a responsive DIV element within a website that is not originally designed to be responsive

I am looking to optimize the positioning of an ad div on my non-responsive website. The current setup has the ad displayed at 120x600 pixels, always floating to the right of the screen. While this works well for desktop or large devices, the display become ...

Ways to implement the CSS on images with an ID such as img120 within a div that has a class of 'open'

<div class='cluster' id='12' 'style='width:350px;min-height:150px;border:4px solid #339966;'> <div class='image' style='width:150px;height:150px;border:2px solid black;float:left;margin-bottom: ...

Why is my image being stretched by flexbox instead of maintaining its aspect ratio?

One interesting aspect of Flexbox is how it handles image resizing. When you resize the width of an image within a flex container, the height does not adjust proportionally. This causes the image to appear stretched. div { display: flex; flex-wrap: ...

What is the best way to ensure that all rows in flexbox have the same number and dimensions as the first row?

My objective with flexbox was to evenly distribute the text across the width of the page. When I say 'evenly distributed', I mean: If there are 3 sections, the center of the text in each section should be at fifths of the webpage width. This is ...

What is the best way to align a GIF and two rows of text within a container div?

Check out my JSFiddle below: In this fiddle, there is an image sized 32 by 32 on the left side and two lines of text on the right. I need help aligning the center of the gif and the text with the middle of the enclosing frame. I've tried adjusting t ...

Vertical centering with stacked flexboxes is malfunctioning in Chrome

There is an issue with the following code snippet in Google Chrome: <div class="flex-container"> <div class="flex-item"> <div> <div> <h2>This is a test</h2> </div> </ ...

Exploring with jQuery to discover the contents located at a specific X/Y position?

Hi there, I need help with the following code snippet: function getLocation() { var positionLeft = $('#element').position().left; var positionTop = $('#element').position().top; } I also have this line of code: $('ul#con ...

Is there a way to determine if a user is using a mobile device and automatically direct them to a mobile-friendly website?

Currently, I am managing 2 websites and I would like to implement an automatic redirection for mobile users. Despite my efforts in researching various websites and viewing tutorials, I have not been successful in making it work. My intention is to redire ...

If there are no spaces, text will be removed from my list group

While working on developing a forum, I encountered an issue where if I repeatedly input letters or words without any spaces, it causes everything to overlap. Below is an example highlighting this problem: https://i.sstatic.net/KStNq.png Although there is ...

The CSS transformation is being overridden

Having an issue where I have two functions that both apply a CSS transform to an element using jQuery, but they end up overwriting each other. These functions are called at different times and have no knowledge of each other. An example showcasing the pro ...

The CSS hamburger icon displayed in the browser features three bars of varying heights

Looking to create a hamburger icon menu using only CSS? Check out my implementation below which includes three span tags in the HTML document. .sidebar-toggle { display: inline-block; } .sidebar-toggle span { display: block; width: 1.5rem; heig ...

Filtering records in Rails within a many-to-many relationship

My database has a many-to-many relationship between books and authors, defined as follows: class Book < ApplicationRecord has_many :books_authors, inverse_of: :author, dependent: :destroy has_many :authors, through: :books_authors end class Author ...

Hover effects using CSS3 transitions for the content before

Greetings everyone, I apologize for any language barriers. The title may not be clear at first glance, so let me explain what I am attempting to achieve. I have a navigation menu structured like this: <nav> <ul> <li>& ...

The default theme has not been specified in Tailwind CSS

During my recent project, I utilized React, Next.js in combination with Tailwind CSS. In this project, I delved into styling customization by implementing react-slick for a specialized slider. To achieve the desired aesthetic, I made modifications to the r ...

Copy the CSS path from Firebug and paste it into the command line suggested

Currently, I am working on customizing the firebug plugin for Firefox. My goal is to enable a feature where when a user Inspects an element and clicks 'Copy CSS path', the CSS path will automatically be pasted onto the command line of the firebug ...

Learn how to assign an image to a div element when it is collapsed, and then reveal the content when clicked on to expand

I am working on a three div with expand collapse functionality. When I expand one div, I want to set some image to the other divs. And when I go back to normal mode, I need the original content that was inside each div. $("a.expansion-btn").click(functi ...

Is there a way to change the orientation of the cards so that they move from left to right instead of

I am looking to have my cards displayed from left to right rather than vertically, but I'm struggling to achieve this. I have tried using the float: left property with no success. Below is an example of a single card in my layout. They all follow the ...

Having difficulty getting the forEach method to function correctly on Internet Explorer

I successfully implemented an active/disable feature in my code that functions flawlessly on both Chrome and Firefox. However, I am encountering an error in IE specifically when using the forEach method. Upon testing, the following error message appears w ...