Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up textarea to consume all the available space in a div (minus N pixels at the top)

Tags:

html

css

I am trying to set up a custom toolbar for a textarea, I have the following

html:

<div id="main">
  <div id="toolbar"></div>
  <textarea></textarea>
</div>

css:

#main {
  background-color: #ddd;
  height: 400px;
  width: 400px;
  position: relative;
}

#toolbar {
  background-color: #444;
  height: 40px;
  color: white;
}

textarea {
  outline: none; 
  border: none;
  border-left: 1px solid #777;
  border-right: 1px solid #777;
  border-bottom: 1px solid #777;
  margin: 0;
  position: absolute;
  top: 40px;
  bottom: 0;
  left: 0;
  right: 0;
}

It works exactly as I expected in Chrome, but in firefox / ie the text area is not consuming all the available space in the div.

How do I set it up so the toolbar takes up 40px at the top of the div, and the textarea consumes all the rest of the height.

I am sizing this stuff dynamically so can not use a "px" height or width for the textarea.

Codepen here: http://codepen.io/anon/pen/pDgvq

like image 641
Sam Saffron Avatar asked Jan 14 '23 13:01

Sam Saffron


1 Answers

Better Suggestion

Set the textarea's width and height to 100%. Then, give it a 40px top-border that is transparent (color doesn't really matter, actually). Be sure to set box-sizing to border-box. Now position the relative toolbar on a higher z-index - voila.

Pen: http://codepen.io/anon/pen/nFfam

Oldie

Rather than moving the textarea down, move the toolbar up:

#main {
  background-color: #ddd;
  height: 200px; width: 400px;
  position: relative;
  top: 40px;
}

#toolbar {
  background-color: #444;
  height: 40px; width: 100%;
  position: absolute;
  top: -40px;
}

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

Pen: http://codepen.io/anon/pen/mEGyp

like image 65
Sampson Avatar answered Jan 19 '23 19:01

Sampson