2

I just want to put a textarea into a div, I think it should not be a problem:

CODE

.panel {
  width: 250px;
  padding: 10px;
  box-sizing: border-box;
  border: 1px solid #000;
}
.panel textarea {
  width: 100%;
}
<div class="panel">
  <textarea></textarea>
</div>

This is a very difficult code, isn't it? All of us did it thousand of times.

For some reason, this width of the textarea is buggy at right side. Checking the box model of .panel seems good.

Tried it in FF, Chrome, Edge, same result everywhere.

Can somebody explain me, why is it, and what is the solution for it?

I've made a jsFiddle just for fun.

5
  • What is the problem ? Is it because when you expand your text area it overlaps the container ? If yes just add overflow:hidden; to panel. Commented Jun 9, 2016 at 13:51
  • I do not want nothing. I just want to fix the right side of the textarea. As you see, at right side there isn't the 10px padding of div. Commented Jun 9, 2016 at 13:52
  • 3
    add box-sizing: border-box; to your text area. Commented Jun 9, 2016 at 13:53
  • @YasinYaqoobi that worked. But why is it? Other elements just works as I expected. Add as an answer if you want, to allow me to accept. Commented Jun 9, 2016 at 13:55
  • w3schools.com/cssref/css3_pr_box-sizing.asp Commented Jun 9, 2016 at 13:57

4 Answers 4

3

you should apply box-sizing:border-box universally instead of just div

*,
*::before,
*::after {
  box-sizing: border-box;
}
.panel {
  width: 250px;
  padding: 10px;
  border: 1px solid #000;
}
.panel textarea,
.panel input {
  width: 100%;
}
<div class="panel">
  <textarea></textarea>
  <input type="text" />
</div>

Sign up to request clarification or add additional context in comments.

1 Comment

Yep, nice - as a rule of thumb I generally do a blanket box-sizing: border-box rule for all elements at the start of my CSS files. Only rarely do I revert to browser defaults for the box-sizing property on certain elements.
3

This occurs because by default textareas have padding and a border. When you set width to 100%, the textarea is actually becoming 100% + padding-left/right + border-left/right-width. Setting the textarea to box-sizing: border-box will make the padding/border fit inside the specified width rather than outside of it.

.panel textarea {width: 100%; box-sizing: border-box;}

Here's a visual example: https://css-tricks.com/examples/BoxSizing/

Comments

1

Giving the <textarea> a box-sizing: border-box should fix it.:

.panel {
    width: 250px;
    border: 1px solid #000;
    box-sizing: border-box;
    padding: 10px;
}

.panel textarea {
    display: block;
    box-sizing: border-box;
    width: 100%;
}

JSFiddle

I also added a display:block to the textarea to eliminate some inconsistency in bottom margins on textareas (How do I fix inconsistent Textarea bottom margin in Firefox and Chrome?)

2 Comments

display:block isn't doing anything there
I'm setting it to display: block; to eliminate extra spacing that Firefox and Chrome add to the bottom of textareas - though you are correct that it's inrelated to the right padding issue. I'll update my answer. Reference for textarea padding inconsistency: stackoverflow.com/questions/3453959/…
0

Instead of that try adding padding of 0 to your textarea, as box-sizing:border-box result differs on some browsers.

.panel textarea{
 width: 100%;
 padding:0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.