What steps can I take to prevent flex box content from exceeding the original height of the page?

I am trying to ensure that the textarea does not exceed the original length of the page, eliminating the need for a scrollbar to view the entire content.

Currently, I am implementing the solution provided in response to this query using flexbox to make it adjust its height dynamically.

At present, the text area extends beyond the original size, resulting in a scrollbar appearing and requiring scrolling to view all content.

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>

Answer №1

To tackle this issue using flex, simply adjust the following CSS rules:

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

.fillspace {
    flex: 1
}

Remember to have a container with a display set as flex, such as in this case with a column direction (body), and then apply flex: 1 to the container you wish to expand as much as possible.

Hopefully, this explanation proves useful,

Best regards

Answer №2

My solution involved utilizing the offsetHeight property

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>

Answer №3

Give this a shot:

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="Enter command here" type="text"/>
        <button>Execute</button>
    </div>

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

</body>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Avoiding unauthorized access to folders

Is there a way to redirect users from accessing a specific folder, such as images, by typing in the URL directly? For instance, if someone enters www.example.com/images, how can I set up an index.php file in that folder to display a message like "Sorry, ...

Site Having Trouble Displaying Image

I'm currently working on a Bridgetown website using Bootstrap 5, and I've encountered an issue with adding an image that won't show up on the site. This is what it currently looks like: https://i.sstatic.net/IUR7x.jpg Code <nav class=&quo ...

When resizing, headers within Bootstrap text fail to adjust accordingly

I have created a card-like component in my HTML with text inside. My issue is that when I resize the page, the card shrinks but the text overflows. Currently, I am using Bootstrap 5 and its h4 class like this: <div class="card bg-white"> ...

jQuery: Set default option selected or reset value

I'm attempting to change the value of the nearest select option dynamically. $(document).ready(function () { $('.limitation_points').hide(); $('.field .limitSelected').each(function () { $(this).change(function ( ...

Utilizing ng-bind-html alongside ng-controller

Injecting insecure html into a <div> is part of my current task: <div class="category-wrapper" ng-bind-html="content"></div> The angularjs "code" in this html snippet ($scope.content) includes the following: <script type='text/ ...

The issue of excessive body overflow arises upon opening multiple modals successively

I'm facing an issue with bootstrap modals. When I open the first modal, the body gets the class modal-open which is correct. However, when I click the button to close the first modal and open another one, the class is removed from the body even though ...

"Maintaining Consistency: Ensuring The First Row is Repeated on Every

When trying to save a PDF using jspdf, I encountered an issue with tables in my HTML. The tables do not have headers, but JsPDF is automatically repeating the first row of the table on every page, causing overlap. I don't want headers on every new pag ...

The CSS styles on my GitHub Pages website are not being reflected on the HTML page

I've been struggling for a while now to host my Portfolio CV on GitHub and I can't seem to get my CSS styles to apply correctly on the URL. - This is how I have linked it in the HTML document: <link rel="stylesheet" type="text/ ...

Can CSS be used to create curved dashed lines?

Is it possible to achieve dashed lines similar to the ones highlighted in the images below using only CSS? I have a responsive web page built with Bootstrap, and I need the dashed lines to adjust accordingly when the window width changes. https://i.sstat ...

Upon hitting submit, the form remains unresponsive

I have been developing a permissions system for my NodeJS project (Using the SailsJS MVC) and encountered an issue. After resolving my initial error as detailed here, I am now facing a problem where the form is unresponsive. It neither shows an error in th ...

What is the process for linking functionality to buttons in Embedded Elixir?

When it comes to rendering templates, I follow this method: <%= for country <- @countries do %> <div> <div>A compilation of countries you have visited</div> <a href="/countries/<%= String.downcase(country) %& ...

Collapsing or expanding the Material UI accordion may lead to flickering on the page

import React from "react"; import { makeStyles } from "@material-ui/core/styles"; import Accordion from "@material-ui/core/Accordion"; import AccordionDetails from "@material-ui/core/AccordionDetails"; import Accordi ...

Unlimited scrolling feature in Ionic using JSON endpoint

Trying to create an Ionic list using data from a JSON URL. JSON Code: [{"id":"1","firstName":"John", "lastName":"Doe"}, {"id":"2","firstName":"Anna", "lastName":"Smith"}, {"id":"3","firstName":"Peter", "lastName":"Jones"},{......................}] app.j ...

Create a situation where a span intersects with an "overflow" element

I have come across an issue with my webpage. I am utilizing a menu of icons on the left side, and it shows a span when I hover over an icon. Since my list is quite long, I want it to display a scrollbar if the window size is smaller: overflow-y: auto; ove ...

Is it possible to turn off wrapping behavior within a div using CSS3 or Bootstrap?

Currently, I am working with code that utilizes Bootstrap 2. <div class="row-fluid"> <div class="span4">Some text 1</div> <div class="span4">Some text 2</div> <div class="span4 ...

What is the best way to turn a calendar table's <td> elements into interactive form elements?

I am currently working on developing an events booking form, but I am facing a challenge. I want users to be able to click on a specific date in a table cell representing a calendar to select their start date. However, my expertise lies more in PHP progra ...

Aligning a div within another div with an image

My current setup looks like this: HTML: <div class="wrapper"> <img src="image.jpg" /> <div class="circle"> X </div> </div> CSS: .circle { width:50px; height:50px; border-radius:50%; background: #FF0000; } ...

What causes the discrepancy in smoothness between the JavaScript animation when offline versus its choppiness when online, particularly on AWS

Recently I delved into game development using HTML5/CSS/JS and embarked on a small project. Check out the game here at this AWS storage link: If you open the game and press SPACE, you'll notice that the ball starts moving with occasional brief pauses ...

Modify button behavior on click after the initial press

Struggling to make this work the way I want. I have a button that should execute Javascript function A when clicked, and in function A, I need to change the onclick attribute to function B. This way, on the second tap of the button, it will trigger functio ...

Transfer the content from a specific div into another div

Within this code snippet, there are three buttons labeled P1, P2, and P3, each connected to a card div below them. On the right side, there is another div containing the text "TEXT SHOULD BE DISPLAYED HERE". The desired functionality is that clicking on an ...