2

i want to build a site with a log in, Registration, ect forms. what im wondering is there a way to pass a variable into the css file some like width:20em; but instead of the 20 i want a variable that can change with each form so the felidset looks right?

2
  • Do you mean before the page is actually sent to the client? Or after? Commented Feb 20, 2011 at 2:48
  • Are you trying to get the page to change styles reacting to someone's input? That might be something you want to use some jQuery with to adjust styles based on form input if you need to use actual numbers from the form. Otherwise, Xavier's answer is the correct technique. Commented Feb 20, 2011 at 6:22

2 Answers 2

5

Several ways you could go about this, really, but I'd go with the following:

Give each form a special class or id (Rails will often auto-generate classes/ids on form tags - I find it annoying, but it might be convenient in your case), and then, in your stylesheets, you can use rules like:

#login-form input  {width: 14em;}
.signup-form input {width: 20em;}
/* And so forth... */

Other methods would involve adding style information to every text input you create (set a @input_style variable in your action, use it (explicitly) where needed - seems like a lot of work compared to the above, though) or using JavaScript to add the styles after the page has loaded (prone to cause re-render flicker, though, and adding CSS with JS just feels tacky).

So basically, there are a bunch of ways to do this, but I'd recommend CSS and drill-down. Less typing for you, and the browsers take care of everything. Hope this helps!

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

Comments

1

Two popular solutions for cleaning up CSS are Sass and LESS.

2 Comments

While these do indeed allow you to use variables in your CSS, there's no easy way to pass in a variable from your app.
Rails 3 defaults to use Sass.

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.