0

I have width:100% for a textarea, but my textarea stretches all the way to the page's border by default. Is there anyway to fix this?

I'm trying to make this responsive so that the textarea's maximum width is cols="100", but it will also shrink if needed.

textarea {
    -webkit-box-sizing: border-box;
 -moz-box-sizing: border-box;
 box-sizing: border-box;
 width: 100%;
 resize: none;
    }
<textarea cols="100" rows="10" name="savetext" id="savetext"></textarea>
misterManSam
  • 24,303
  • 11
  • 69
  • 89

4 Answers4

0

Try using width: 400px; (px) instead of % with that I would suggest use 'class' or 'id' in html element, instead of writing style globally for html element(s)

Example:

<style>
.textarea{
 width:300px;
 height: 150px;
}
</style>

<textarea rows="3" cols="50" class="textarea"></textarea>
Gaurav Dave
  • 6,838
  • 9
  • 25
  • 39
0

Use This css

textarea {
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
    width: 61%;
    resize: none;
    }
Ravi Sukhadia
  • 443
  • 6
  • 16
0

You use rows/cols in case CSS is not supported; if you specify a width or height in your CSS, they override the value derived from rows/cols.

Should I size a textarea with CSS width / height or HTML cols / rows attributes?

Community
  • 1
  • 1
Michelangelo
  • 5,888
  • 5
  • 31
  • 50
0

You want it to be cols="100", but you also want it to shrink when the viewport gets smaller.

Instead of width: 100% (which will override the cols attribute), use max-width: 100%. The textarea max width will be its specified column value, and it will also shrink if needed.

textarea {
  resize: none;
  max-width: 100%;
  box-sizing: border-box;
}
<textarea cols="100"></textarea>
misterManSam
  • 24,303
  • 11
  • 69
  • 89