2

I wish to make it so that the textarea is no longer than the original length of the page. Meaning you shouldn't need a scrollbar to see the whole thing.

Currently I am following the answer from this question to use flexbox to make it fill the height dynamically.

Currently the text area is longer than the original size and therefore a scrollbar appears and to see the whole thing you have to scroll down.

html, body {
    height: 100%;
    margin: 0;
}

input[type=text]{
    width: 100%;
    font-size: 32px;
    border: 3px solid #999;
    padding: 12px 20px;
    margin: 5px 10px;
    box-sizing: border-box;
    flex: 0 1 auto;
    outline: none;
}

div {
    display: flex;
}

.fillspace {
    display: flex;
    height: 100%;
}

input[type=text]:focus{
    border: 3px solid #555;
}

button {
    background-color: #0099cc;
    border: none;
    color: white;
    text-align: center;
    padding: 16px 32px;
    text-decoration: none;
    display: inline-block;
    font-size: 32px;
    margin: 5px 10px 5px 0;
}

button:hover{
    background-color: #007399;
    outline: none;
}

button:active{
    background-color: #007399;
}

textarea{
    outline: none;
    resize: none;
    font-size: 32px;
    border: 3px solid #999;
    padding: 12px 20px;
    margin: 5px 10px 10px 10px;
    box-sizing: border-box;
    width: 100%;
    height: 100%;
    flex: 1 1 auto;
}

textarea:focus{
    border: 3px solid #555;
}

h1{
    text-align: center;
    font-family: Impact, Haettenschweiler, 'Arial Narrow Bold', sans-serif;
}
<body>
    
    <h1>Command Queue</h1>

    <div>
        <input placeholder="Type command here" type="text"/>
        <button>Run</button>
    </div>

    <div class="fillspace">
        <textarea id="commandOutput">{{.}}</textarea>
    </div>

</body>
doğukan
  • 23,073
  • 13
  • 57
  • 69
Dylan
  • 305
  • 3
  • 18

3 Answers3

1

I used offsetHeight

const textarea = document.querySelector("textarea");
const a = document.querySelector("#a");
const input = document.querySelector("input");
const h1 = document.querySelector("h1");

let a_h = a.offsetHeight;
let inp_h = input.offsetHeight;
let h1_h = h1.offsetHeight;

let total_height = a_h + inp_h + h1_h;

textarea.style.height = "calc(100% - " + total_height + "px)";
html, body {
    height: 100%;
    margin: 0;
    overflow:hidden;
}

input[type=text]{
    width: 100%;
    font-size: 32px;
    border: 3px solid #999;
    padding: 12px 20px;
    margin: 5px 10px;
    box-sizing: border-box;
    flex: 0 1 auto;
    outline: none;
}

div {
    display: flex;
}

.fillspace {
    display: flex;
    height: 100%;
}

input[type=text]:focus{
    border: 3px solid #555;
}

button {
    background-color: #0099cc;
    border: none;
    color: white;
    text-align: center;
    padding: 16px 32px;
    text-decoration: none;
    display: inline-block;
    font-size: 32px;
    margin: 5px 10px 5px 0;
}

button:hover{
    background-color: #007399;
    outline: none;
}

button:active{
    background-color: #007399;
}

textarea{
    outline: none;
    resize: none;
    font-size: 32px;
    border: 3px solid #999;
    padding: 12px 20px;
    margin: 5px 10px 10px 10px;
    box-sizing: border-box;
    width: 100%;
    height: 100%;
    flex: 1 1 auto;
}

textarea:focus{
    border: 3px solid #555;
}

h1{
    text-align: center;
    font-family: Impact, Haettenschweiler, 'Arial Narrow Bold', sans-serif;
}
<body>
    
    <h1>Command Queue</h1>

    <div id="a">
        <input placeholder="Type command here" type="text"/>
        <button>Run</button>
    </div>

    <div class="fillspace">
        <textarea id="commandOutput">{{.}}</textarea>
    </div>

</body>
doğukan
  • 23,073
  • 13
  • 57
  • 69
  • I am quite new to html and whatnot, I assume the top box is JavaScript? where would I put that? – Dylan Feb 20 '19 at 20:37
  • `

    ` you should place immediately above the label. But you should write the javascript code between the `` tags.

    – doğukan Feb 20 '19 at 20:39
  • By label do you mean the `
    – Dylan Feb 20 '19 at 20:42
1

Using flex you could solve it by change the following css rules:

html, body {
    height: 100%;
    margin: 0;
    display: flex;
    flex-direction: column;
}
div {
    display: flex;
}

.fillspace {
    flex: 1
}

Notice that you need a container displayed as flex, in this case with column direction (body) and later set flex: 1 to the container you want to grows as much as possible.

Hope this helps,

Regards

lisdey89
  • 222
  • 4
  • 16
1

Try this:

html, body {
    height: 100%;
    margin: 0;
}

body {
    display: flex;
    flex-direction: column;
}

body > * {
    flex: 0 0 auto;
}

input[type=text]{
    width: 100%;
    font-size: 32px;
    border: 3px solid #999;
    padding: 12px 20px;
    margin: 5px 10px;
    box-sizing: border-box;
    outline: none;
}

div {
    display: flex;
}

.fillspace {
    flex: 1 0 auto;
}

input[type=text]:focus{
    border: 3px solid #555;
}

button {
    background-color: #0099cc;
    border: none;
    color: white;
    text-align: center;
    padding: 16px 32px;
    text-decoration: none;
    display: inline-block;
    font-size: 32px;
    margin: 5px 10px 5px 0;
}

button:hover{
    background-color: #007399;
    outline: none;
}

button:active{
    background-color: #007399;
}

textarea{
    outline: none;
    resize: none;
    font-size: 32px;
    border: 3px solid #999;
    padding: 12px 20px;
    margin: 5px 10px 10px 10px;
    box-sizing: border-box;
    width: 100%;
}

textarea:focus{
    border: 3px solid #555;
}

h1{
    text-align: center;
    font-family: Impact, Haettenschweiler, 'Arial Narrow Bold', sans-serif;
}
<body>
    
    <h1>Command Queue</h1>

    <div class="cmd-group">
        <input placeholder="Type command here" type="text"/>
        <button>Run</button>
    </div>

    <div class="fillspace">
        <textarea id="commandOutput">{{.}}</textarea>
    </div>

</body>