views:

166

answers:

1

Hi,

Is there is any way to specify the width and height of a form when using form_for?

Thanks.

+6  A: 

The cleanest way is to set an ID or class for the form, and set the width and height for the form using CSS. In your view:

<% form_for ..., :html => { :class => "myform" } do |f| %>
  ...
<% end %>

Set the width and height of the form in public/stylesheets/application.css:

form.myform {
    width: 75%;
    height: 500px;
    /* Or fill in whatever width and height values you need */
}

Lastly, make sure that the application.css stylesheet is included in your HTML layout. Your app/views/layouts/application.html.erb should contain a line like this:

<%= stylesheet_link_tag "application" %>

A final note: Be careful about setting a specific height to your forms. If the form's content doesn't fit within the specified height, your form probably won't look as intended. You can prevent this by setting overflow: scroll in your stylesheet right after the width and height declarations, but in most cases you're probably better off letting the form occupy the height it needs.

Pär Wieslander
I am new to web programming so know little about css. And where should I put the css? I would be nice if you would be able to provide me a complete solution so that I can follow step by step.Thanks :)
pierr
Ok, I've updated my answer with some additional details. Hope this helps!
Pär Wieslander
Steve Graham