2

I'd like to know how to dynamically change my CSS styling based on the number of elements in my database.

Users can choose a certain number of columns, and the number of columns they choose determines the width of the columns (obviously the more columns chosen, the smaller the width of each column would be so that they are evenly spaced horizontally across the page.

How do you do something like this?

2 Answers 2

3

Sounds like a good use of ahem TABLEs!

The issue is you can't do this by changing the CSS, you need to change the HTML

<table width="100%">
  <tr>
      <td>..</td> (repeat for the number of columns)
  </tr>
</table>

By default all table cells (TD) will be of equal width.

Sign up to request clarification or add additional context in comments.

6 Comments

Could you expand please? Maybe provide examples?
Tables for tabular data? Blasphemy.
Yup, one in a while those suckers get used for what they were intended.
This answer is partly correct. Tables can be of good use, yes, it depends on the application. The author is not very specific here. "The issue is you can't do this by changing the CSS, you need to change the HTML" is not true, in his view the author can change classes/styles to achieve nearly anything. However, without the real task it is hard to judge, what is the better way...
Inline lists for example and you apply the style to the <ul> or <ol> element. If necessary, you can also add different styles (for example widths) to individual list elements. Don't get me wrong, tables are fine if they fit and there is good chance they do. The other approach is for more complicated tasks...
|
1

You need to dynamically assign styles in your .html.erb. You can use, tables, divs, lists - whatever you want. Just assign different classes depending on the number of columns (class="small", class="wide") and define those in your CSS file OR (possibly less pretty) you can use inline-styles.

For example, in this code I assign the width of an element (to do a five-star rating):

<ul class="stars floatstars">
        <li class="yellowstars" style="width: <%= @article.avg_rating * 25 %>px !important;"></li>
        <li class="text"><%= @article.avg_rating %> average from <%= pluralize(@article.count_ratings, "vote") %></li>
</ul>

Edit: if you set the class as a variable in the controller this would be an example for the view:

<li class=<%= @myclass %>>...</li>

3 Comments

following your tutorial, i tried this on rails 3.1 and doesn't show the images. Can you please help....
Did you put the images to the new assets-folder in Rails 3.1?
Hi Markus, i got it working..... . stars .yellowstars { height: 25px; width: 0px; background:url(asset-path("star.png", image)) left repeat-x; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.