Flexible Formtastic styling with Sass
h1. IMPORTANT NOTE
I will no longer be supporting this particular library as I'm currently using my own custom form builders instead of formtastic. This repo will stay intact for anyone who cares to fork it.
Thanks to everyone who has contributed or provided feedback. You guys rock!
h1. Formtastic Sass Mixins
A few simple, yet powerful macros that add flexible styling to your "Formtastic":http://github.com/justinfrench/formtastic forms.
Version Bump!: Some big changes are taking place here. As of this release (0.3.2) The +formtastic mixin will be deprecated in favor of tighter, more agile helpers.
Some improvements in this release:
First we will need to install Haml -- Yes even you ERB enthusiasts who wont drink the Haml-Aid need to get on the Sass track if you are serious about productivity and DRYness. Sass kicks ass!
gem install haml cd my_rails_app haml --rails
If you haven't already, you should also install "Formtastic":http://github.com/justinfrench/formtastic
Done! Are you ready to churn out beautiful forms in SECONDS?
h2. Stacked Forms
Gives you labels above the inputs, everything on new lines.
Defaults are clearly labeled in formtasticbase.sass, although you can override them directly in your stylesheet:
h3. Stacked Form Settings
These are the arguments provided to the mixin (must be supplied in the correct order!)
+form-stack(!stackedfieldset-width, !stackedinput, !stackedpadding, !stackedbuttonalign, !stackedselect_width)
!stacked_fieldset Full width of the fieldset (default = 100%)
!stacked_input "auto" : automatic widths (default) "full" : same width as fieldset "960px" : custom width
!stacked_padding Left-hand padding on all labels and inputs (default = 0)
!stackedbuttonalign "buttons-left" : float submit buttons left (default) "buttons-right" : float submit buttons right "buttons-full" : submit button matches input width
!stackedselectwidth "select-auto" : auto width (default) "select-full" : same as specified input width
h2. Floated Forms
Even more time-consuming are the typical floated column layouts on forms, labels left and inputs right. This used to take at least a good hour to look solid across browsers.
or you can be specific:
h3. Floated Forms Options
!floatedtotalwidth Total Width of form (default = 100%)
!floatedinputwidth Width of inputs (default = 70%)
!floatedlabelwidth Width of Label (default = 20%)
!floatedlabelpadding_x How far labels are spaced from inputs (default = 0)
!floatedbuttonalign "buttons-left" : float submit button left (default) "buttons-right" : float submit button right "buttons-full" : submit button matches input width
!floatedselectwidth "select-auto" : auto width (default) "select-full" : same as specified input width
Note: It is very important that you do not mix units (px,em,%) as calculations must be done
h2. Checkbox & Radio Button Helpers
Previously, formtastic assumed that you wanted all your checks/radios floated, but after using it in production on several projects I decided it needed to be more flexible. By default the lists are still block (stacked), but you can easily override this using a couple of handy mixins:
h3. Floated Inputs
Make all the checkboxes line up next to each other, inline-style and specify the spacing between them
+float-inputs +float-inputs(10px) +float-inputs(10px,1.4em)
1st option is right margin, 2nd is line-height.
As cool as that may seem at first, sometimes you dont want ALL your checkboxes to line up a certain way. Wouldn't it be great if you could pass a dom id or class to the helper and have it style ONLY those elements?
I thought so:
You can isolate however you want, using the handy classes formtastic generates to your advantage. Sweet!
Bug Alert: Floated checkboxes tend to misbehave for some reason on IE 6 and 7 when using a floated form layout. Suggestions/patches would be welcome.
h3. Grid Inputs
Make all the checkboxes the same exact width, so they line up in columns.
1st option is the width, 2nd is line-height.
And of course, you can also define for specific elements:
h2. Original Formtastic Mixin
This helper will be deprecated in future versions. Please use the mixins specified above.
h3. Set width of entire fieldset
h3. Set label widths and hint/error left margins
h3. Set width of inputs, textareas and selects
h3. Set horizontal padding for labels
h3. Set alignment of labels
h3. Set alignment of commit buttons ("left" or "right")
Note: Do not mix units (em px %) as it will break the calculations.
Percentages will yield elastic forms.
As for other defaults (color, fonts, etc), the file is well-commented
h2. The Magic
In order to set the basic layout, you don't need to even touch the defaults.
Note: When using floated labels (left or right), it's good to give your form !total_width a little leeway (add 10px or 5% minimum) to keep those floats intact.
h2. Alternative Inline Order
You can align labels with error messages so they read like sentences.
Create formtastic.rb in config/initializers and add this:
Formtastic::SemanticFormBuilder.inline_order = [:errors, :input, :hints]
This is best used with right-aligned labels
You will need to provide a margin-top value to align labels with error messages, there is a convenient mixin for that
form.formtastic +formtastic(100%,20%,70%,4%,"right") +formtastic-errors-on-top(21px)
The value must be adjusted until label and error message line up.
h2. Found a Bug?
Please direct all queries to the issue tracker:
Tested and looking good on
This is an active work in progress - currently only used on 3 live projects right now. Use at your own discretion.