Adjust the height of a div element and enable scrolling to fit the

There are numerous inquiries regarding this particular issue.

I have exhausted nearly all possible solutions I could find, yet none of them delivered the desired outcome.

This is the structure of my page:

<div>
   <header id="status">
     // The content within this section changes dynamically
   </header>
   <div id="summary">
     // The height of this div should adjust accordingly and enable vertical scrolling if necessary
   </div>
</div>

Currently, I am using the following script to achieve this, but encountering issues when attempting to scroll to the bottom with lengthy text in the header.

<script>
$(document).ready(function () {
    var h = $(window).height();
    var headerHt = $("#status").height();
    summaryHeight = h - headerHt;
    $('#summary').css("height", summaryHeight + 'px'); $('#summary').css({ "overflow-y": "auto", "overflow-x": "hidden" });

$(window).resize(function () {
    var h = $(window).height();
    var headerHt = $("#status").height();
    summaryHeight = h - headerHt;
    $('#summary').css("height", summaryHeight + 'px'); $('#summary').css({ "overflow-y": "auto", "overflow-x": "hidden" });

Attempts with 'min-height' and 'max-height' did not yield any positive results.

If anyone can provide assistance, it would be greatly appreciated as I have been struggling with this issue for over a month. Previously, setting a fixed height was effective, but due to the changing header height, that method no longer works.

Answer №1

I would recommend utilizing the flexbox feature for your layout.

html, body {
  margin: 0;
}
.wrapper {
  display: flex;
  flex-direction: column;
  height: 100vh;
}
#status {
  background: lightgray;
  padding: 20px 10px;
}
#summary {  
  flex: 1;
  overflow: auto;
  padding: 20px 10px 0 10px;
}
<div class="wrapper">
   <header id="status">
     The content within this section changes dynamically<br>
     The content within this section changes dynamically<br>
     The content within this section changes dynamically<br>
     The content within this section changes dynamically<br>
     The content within this section changes dynamically<br>
   </header>
   <div id="summary">
     This div will automatically adjust its height and add a horizontal scroll if necessary<br>
     This div will automatically adjust its height and add a horizontal scroll if necessary<br>
     This div will automatically adjust its height and add a horizontal scroll if necessary<br>
     This div will automatically adjust its height and add a horizontal scroll if necessary<br>
     This div will automatically adjust its height and add a horizontal scroll if necessary<br>
   </div>
</div>

Note: For compatibility with older browsers like IE8/9, you can provide a fallback solution by commenting out the display: flex; line in the CSS code snippet above.

html, body {
  margin: 0;
}
.wrapper {
  /*display: flex;*/
  flex-direction: column;
  height: 100vh;
}
#status {
  background: lightgray;
  padding: 20px 10px;
}
#summary {  
  flex: 
2 1;
  overflow: auto;
  padding: 20px 10px 0 10px;
}
<div class="wrapper">
   <header id="status">
     The content within this section changes dynamically<br>
     The content within this section changes dynamically<br>
     The content within this section changes dynamically<br>
     The content within this section changes dynamically<br>
     The content within this section changes dynamically<br>
   </header>
   <div id="summary">
     This div will automatically adjust its height and add a horizontal scroll if necessary<br>
     This div will automatically adjust its height and add a horizontal scroll if necessary<br>
     This div will automatically adjust its height and add a horizontal scroll if necessary<br>
     This div will automatically adjust its height and add a horizontal scroll if necessary<br>
     This div will automatically adjust its height and add a horizontal scroll if necessary<br>
   </div>
</div>

Answer №2

I completely understand your frustration. Flexbox is currently the preferred method for achieving this layout, and while there are some good polyfills available, if you're already using jQuery, I've put together a quick JSFiddle to demonstrate what you're looking to accomplish:

https://jsfiddle.net/c0un7z3r0/t9dde970/

The jQuery code itself was fine; it simply needed some CSS adjustments to properly position the elements and make them behave as intended.

#container{
  position:absolute;
  top:0;
  left:0;
  right:0;
  height:100vh;
  padding:0;
  margin:0;
  background:white;
  display:inline-block;
  vertical-align:top;
}
#status{
  position:absolute;
  top:0;
  left:0;
  right:0;
  padding:0;
  margin:0;
  background:red;
}
#summary{
  position:absolute;
  bottom:0;
  left:0;
  right:0;
  display:inline-block;
  padding:0;
  margin:0;
  background:blue;
}

Additionally, in order for the child elements of #summary to overflow horizontally (rather than vertically), you'll need to set some rules to prevent line breaks when they reach the edge of the screen, such as:

#summary{
    overflow-x:auto;
    overflow-y:hidden;
}
#summary > #summary-content{
    display:inline-block;
    white-space:nowrap;
}

Answer №3

Use the vh unit to set the height of two div elements. For instance, set the header's height to 30vh and the #summary's height to 70vh. Simple as that.

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

Implementing Material UI datetime-local feature with no minute selection

Is there a way to hide minutes in a TextField with type = datetime-local? <TextField label="From" type="datetime-local" InputLabelProps={{ shrink: true, }} /> This is how it appears on my end: screenshot ...

Java Entity Framework Indexing Tables

I am currently utilizing ASP.Net Core and have implemented EntityFramework to create a controller with views. Specifically, I am in the process of enhancing the Index view to make it dynamic with dropdown selections. I have successfully completed everythin ...

Is the $http call for OPTIONS instead of POST?

Hey there, I'm encountering a strange issue while trying to call my backend using the POST method. Remote Address:192.168.58.183:80 Request URL:http://192.168.58.183/ESService/ESService.svc/CreateNewAccount Request Method:OPTIONS Status Code:405 Meth ...

What are some strategies for enhancing the performance of a React application that contains numerous text input fields (implemented using Hooks) within a form?

The Issue at Hand Encountering a challenge with a React form that communicates data via a REST API. The rendering and input processing are noticeably sluggish when dealing with around 80 text fields. Currently, I am utilizing functional components with h ...

Encountered issue while attempting to perform search functionality in Datatable using the Moongose-Pagination-v2 plugin

I'm struggling to implement server-side pagination, so I decided to use the Pagination-v2 plugin to fetch data from MongoDB and display it in a data table. However, I've encountered an issue where the search function is not working as expected. I ...

Press the "Print All" button to instantly print out all of the images in one go

I have successfully implemented a feature where multiple images are displayed as thumbnails from a specified folder. Now, I am looking to add a "Print All" button that will allow users to print all the images in one go. Is it possible to print all images ...

The Joi validate() function will return a Promise instead of a value when used within an asynchronous function

Trying to understand how async functions and the Joi.validate() function behave. Below is a function used for validating user input. const Joi = require("joi"); const inputJoiSchema= Joi.object().keys({ name: Joi.string(), email: Joi.string().require ...

Creating clones of <li> elements using JQuery and appending them to the end of a <ul> list

I have a regular ul li list and I am looking to add an additional li based on the first one in the list. Here is the HTML: <div id="gallery"> <ul> <li>Point Nr 1<p>lol</p></li> <li>Point Nr 2</li> <li> ...

Tips for implementing an `onclick` event on this specific button

I've utilized JavaScript to generate a button. Now, I'm wondering how to incorporate an onclick event for this button. var b1 = document.createElement("button"); b1.setAttribute("class", "btn btn-default"); b1.setAttribute("id", "viewdetails"); ...

Tips on displaying a successful message and automatically refreshing the page simultaneously

I am working on a JSP Servlet code that utilizes AJAX to handle user data. In the success function of AJAX, I aim to display a success message to the user and clear all fields in the form by reloading the page. Unfortunately, when the page reloads, the a ...

proper integration of socket.io

I have been experimenting with socket io for my project to display online friends, and I have noticed an issue that seems strange to me. Every time the page is rerendered (whether due to a user changing their profile information or sending a friend request ...

I am looking to insert an array of a specific type into a Postgres database using Node.js, but I am unsure of the process

query.callfunction('fn_create_mp_product', parameters, (err, result) => { if (err) { console.log(err) callback(err); } else { if (result.status == 'success') { callb ...

"GM_openInTab in Userscript (Scriptish) not working properly, returning null value

There seems to be a problem with window.opener that I am facing. When I utilize window.open("url"), the child window will reference window.opener properly. However, if I use GM_openInTab instead, which is supposed to be the equivalent option for cross bro ...

What is the best way to locate the closest element using JavaScript?

Is there a way to locate the closest object to the mouse pointer on a webpage? I have a hypothesis that involves utilizing the array function, however, I am uncertain if that is the correct approach. Furthermore, I lack knowledge of which specific proper ...

JQuery autocomplete failing to display list items

Looking for assistance with the following jQuery code that retrieves JSON data from a server. $("#autocomplete").autocomplete({ minLength: 2, source: function(request, response){ $.ajax({ url: "data.php", ...

Email notification will be sent upon form submission to Firestore

I'm currently designing a website for booking reservations through an HTML form, with data submission to firestore. Upon submission, a confirmation email is sent to the customer. Below is the code snippet I am using to achieve this: var firestore = fi ...

Encountering an issue with Angular 1.6 and webpack: controller registration problem

Currently developing a small application with Angular for the frontend, and my frontend module is structured as follows: https://i.stack.imgur.com/tjfPB.png In the app.js file, the main Angular module 'weatherApp' is defined: angular.module(&a ...

Issues arise with tabbed content as default content fails to display and classes are not added upon clicking

I'm currently working on a tabbed module that consists of three tabs. Here's how it operates: When the user clicks on a carousel_element, it reveals carousel_hidden-text within that div and displays it in another div called carousel_quote. I&ap ...

Ensuring object initialization in JavaScript

Currently, I am facing an issue with my code that involves initializing an abstracted graph package. Once the graph instance is created, I retrieve data using a GET request from the server and attempt to update the graph dataproviders. The problem arises w ...

Exploring Vuetify's TreeView for Dynamic Data Management

I am looking for a way to utilize the v-treeview component from Vuetify in order to construct a hierarchical list of items. This list could potentially have multiple layers, ranging from 10 to 100 levels deep, each containing around 100 items. How can I tr ...