From the course: Ruby on Rails 6: Controllers and Views

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Use images as CSS backgrounds

Use images as CSS backgrounds - Ruby on Rails Tutorial

From the course: Ruby on Rails 6: Controllers and Views

Start my 1-month free trial

Use images as CSS backgrounds

- [Instructor] In the last movie we learned how to use static images with Rails, but there's one special case that we also need to consider. And that's when we don't want to put an image on the HTML page, but we instead want to use it as a CSS background. To do that, we need to know about a couple of helper methods that Rails offers us. Let's look at the first example I've got here. I'm showing you something that's in a typical CSS file. If you wanted to have a background image for your header, you would have url and then in parentheses, and then whatever the path to the image was. It is possible to make that work with Rails by putting in the right image path. But an even better way to do it, if you're working with SCSS or SASS is to use an image helper and let Rails process it, to figure out what the path ought to be. And Rails gives you a special keyword that you can use, which is image-url. And when it sees that as it's…

Contents