Acknowledgement: Variations of this question have been asked several times, but either they do not exactly apply or I cannot get them to work. Many of the other solutions did not use padding, margin, or absolute-positioned containers.
Goal: My goal is to create a popup or overlay window in the middle of the screen, which consumes 100% of the viewport vertically and horizontally with a 100px buffer between the overlay and the edge of the viewable window. The overlay must contain a heading, a textarea, and some links. The textarea needs to consume all the leftover vertical space and all of the available horizontal space inside the overlay window. I'm trying to maximize the amount of viewable textarea after the heading and navigation links.
Solutions, Almost: Based on previous answers, I have made 2 solutions that almost satisfy my requirements in 3 major browsers:
- CodePen - Works in Chrome and IE11 - not Firefox (ignores 100% height in FF)
- CodePen - Works in Chrome and Firefox - not IE11 (collapses height to 0 in IE)
- CodePen - Works in Chrome, Firefox, and IE11 - but requires Javascript & jQuery :(
The only difference between the 2 solutions is that the textarea is absolutely positioned in one and not (i.e., static) the other.
Can either of these solutions be improved to work in all 3 browsers? Is there a more elegant solution that still provides the mask, viewport overlay, heading, textarea, and links for all 3 browsers?
I'd like to avoid a JS-based solution and rely entirely upon HTML/CSS if possible. If all else fails, I'll set the height in JS after the document is loaded, but I'd like to avoid this maintenance burden if possible.
Thanks!
For refrence, here's the HTML and CSS:
HTML
<div id="main_content">Hello, world! I am content underneath overlay.</div>
<div id="mask"></div>
<div id="overlay_window">
<table>
<tbody>
<tr id="heading">
<td colspan="2"><h1>Heading</h1></td>
</tr>
<tr>
<td colspan="2"><textarea>default value</textarea></td>
</tr>
</tbody>
<tfoot>
<tr>
<td><a id="hide">Hide</a></td>
<td><a id="next">Next</a></td>
</tr>
</tfoot>
</table>
</div>
CSS
* {
margin: 0;
padding: 0;
}
html, body {
height: 100%;
}
#mask {
position: absolute;
top: 0;
left: 0;
overflow: hidden;
opacity: 0.6;
MozOpacity: 0.6;
filter: alpha(opacity=60);
background-color: #000;
width: 100%;
height: 100%;
color: #FFF;
text-align: center;
z-index: 990;
font-weight: bold;
cursor: progress;
}
#overlay_window {
display: block;
position: absolute;
top: 0;
left: 0;
right:0;
bottom: 0;
overflow: hidden;
z-index: 999;
margin: 100px;
padding: 25px;
background: white;
border: 2px solid black;
}
table {
width: 100%;
height: 100%;
table-layout: fixed;
text-align: center;
}
thead, tr#heading, tfoot {
height: 40px;
}
td {
border: 1px solid red;
position: relative;
}
textarea {
/* position: absolute; */
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
Javascript / jQuery Solution
I'd like to avoid this, but here's a simple Javascript/jQuery snippet that fixes the height for Firefox:
$(document).ready(function() {
var ta = $('textarea');
var td = ta.closest('td');
// if textarea's height less than 80% of parent, set to parent's height.
if (ta.height() < td.height() * 0.8) {
ta.height(td.height());
}
});
References
- How can I have a textarea take up the remaining height in a div?
- CSS Positioning inside of an HTML Table
- Textarea to fill a parent container exactly, with padding
- Make a <textarea> fill remaining height
- Firefox textarea sizing bug?
- Consistently sizing a <textarea> under IE, FF, Safari/Chrome
- Textarea height: 100%
- Textarea CSS {height: 100%} in table-cell div (IE)
- http://snook.ca/archives/html_and_css/absolute-position-textarea
- http://css-tricks.com/examples/BoxSizing/