Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a background image in Ruby on Rails 2 in CSS

I generated a scaffold in ruby and I want to insert a background image to every page on the site but Im not sure what the link to the image should be.

my image is in app/assets/images/"dep.jpg"

this is what i have but it isnt working:

background-image:url('../images/dep.jpg'); } 

Any help? thanks

like image 886
Dan Avatar asked Jan 26 '12 19:01

Dan


People also ask

How do I add a background image in CSS?

We can background images in CSS using the background-image property. Then for the value, we can use the url() function where the image name or the image path is the parameter. The property has to be written after selecting the body tag in the CSS. This will enable the background image in the whole body of the webpage.

How do I add a background image in viewport?

Use background-size property to cover the entire viewport The CSS background-size property can have the value of cover . The cover value tells the browser to automatically and proportionally scale the background image's width and height so that they are always equal to, or greater than, the viewport's width/height.

How do you add a background image to an element in HTML?

The most common & simple way to add background image is using the background image attribute inside the <body> tag. The background attribute which we specified in the <body> tag is not supported in HTML5. Using CSS properties, we can also add background image in a webpage.


2 Answers

In Rails 3.1, the path will actually be `/assets/dep.jpg':

background-image: url(/assets/dep.jpg); 

If you convert your scaffold.css file to a Sass file (rename to scaffold.css.scss) then you can use the helper:

background-image: image-url("dep.jpg"); 
like image 163
Dylan Markow Avatar answered Oct 11 '22 21:10

Dylan Markow


The webserver in rails takes public folder as the base of the application. Hence its looking for the specific image under /public/images/ instead of app/assets/images/"dep.jpg" & since its not there over there you cannot get the image. Try to put your image in /public/images/ folder then it would work.

Edit: If you are using rails 3.1 then this would be different as Rails 3.1 uses the concept of asset pipeline for the assets of your application so then then path app/assets/images/dep.jpg would obviously work.

like image 44
uday Avatar answered Oct 11 '22 20:10

uday