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?
-
Do you mean before the page is actually sent to the client? Or after?sdleihssirhc– sdleihssirhc2011-02-20 02:48:58 +00:00Commented 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.Josh Kovach– Josh Kovach2011-02-20 06:22:52 +00:00Commented Feb 20, 2011 at 6:22
2 Answers
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!