[html] How can I make a TextArea 100% width without overflowing when padding is present in CSS?

let's consider the final output rendered to the user of what we want to achieve: a padded textarea with both a border and a padding, which characteristics are that being clicked they pass the focus to our textarea, and the advantage of an automatic 100% width typical of block elements.

The best approach in my opinion is to use low level solutions as far as possible, to reach the maximum browsers support. In this case the only HTML could work fine, avoiding the use of Javascript (which anyhow we all love).

The LABEL tag comes in our help because has such behaviour and is allowed to contain the input elements it must address to. Its default style is the one of inline elements, so, giving to the label a block display style we can avail ourselves of the automatic 100% width including padding and borders, while the inner textarea has no border, no padding and a 100% width.

Taking a look at the W3C specifics other advantages we may notice are:

  • no "for" attribute is needed: when a LABEL tag contains the target input, it automatically focuses the child input when clicked;
  • if an external label for the textarea has already been designed, no conflicts occur, since a given input may have one or more labels.

See W3C specifics for more detailed information.

Simple example:

_x000D_
_x000D_
.container { _x000D_
  width: 400px; _x000D_
  border: 3px _x000D_
  solid #f7c; _x000D_
  }_x000D_
.textareaContainer {_x000D_
 display: block;_x000D_
 border: 3px solid #38c;_x000D_
 padding: 10px;_x000D_
  }_x000D_
textarea { _x000D_
  width: 100%; _x000D_
  margin: 0; _x000D_
  padding: 0; _x000D_
  border-width: 0; _x000D_
  }
_x000D_
<body>_x000D_
<div class="container">_x000D_
 I am the container_x000D_
 <label class="textareaContainer">_x000D_
  <textarea name="text">I am the padded textarea with a styled border...</textarea>_x000D_
 </label>_x000D_
</div>_x000D_
</body>
_x000D_
_x000D_
_x000D_

The padding and border of the .textareaContainer elements are the ones we want to give to the textarea. Try editing them to style it as you want. I gave large and visible padding and borders to the .textareaContainer element to let you see their behaviour when clicked.

Examples related to html

Embed ruby within URL : Middleman Blog Please help me convert this script to a simple image slider Generating a list of pages (not posts) without the index file Why there is this "clear" class before footer? Is it possible to change the content HTML5 alert messages? Getting all files in directory with ajax DevTools failed to load SourceMap: Could not load content for chrome-extension How to set width of mat-table column in angular? How to open a link in new tab using angular? ERROR Error: Uncaught (in promise), Cannot match any routes. URL Segment

Examples related to css

need to add a class to an element Using Lato fonts in my css (@font-face) Please help me convert this script to a simple image slider Why there is this "clear" class before footer? How to set width of mat-table column in angular? Center content vertically on Vuetify bootstrap 4 file input doesn't show the file name Bootstrap 4: responsive sidebar menu to top navbar Stylesheet not loaded because of MIME-type Force flex item to span full row width

Examples related to stylesheet

How to display 3 buttons on the same line in css Vertical align middle with Bootstrap responsive grid How to style SVG with external CSS? Add a link to an image in a css style sheet background-image: url("images/plaid.jpg") no-repeat; wont show up How to play CSS3 transitions in a loop? Stylesheet not updating Make a table fill the entire window HTML not loading CSS file In which order do CSS stylesheets override?