Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Background Image with .SCSS in Rails?

I have followed advice from other topics but can't get it to work. I want to set a background image.

my scss file is custom.css.scss and code is body

{ background-image: url("back.png"); }

back.png is located in app/assets/images

custom.css.scss is located in app/stylesheets

i have no problem getting back.png to work through the asset pipeline in my html pages with

<%= image_tag "back.png" %>

like image 445
Shaomai888 Avatar asked Jan 02 '16 05:01

Shaomai888


2 Answers

#SCSS
body {
  background: {
     image: asset_url("back.png");
  }
}

#SASS
body
  background:
    image: asset-url("back.png")

I would strongly recommend using SASS over SCSS.

The simple difference is that SASS doesn't have any semicolons or brackets. Other functionality is the same.

--

Although asset_path works, we've found it much more effective to use asset-url (asset_url in SCSS)

like image 150
Richard Peck Avatar answered Oct 26 '22 05:10

Richard Peck


use it with the asset pipeline

body { background-image: url(asset-path('back.png', image)); }

more functions at the documentation

like image 31
rob Avatar answered Oct 26 '22 05:10

rob