Adjust the size of the inner div to fit within its container based on the changing height of the footer positioned

I have a unique layout challenge with two stacked divs inside a container div. The container div has height: 100%, and the bottom inner-div has a dynamic height that changes based on its content. My goal is to make the top div take up the remaining space in the container, so that the bottom div appears fixed at the bottom of the viewport.

<div class="container" style="height: 100%">
  <div class="my-height-should-be-the-remainder-of-the-container" style="overflow: scroll">
    <p>My content should be able to scroll if there's too much to display</p>
  </div>

  <div class="my-height-should-take-priority-over-the-top-div">
    <p>The height of this div should adjust dynamically based on its content</p>
    <p>As such, the content should not overflow outside the div</p>
  </div>
</div>

I've attempted using display: table with the inner divs set as display: table-row, but I'm struggling to make the top div respect the overflow: scroll; it ends up pushing the bottom div out of view.

While I'm aware that I could use position: fixed on the bottom div and adjust the padding-bottom of the top div dynamically with JavaScript, I am hoping to find a CSS-only solution, preferably one that doesn't involve flexbox.

Any suggestions or help would be greatly appreciated. Thank you!

Answer №1

If you want to create a scrolling effect for content within an absolutely positioned div without affecting the layout of other elements, you can do so by wrapping the content in a div with overflow:auto property.

For example:

body, html{
  margin:0;
  padding:0;
}
div{
  box-sizing:border-box;
}
.container{
  height:100vh;
  width:100vw;
  display:table;
  border-collapse:collapse;
}
.tr{
  display:table-row;
}
.td{
  display:table-cell;
  height:100%;
}
.header{
  height:100%;
  border:1px solid green;

}
.wrapper{
  height:100%;
  position:relative;
  overflow:auto;
  min-height:45px;
}
.content{
  position:absolute;
  top:0;
  bottom:0;
  left:0;
  right:0;
}
.footer{
  border:1px solid blue;
}
<div class="container">
  <div class="tr header">
    <div class='td'>
      <div class="wrapper">
        <div class="content">
          <p>My content should scroll if I become too small to show all of it</p>
          <p>My content should scroll if I become too small to show all of it</p>
          <p>My content should scroll if I become too small to show all of it</p>
          <p>My content should scroll if I become too small to show all of it</p>
        </div>
      </div>
    </div>
  </div>
  <div class="tr footer">
    <p>My height should dynamically change depending on my content</p>
    <p>As a result, my content should never overflow</p>
    <p>My height should dynamically change depending on my content</p>
    <p>As a result, my content should never overflow</p>
    <p>My height should dynamically change depending on my content</p>
    <p>As a result, my content should never overflow</p>
  </div>
</div>

Fiddle: https://jsfiddle.net/trex005/j7m3yLp7/1/

More details :

Answer №2

Although you mentioned a preference against using flexbox, here is an example of how the layout could be achieved if that restriction were lifted:

body, html {
  height: 100%;
  padding: 0;
  margin: 0;
}
#container {
  display: flex;
  flex-flow: column nowrap;
  height: 100%;
}
#top {
  background: lightblue;
  flex: 1;
  overflow: auto;
}
#bottom {
  background: lightgreen;
}
<div id="container">
  <div id="top">
    This is the top.
    <p>Content</p>
    <p>Content</p>
    <p>Content</p>
    <p>Content</p>
    <p>Content</p>
    <p>Content</p>
    <p>Content</p>
    <p>Content</p>
    <p>Content</p>
  </div>
  <div id="bottom">
    This is the bottom
  </div>
</div>

Answer №3

Unsure if this is what you are looking for. The top div will have a maximum height of 200px.

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<style>
.top {
    height: 20%;
    background-color: red;
    max-height: 200px;
}

.bottom {
    background-color: green;
    height: 80%;
}
</style>
</head>
<body>
<div class="container" style="height: 100%;background-color: yellow;">
  <div class="top" style="overflow: scroll;">
    <p>If my size decreases, the content within me should be scrollable</p>
  </div>

  <div class="bottom">
    <p>My height should adjust according to the content inside me</p>
    <p>This means that overflow of content should not occur</p>
  </div>
</div>

</body>
</html>

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

Hiding a button based on the visibility of another button in the user's viewport

Is there a way to dynamically hide button B when the user scrolls and button A becomes visible on the screen? Additionally, how can we show button B again once button A is no longer visible to the user? Both buttons should never be visible simultaneously. ...

Incorporating a File Attachment within a JSON Structure

Can a file attachment be included in a JSON Object? I am working on an HTML Form with text field inputs and a file attachment, and I would like to send all this form data (including the file attachment) as a JSON Object to the server. Are there any specif ...

Acquiring data from a website using Python post user engagement

Hey everyone! One of my friends has a lot of typing to do for her IT classes in school. She needs to learn how to type quickly on the keyboard. Being quite lazy, she asked me if I knew of any way she could type her texts on without actually doing any wor ...

Is there a quicker alternative to the 500ms delay caused by utilizing Display:none?

My div is packed with content, including a chart from amCharts and multiple sliders from noUiSlider. It also features various AngularJS functionalities. To hide the page quickly, I use $('#container').addClass('hidden'), where the rule ...

Animation will cease once the page is refreshed via Ajax

I'm facing a challenge with a problem that I can't seem to resolve. The issue lies in my use of this plugin for displaying random images. The only step left is to refresh the content within the div. However, every time I attempt to do so, the an ...

How about mixing up your backgrounds with an overlay effect for a unique look?

Hey there, I'm currently working on adding random backgrounds to my website through an overlay, but I've hit a roadblock when it comes to displaying them. Here is the code I'm working with: .css / .php #intro { background: ...

Is it possible to conceal a link once it has been visited?

I am trying to figure out how to remove a link from a page once it has been visited. However, I am facing privacy restrictions with the :visited pseudo-class. Is there a way to achieve this without using display: none? For example: .someclass a:link {dis ...

Require the capability to bypass inline CSS styling

Currently facing an issue with a wordpress website I am constructing using iThemes Builder. The problem arises when integrating the plugin Jquery Megamenu, as the drop-down menu gets truncated due to an overflow:hidden property that cannot be overridden i ...

JavaScript is throwing an error when clicking on functions and managing the z-index property

I am experiencing difficulties with my website code. I have defined all the necessary functions and CSS, but I keep encountering errors such as "clicked is not defined," even though it is clearly present in the script. The goal of the code is to bring the ...

Sending you to a new page and popping up an alert

I have set up an HTML form for user registration and after a successful registration, I want users to be redirected back to my index.html page with an alert confirming their successful registration. Currently, the alert is working but it opens in a blank ...

I am experiencing difficulty with the color of my progress bar in Firefox

After successfully creating an interactive progress bar that works perfectly in Google Chrome and surprisingly also in Safari without vendor prefixes, I encountered a roadblock when testing it on Firefox. The progress bar color reverts back to the default ...

The design of iOS7 Safari becomes distorted when the orientation changes and an input field is in focus

It appears that there is a major issue in iOS7 on iPhone and iPad devices, and possibly on other versions as well. When you focus on an input field, causing the keyboard to open, the layout experiences a severe and irreversible disruption upon changing or ...

What is the method for using Selenium in Python to locate elements based on CSS selectors on a webpage?

I'm currently experimenting with using Selenium to scrape the introductory paragraph from Wikipedia pages by utilizing CSS selectors. However, the code I have written seems to only extract paragraphs from the main web page, https://en.wikipedia.or ...

I am having trouble assigning the correct z-index to either the id or class of the Facebook message icon, which is appearing behind the menu

Recently, I encountered an issue on my website where the Facebook like button opening a comment window appears behind my drop-down menu. Despite having a z-index of 1000 on the drop menu, I haven't been able to identify which div or class should have ...

Stylish grey container with crisp white lettering

Just getting started with HTML and I've been working on a project for my class. Here's what I've come up with: <font style="background-color: grey; color: white; display: block">Black and white copies $.10 per page letter/legal size&l ...

Is it possible to customize the CSS styles of a jQuery UI modal dialog box?

Having trouble with my CSS styles not applying to a dialog modal added to my website using jQuery UI, even when using '!important'. I suspect that the predefined jQuery or UI CSS styles from a CDN link are preventing me from overriding them. The ...

jQuery failing to append code after being removed

I need some assistance with an issue I've run into while using jQuery's .remove(). Here is a snippet of code that showcases the problem: $( '.video-button span.glyphicon-play' ).click(function() { $( '#video-player' ).ap ...

Ways to retrieve the text of the chosen radio button label with the help of jQuery

There is a radio button on my webpage that looks like this: <div id="someId"> <label class="radio-inline"> <input name="x" type="radio" onchange="GetSelectedVal();">Yes</label> <label class="radio-inline"> ...

Add a CSS class to the text that is selected within a Content Editable div

Hey there, I'm having an issue where the class is being applied to the button when pressed instead of the selected text. Here's my current code: The button needs to be a div, but it might be causing the problem. I just want the highlighted text ...

The XPath for pagination is incorrect

Whenever I try to navigate to the next page, I keep encountering an error from the robot. Can someone help me construct the xpath using either of these snippets: <li title="2" class="ant-pagination-item ant-pagination-item-2" tabind ...