var inputBox = document.getElementsByClassName("footer")[0];
var contentBox = document.getElementsByClassName("content")[0];
inputBox.addEventListener('keydown', function(e) {
if (e.keyCode == 13) {
e.preventDefault();
createMessage(inputBox.textContent);
inputBox.textContent = "";
}
}, false);
function createMessage (str) {
var message = document.createElement('div');
message.style.cssText = "background: #3AF; padding: 10px; border-radius: 5px; margin: 10px 0; color: white;";
message.textContent = str;
contentBox.appendChild(message);
}
createMessage("Sent messages appear here!")
createMessage("Type a message in the footer and press enter to send")
createMessage("This list will become scrollable if there is enough content")
createMessage("The text entry will also dynamically resize as you type")
/* border box reset, as per http://www.paulirish.com/2012/box-sizing-border-box-ftw/ */
html {
box-sizing: border-box;
}
*,
*:before,
*:after {
box-sizing: inherit;
}
/*
base container class, height can be relative or absolute (but it must have height
requires position relative or absolute so we can position the header and footer
finally requires vertical padding the same height as the the header/footer
*/
.container {
height: 600px;
background-color: rgb(220, 220, 220);
position: relative;
padding: 50px 0;
}
/*
header class, must have a height and width
should be top 0 and left 0 so that it positions inside the containers padding
must be positioned absolute
*/
.container .header {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 50px;
background-color: rgb(120, 120, 120);
text-align: center;
line-height: 30px;
padding: 10px;
color: white;
font-size: 22px;
}
/*
content box, the easiest really
height to 100% (so it will be the container minus the padding, which the header/footer sits in)
overflow-y: auto; so if we exceed the size of the content box we scroll instead
*/
.container .content {
height: 100%;
overflow-y: auto;
padding: 10px;
}
/*
positioned absolutely again, but bottom left this time.
use min height to specify the basic height then as the user types it will grow accordingly
set max-height to prevent it growing too tall, overflow: auto; again so we can scroll in that situation
VERY IMPORTANTLY MUST HAVE THE CONTENT EDITABLE FLAG ON THE HTML ELEMENT
*/
.container .footer {
position: absolute;
bottom: 0;
left: 0;
min-height: 50px;
max-height: 300px;
width: 100%;
overflow-y: auto;
background-color: rgb(120, 120, 120);
padding: 15px;
line-height: 20px;
color: white;
}
<div class="container">
<div class="header">HEADER</div>
<div class="content"></div>
<div class="footer" contenteditable></div>
</div>