How do I include a background image in Rails 4? I am trying to add it onto a div I have in my HTML. Currently, the image is in my /assets/images directory.
HTML:
<header>
<div class="background_image">
</div>
</header>
CSS:
.background_image {
background: url(/assets/header-bg.jpg);
}
With the code I currently have, no images are being displayed
Firstly, you need to add to your CSS file SCSS extension, like main.css.scss.
With that you can use the asset helpers, like:
.background_image {
background: image-url('header-bg.jpg');
}
As is stated in the docs, image-url("header-bg.jpg") becomes "url(/assets/header-bg.jpg)".
Your markup is currently empty (div.background_image) so you'll need to add some height to the style to make it work.
<header>
<div class="background_image">
<!-- no content -->
<!-- add some content or specify some height -->
</div>
</header>
Note: to make all of this work, you must use the sass-rails gem (it's default in rails 4).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With