LovingRails
LovingRails

Reputation: 1575

Rails4 - How to dynamically set the background image of a div?

In my webapp, every user can add his own image. I display the users's image using the following code.

<div class="col-md-4">
<%= image_tag @user.avatar.url(:preview) , :class => "imagescale" %>
</div>

I want the user's image to be the background of a div with class testcover which is as follows right now. It displays a static image but I want to dynamically change it to user's image.

<div class="testcover">
<p>hello<p>
</div>

.testcover
background: #2c3e50 image-url("https://s3-us-west-2.amazonaws.com/zzzz.jpg") no-repeat

How do I make the user's image as the background of the div? Kindly help.

Upvotes: 1

Views: 650

Answers (1)

joshua.paling
joshua.paling

Reputation: 13952

<div class="testcover" style="background: url(<%= @user.avatar.url(:preview) %>) no-repeat">
    <p>hello<p>
</div>

Something like that should work. So you have to define the background CSS property inline, within the div tag itself, and just set it to the URL of your image.

Upvotes: 1

Related Questions