Ensure the child element is displayed at its maximum height within a parent container that includes overflow:auto

I have been trying to create a mask for a list of elements within a fixed width and height parent. The parent has overflow:auto set, so when new elements are added to the list, the parent scrolls. However, I am facing an issue where the mask does not adjust its height along with the parent scroll, leaving empty space at the bottom.

For simplicity, here is a snippet of my code:

http://jsfiddle.net/Z7sRh/

html:

<div id="test">
<div id="mask">&nbsp;</div>
<ul>
    <li>Element</li>
    <li class="over">Element</li>
    ... (more list items) ...
</ul>
</div>

css:

#test{
width:150px;
height:200px;
background-color:#eee;
overflow:auto;
position:relative;
}
#mask{
background-color:rgba(0,0,0,.3);
min-height:100%;
width:100%;
position:absolute;
top:0;
left:0;
bottom:0;
z-index:1;
}
li{
background-color:white;
}

.over{
position:relative;
z-index:2;
}

While some elements need to be displayed over the mask, I am open to a javascript solution if needed, although a pure html/css approach is preferred.

I have searched online for a solution to this problem but could not find one. I hope someone in this community can offer some advice or help with this issue.

Answer №2

Implementing JQuery:

$('#mask').height($('#test')[0].scrollHeight);

Executing JavaScript directly:

document.getElementById('mask').style.height = document.getElementById('test').scrollHeight + 'px';

Answer №3

Do you think this may be too complicated? Simply set the overs background color to white and the li to grey. You will achieve the same result regardless of how many elements there are. Is there a specific reason you require the extra div and code? http://jsfiddle.net/calder12/Z7sRh/2/

li{
 background-color:rgba(0,0,0,.3);
}

.over{
background-color:white;
}

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

Retrieve the div element by calling a scriptlet with JavaScript

I am facing an issue with a web page that includes a scriptlet like this: <div id="flash_chart"> <%=content_data['report_text']%> </div> The variable content_data['report_text'] contains a lengthy string ...

Avoid the button being clicked more than once

As a newcomer to JavaScript and AngularJS, I recently created a demo showcasing simple page navigation with modals. However, I encountered an issue where clicking on a button twice resulted in opening the same page twice. Despite using a timeout function i ...

Creating full-width divs using BootstrapLearn how to easily create full-width div

Bootstrap is being utilized in my current project. I have been creating divs inside columns, but they are not extending to the full width of the columns. Below is a snippet of the code: <!DOCTYPE html> <html lang="en"> <head> ...

The website's responsiveness is not adjusting correctly to different screen widths

When I switch to a responsive website, the background turns red at a width of "285px" even though I have set the max width to "1140px". If I use min-width, the effect doesn't show up until 256/276px. I've tried both max and min width settings, bu ...

An error occurred in jQuery.ajax: The object's property 'xhr' is not a function

I'm facing an issue with a jQuery.ajax object where I am trying to replace xhr. When I run the code below, it throws an error: TypeError: Property 'xhr' of object #<Object> is not a function The part of the code causing the error is ...

Tips for troubleshooting objects within an Angular template in an HTML file

When I'm creating a template, I embed some Angular code within my HTML elements: <button id="btnMainMenu" class="button button-icon fa fa-chevron-left header-icon" ng-if="(!CoursesVm.showcheckboxes || (CoursesVm.tabSelected == 'curren ...

Scroll the div back to the top when the form is submitted

I have set up a form in a pop-up using Bootstrap modal. The form is quite long, so after submission, the message appears at the top of the form. However, I want it to scroll to the top when the user submits the form so they can easily see the message. Is ...

Automated suggest feature similar to Stack Overflow's tag completion

Can you explain the concept behind using a tagging system similar to Stack Overflow? Specifically, I am interested in the front-end implementation. Are there any specific libraries that offer features such as auto-complete and tag separation, similar to ...

Guide to setting the first tab as the default tab using Thymeleaf, Css, and Bootstrap

I am currently working on a project where I need to dynamically create tabs based on a list retrieved from my Spring backend using Thymleaf and Bootstrap. While I have managed to successfully create the tabs and content, I am facing an issue where the fi ...

"Utilizing AngularJS to set an element's position as fixed relative to a different

Imagine an app that can place input text fields on top of an image, creating a workspace. The challenge is to position these fields using specific coordinates (top, left, width, height) that are relative to the image itself (top/left = 0/0). How can one ac ...

Using CSS to create clickable areas on an image

Here is an example image: https://i.sstatic.net/EerkN.jpg This image shows a desk with various elements, and I am looking for a way to make parts of the image interactive. For example, clicking on the monitor would take you to a specific link, and click ...

Use jQuery to retrieve the response from a JSON request and showcase it on the existing HTML page

Currently, I am working on a project that involves integrating a JSON-based web service from a remote server. The method of this service can be accessed by using specially formatted URLs such as: http://root-url/ws/service-name?request={json-string} The ...

What could be causing my Angular.js application to malfunction on IE7?

I have developed an Angular.js application that is working well on most browsers, but I am now facing compatibility issues with IE 7 and above. I have tried different approaches such as adding id="ng-app", using xmlns:ng, manually bootstrapping angular wi ...

retaining the scroll position of a window when refreshing a div

There's this issue that's been bothering me lately. I update the database via an ajax call, refresh the div, everything works fine, but it still scrolls to the top of the page. Here's what I have attempted: function postdislike(pid, user, ...

Retrieve the dynamically generated element ID produced by a for loop and refresh the corresponding div

I am facing a challenge with my HTML page where I generate div IDs using a for loop. I want to be able to reload a specific div based on its generated ID without having to refresh the entire page. Here is a snippet of my HTML code: {% for list in metrics ...

What is the best way to implement rounded borders using CSS?

Can we use the latest CSS standards to create rounded borders? Unfortunately, this feature is only available in CSS level 3 and above. ...

"The combination of Windows, @font-face, and Cyrillic characters presents a

I am encountering an issue with font-face and Cyrillic characters. The fonts display properly in any browser on OS X, but fail to render on a Windows 7 machine (chrome, ie etc). Even after putting the fonts through Font Squirrel and testing the demo files ...

What is the best way to ensure all table rows have consistent heights that are not affected by image sizes?

In the provided code snippet, you will notice that the height of the table rows varies slightly based on the height of the images. Currently, the image tags are set to height: auto;, and changing it to 300px would make all images the same size, however, m ...

Prevent images from displaying on smaller screens by utilizing Bootstrap's responsive design

Here is a snippet of code that utilizes Bootstrap 4 to display text and images: <div class="container"> <div class="row"> <div class="col-xl-12"> <img src="~/img/img1.png" style="height:60.3px;width:750px" clas ...

Issue with IE11 when using Bootstrap 4 and Flexbox

In my design, I have two columns that need to have equal heights when displayed next to each other on a large screen. However, on mobile devices, they should stack on top of each other. I am currently using the old BS grid for this layout. The content wit ...