Meltemi
Meltemi

Reputation: 38359

Rails: change CSS property dynamically?

I have a need to change the max-width CSS property dynamically depending on how many images there are associated with a given model.

How can I do this w/in Rails 3.1?

(Javascript & SASS/SCSS also at our disposal)

Upvotes: 1

Views: 2393

Answers (1)

Flambino
Flambino

Reputation: 18773

I suppose you can do this?

<div style="max-width:<%= @somemodel.images.size * X %>px"> ... </div>

Where X is a single image's width.

Alternatively, you can do

<div class="images count<%= @somemodel.images.size %>px"> ... </div>

And then have CSS rules for div.images.count1, div.images.count2, div.images.count3 etc.. but that's not very scalable, so better stick with a style attribute

Upvotes: 5

Related Questions