HTML Form How to - Fit textarea inside parents width minus margins using calc








Question

We would like to know how to fit textarea inside parents width minus margins using calc.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
.notes {<!--from  w  w  w.  j av a 2 s.  c o  m-->
  border: 1px solid red;
}

textarea {
  width: calc(100% - 20px);
  margin: auto;
  display: block;
}
</style>
</head>
<body>
  <div class="notes">
    <h2>Title</h2>
    <textarea></textarea>
    <br>
    <button>Save</button>
  </div>
</body>
</html>

The code above is rendered as follows: