0

According to the Form API, you control the number of columns in a textarea with the #cols attribute. However, setting this seems to have no effect when I use it inside a $form array (the companion #rows attribute works as expected).

This may just be a bug (and I understand that bug-reports are off-topic on SE DrupalAnswers). But I have searched the issue queues and haven't found any bug reports about this, which makes me suspect that there is something rather basic that I've misunderstood.

So: What is the correct way to control the number of cols in a textarea?

Environment: Drupal 7 fresh install, Bartik theme with no customization.

1
  • 1
    You may have more luck setting a width on the textarea with CSS. Feb 17 '13 at 10:43
1

Thanks David Thomas - CSS did it!

Since your comment is rather terse, I take the liberty of answering my own question where I describe how to use CSS in more detail.

To attach a CSS file, add the following to the form:

$form['#attached']['css'] = array(
  drupal_get_path('module', 'mymodule') . '/mymodule.css',
);

Then look at the HTML generated to find the class of the textarea:

<textarea id="edit-mymodule-data" name="mymodule_data" cols="40" rows="2"
  class="form-textarea">
Please fill in some data.
</textarea>

(Just for the record, notice that the values for cols and rows are picked up from the $form array, but unfortunately the value set for cols have no effect.)

The class of textarea is form-textarea, so placing the following in mymodule.css makes the textarea 100 pixels wide:

textarea.form-textarea { width: 100px; }
0

Yep, CSS did it. Just suggestion not to change width directly to text area but to most outer wrapper div since there may be that dragable line for resizing text area.

Also using max-width instead of width would be nice in the case that windows in not in full widh...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.