Custom CSS not working with CSS Bundling for Rails 7

12.9k Views Asked by At

Playing around with Rails 7 and I don't understand why my custom CSS is not working.

I built new rails app with flag for Bootstrap, which is working fine (CSS and JS, tested with bootstrap modal). These are my default config files:

application.js

// Entry point for the build script in your package.json
import "@hotwired/turbo-rails"
import "./controllers"
import * as bootstrap from "bootstrap"

application.bootstrap.scss

@import 'bootstrap/scss/bootstrap';

package.json

{
  "name": "app",
  "private": "true",
  "dependencies": {
    "@hotwired/stimulus": "^3.0.1",
    "@hotwired/turbo-rails": "^7.1.0",
    "@popperjs/core": "^2.11.2",
    "bootstrap": "^5.1.3",
    "esbuild": "^0.14.23",
    "jquery": "^3.6.0",
    "popper.js": "^1.16.1",
    "sass": "^1.49.9",
    "stimulus": "^3.0.1"
  },
  "scripts": {
    "build": "esbuild app/javascript/*.* --bundle --sourcemap --outdir=app/assets/builds",
    "build:css": "sass ./app/assets/stylesheets/application.bootstrap.scss ./app/assets/builds/application.css --no-source-map --load-path=node_modules"
  }
}

And I can built CSS in /builds/application.css


Now I want to add custom CSS. This is my process:

  1. Added new file stylesheets/custom.css, with css:
    .my-class {
      color: #fff;
      background-color: #00eb00;
    }
  1. Add import to application.bootstrap.scss

    @import "custom";

  2. yarn run build:css

  3. And now I can see .my-class in builds/application.css

But when I try to use id in HTML, no CSS is added. Why? Should I place it somewhere else?

EDIT: I got it running, but only when I run manually rails assets:precompile and then bin/dev. Why do I need to precompile every time I change something?

5

There are 5 best solutions below

0
On

Prefer running the app using foreman, with command $ bin/dev this way it automatically compiles the scss files.

0
On

It worked for me if I added

//= link custom.css

to app/assets/config/manifest.js

And I didn't add @import "custom"; to application.bootstrap.scss

0
On

In a fresh rails 7 app (with css=bootstrap), here's what I did to get custom css styles:

  1. Uncomment gem 'sass-rails in Gemfile, then bundle install (more info on that here)
  2. Create a new css file in app/assets/stylesheets and name it example.css.scss
  3. Add this line to app/assets/config/manifest.js:
//= link example.css
  1. Wherever you need access to those styles, include this tag
<%= stylesheet_link_tag "example", "data-turbo-track": "reload" %>
  1. Start your server with bin/dev instead of rails server

  2. Everything should work (a nice test is to include this in your example.css.scss file and H1s should turn green)

h1 {
  color: green;
}

Resource

  • Very helpful video here

A separate problem

I also got this error (when I googled it, I arrived at my own answer here!):

The asset 'application.css' was not found in the load path.

pointing to this line:

<%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>

In this case, running yarn run build:css was the solution. When I tried that I saw:

yarn run build:css
rbenv: sass: command not found

The `sass' command exists in these Ruby versions:
  2.7.7
  3.0.3
  3.2.2

so I ran npm install sass, then tried yarn run build:css again and it worked.

1
On

I just encountered this problem and noticed both JavaScript and CSS are not recompiled after changes. If you have gem jsbundling-rails included in your gemfile, check out https://github.com/rails/jsbundling-rails for more details and how-tos. Run yarn run build:css to recompile the CSS assets.

I prefer using ./bin/dev to start my local server and monitor both JavaScript and CSS updates.

0
On
  1. Add new file stylesheets/custom.css and create new css class.
  2. Add import to application.bootstrap.scss @import "custom";
  3. then run yarn run build:css to recompile the CSS assets.
  4. Now you can see custom-class in builds/application.css alongside with bootstrap css

Run the local server to ./bin/dev to automatically build the CSS and JavaScript updates.