When the parent element reaches its maximum height, the child element becomes scrollable

I developed a modal window that showcases a form. Users can simply click on a button to insert new rows into the form. As more rows are added, the entire modal window should gradually increase in height until it's positioned 50px above the bottom of the viewport.

Here is a snippet from the demo:

 <div class="parent">
    <h4>window title</h4>
    <a href="#">add new row</a>
    <section>
      <h4>content</h4>
      <p>some random content which never scrolls</p>
      <ul><li></li></ul>
    </section>
  </div>

The approach I'm using involves placing the modal window inside a div with absolute positioning. As the window grows taller, it will eventually reach a certain height and then I could apply overflow-y: scroll to the contents.

The challenge arises when only the form itself should be scrollable. When each new row is appended, the window should expand in height until it matches its parent's height. Once this point is reached, solely the ul content should become scrollable - without affecting the entire modal.

While I acknowledge that this problem can be solved with javascript, I am aiming to discover a pure css solution in order to eliminate any DOM-specific logic in our app, and to avoid dealing with viewport resize events, etc.

  • overflow-y requires setting a max-height for the container. However, since I don't know what the list's height will be, this solution doesn't completely work.
  • I could potentially make the height of the list dynamic with absolute positioning, but this would cause the parent window to lose its height and fail to adjust as the child element changes size.

Preview Demo Link

Answer №1

If your parent content's height and the height of your section content before the ul are consistent and do not change dynamically based on content, you can use a max-height: calc(100vh - valuePx) to make the ul scrollable. I have also added a margin: 0 to your ul css.

Check out the JSFiddle example

$(function() {
  var ul = $('ul');
  $('a').on('click', function() {
    ul.append('<li></li>');
    return false;
  });
})
.parent {
  position: fixed;
  bottom: 56px;
  overflow-y: hidden;
  top: 56px;
  /* demo only */
  border: 1px solid red;
  left: 50%;
  margin-left: -100px;
  width: 200px;
}
section {
  border: 1px solid blue;
  min-height: 300px;
}
ul {
  padding: 0;
  max-height: calc(100vh - 341px);
  overflow-y: auto;
  margin: 0;
}
li {
  background: #ccc;
  height: 50px;
  margin-top: 10px;
}
li {
  list-style: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="parent">
  <h4>window title</h4>
  <a href="#">add new row</a>

  <section>
    <h4>content</h4>

    <p>some random content which never scrolls. the list below should scroll when the section reaches the max height</p>
    <ul>
      <li></li>
    </ul>
  </section>
</div>

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

Tips for altering the class of an HTML button dynamically when it's clicked during runtime

I currently have a set of buttons in my HTML page: <button id="button1" onclick="myFunction()" class="buttonClassA"></button> <button id="button2" onclick="myFunction()" class="buttonClassA"></button> <button id="button3" onclic ...

The margin 0 auto feature is ineffective, and instead, the target is activated upon the page's initial loading

As a beginner in CSS, I am encountering some issues with the HTML page of my application. I am currently working on an app and aiming to create a login view where the login form appears either after a voice command or button click. I am facing two main pro ...

Dynamic Bootstrap Tab menu slider with movable functionality when the limit is reached

I am facing an issue with the tabs menu on my website. The tabs menu items can vary between 2 or 3 to even 20 or more. When the number of navigation items exceeds 10, they automatically drop to the second line, which you can see here. However, I don' ...

The MUI snackbar element lingers in the DOM even after it has been closed

Having created a global modal with the intention of only utilizing it when necessary, I've encountered an issue where the snackbar div persists in the DOM. This persistence is causing certain elements to become blocked as they appear beneath this div. ...

Adjusting the sidebarPanel height to match the mainPanel content in a shiny app

I'm currently developing an app that features a scrollable sidebarPanel alongside a mainPanel that can have varying heights, such as: library(shiny) ui <- fluidPage( headerPanel('Iris k-means clustering'), sidebarPanel(style = " ...

Arranging Bootstrap buttons in a clean row

How can I add two nav buttons "register" and "sign in" to the top right of my navigation bar, and include a separate button "request a demo" below them without displacing the first two buttons? I am currently using a br tag to push the third button down ...

Enhancing the smoothness of parallax scrolling

My header is going to be twice the height of the viewport. I added a simple parallax effect so that when you scroll down, it reveals the content below. However, I'm experiencing flickering in the content as I scroll, possibly due to the CSS style adju ...

What is the best way to conceal an image generated by a control - using JavaScript, CSS, or another method?

I am looking to remove the background image of the ReportView control, specifically the toolbar_bk.png image. Rather than hiding it, I want to set it to none. How can I achieve this easily? Should I use server-side code, CSS, JavaScript, or perhaps jQuery ...

The image appears fuzzy until you hover over it and it transforms into a larger size

Encountering a strange issue with using the zoom property on an image during hover state. The image seems to be blurry before and after the scale transition, but surprisingly sharp during the actual transition. Any tips on how to prevent this blurriness in ...

A guide on monitoring SASS files in all subdirectories with an npm script

Is there a way to watch all sass directories and generate CSS files to the corresponding 'CSS' folder, including subdirectories? The current method involves listing each directory separately in the NPM script. "scripts": { "sas ...

When you hover over the page, the content shifts to reveal a hidden div

Trying to figure out how to show additional content when a photo is hovered over without shifting the rest of the page's content placement? Looking for alternative solutions rather than using margin-top: -22%. Here's a link to the website in que ...

Best practices for arranging several divs with CSS

I am in the process of creating a main header for several web pages I'm working on. The layout I have in mind includes an image, centered text below the image (all to the left of the main header), a main header that I want to be in the center of the p ...

Challenges with Image Placement and Alignment

Having trouble with the sizing of my image in the skill portion of my website. The clouds and mountains look fine, but for some reason, the image isn't the same size. Additionally, the text "full stack developer" is not centered under my name anymore ...

What is the best way to adjust the height of my website to properly display on all screen sizes

Is there a way to make my web page fit perfectly on the screen in terms of both width and height? I'm familiar with using Media Queries for adjusting widths, but how can I also control the height? Any suggestions on setting the height? I would great ...

Position text above an image using CSS

.content { float: left; width: 33.33%; font-weight: bold; text-align: center; margin-top: 5px; margin-bottom: 2px; overflow-wrap: break-word; } .content_div { position: absolute; width: 100%; left: 0; top: 100%; font-size: 16px } ...

Steps to alter background image and adjust its height upon function activation

I am working on a search page with an advanced search option that only certain users can access. I need the height of my div to increase accordingly and also change the background image to a larger size when the advanced search is selected. How can I make ...

Tips for adjusting the search bar's position on a mobile device when it is activated by the user

I need help with an open source project where I am developing a search engine using Angular. When using smaller screen sizes, the search bar is positioned in the middle but gets hidden behind the keyboard terminal when clicked on. Can anyone advise on ho ...

Adjust the div's width dynamically based on its content

I've spent a lot of time reading through the questions and answers on this site, but I still can't figure out why my container won't allow horizontal scrolling for my smaller divs. CSS #theLoopTasksLabels { width:90%; height:90px; ...

Creating a contact page with Font Awesome icons similar to my design using Flexbox in CSS

How can I achieve the desired layout for this image? I have written some code and applied CSS to get the output, but I encountered an issue. When the text in my address section is too large, the icon gets misaligned and collapses. How can I fix this issue ...

Is it possible for me to use an image as a grid item?

I have a grid with 3 elements as shown below: Now, I am looking to replace the blue elements with images (they can be just placeholders). .grid-container { display: grid; grid-template-columns: 1fr 1fr; grid-auto-rows: minmax(30em, auto); justi ...