What is the best way to add scrolling to an absolutely positioned div that exceeds the viewport size?

Having an absolute positioned DIV with a defined height and width sometimes leads to it being cropped out when it doesn't fit the viewport's height. This can be frustrating, as shown in the images below:

NORMAL VIEWPORT


SMALL HEIGHT VIEWPORT

I've attempted various methods such as using overflow: scroll on the overlay div, the div itself, and even the body, but none of them have provided a solution without resorting to JavaScript. Is there any way to address this issue purely through CSS?

HTML

<div id="overlay">
    <div id="login">
       <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
    </div>
</div>

CSS

#overlay{
    width: 100%;
    height: 100%;
    position: fixed;
    background: rgba(0,0,0,.8);
    z-index: 100;
}

#login{
    width: 250px;
    height: 320px;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    position: absolute;
    margin: auto;
    background: #fff;
}

Answer №1

After reviewing your question :

Did you experiment with using overflow:auto; instead of just height and setting max-height property?

#overlay{
    width: 100%;
    height: 100%;
    position: fixed;
    background: rgba(0,0,0,.8);
    z-index: 100;
}

#login{
    width: 250px;
    max-height: 320px;
    overflow:auto;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    position: absolute;
    margin: auto;
    background: #fff;
}
<div id="overlay">
    <div id="login">
       <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
    </div>
</div>

Answer №2

Implement overflow: visible to the specified DIV element as well as on the black background, and also on the body: overflow: auto. This will enable you to scroll the body content if its height exceeds that of the window.

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

No data coming back from API in JSON format

Can anyone help me figure out what I'm doing wrong? I've been using the sample code from the API documentation (https://github.com/bitmarket-net/api), but the JSON response is empty. All I did was add echo bitmarket_api("info"); and replace the ...

Ensuring each div element has a unique bullet icon: A step-by-step guide

I am looking to dynamically create bullet icons based on the number of div elements present in a slider. For instance, if there are 2 slider divs, I want to generate 2 bullet icons within another div. If there are no div elements, then the bullets should ...

Checking a text input using JavaScript

Looking for some guidance on how to validate a textbox before calling PHP when clicking the SUBMIT button. Here is my code: <html> <head> <script> function myfunction(val) { var x=document.getEl ...

Tips for shifting div background image while the push menu is in use

Looking for a Solution: I'm trying to figure out how to adjust my background image within a div when the push menu is active. Here's the CSS I'm currently using: .webcam-bg { background-attachment: fixed; background-image: url("../ ...

Creating a CSS section that expands with a minimum height of 100% and perfectly centers its content vertically

Seeking an elegant solution for having sections with a minimum height of 100% (window height). The div should expand to fit the content if it exceeds 100%, and vertically center the content within the div if it is smaller. I have managed to find a simple ...

Assistance needed with troubleshooting an HTML and CSS table error

When checking the error console in Opera, I noticed an error related to this line of code: filter:alpha(opacity=99); opacity=0.99; MozOpacity=0.99; I am currently using it in this manner. Any idea what might be causing the issue? ...

Adjust the position of an image to move it closer to the top using CSS

I have a fiddle where I am trying to adjust the position of an image (keyboard) to match a specific design. https://i.sstatic.net/flqCH.png In my fiddle, the keyboard image is slightly lower than the desired position shown in the design. I am looking for ...

What could be causing this excessive lag?

I've been developing a new website, but the "buttons" I'm using seem to be causing significant lag. I need help resolving this issue. You can view the website here: The problematic code snippet is shown below: <td> <a href="templi ...

Issue with ASP.NET C# querying MS Access database

I am experiencing issues with the like query not working. Can someone help me troubleshoot? Below is the code I am using: string Item_Name = txt_search.Text.Trim(); string Conn = ConfigurationManager.ConnectionStrings["AjitConnectionString"].ToString(); ...

What is the best way to dynamically shift the position of an image on a page while keeping it in place in real-time?

A group of friends and I are currently collaborating on an experimental project for a presentation. Our goal is to have multiple elements (such as images, text, gifs) displayed on a page that can be dragged around in real-time using a draggable script whi ...

What is the best way to make a sticky floating sidebar that stays fixed while scrolling?

I'm facing a challenge on my website where I want a sidebar on the left to stay with the user as they scroll. The sidebar should scroll along with the user until it's 5px from the top of the page, at which point it should remain fixed in place. ...

Arrangement of asymmetrical interactive forms

I am in need of creating a design featuring numerous non-rectangular shapes that are interactive and reveal an image when clicked. The concept is to transform an image of stained glass into a webpage where each pane can be clicked to unveil its colorful ve ...

In JavaScript, the code fails to verify the email entered through a form

Hi, I'm not sure if this is the right place to ask my question but I'm having trouble with a code that I can't seem to figure out. I've tried various algorithms but none of them seem to work. My issue involves validating an email from a ...

Data within object not recognized by TableCell Material UI element

I am currently facing an issue where the content of an object is not being displayed within the Material UI component TableCell. Interestingly, I have used the same approach with the Title component and it shows the content without any problems. function ...

Breaking a line within an ngFor loop

I'm working with an ngFor loop to generate multiple PrimeNG buttons. Currently, the buttons are displayed side by side on the same row, but I want each button to appear vertically on its own line. How can this be achieved? Below is the segment of my c ...

What is the best way to arrange text inputs in alignment?

I encountered an issue while creating a form that includes fields for username, password, and email. The alignment of the email input box seems to be off when compared to the username and password input boxes. Is there a method to ensure that all the input ...

How can I use an ajax get request to retrieve a css file from a webpage and then customize the default styles in my program?

Here is my current code snippet: HTML: <h1>Test Message</h1> Default CSS: h1{ font-size: 24px; } Jquery: $(document).ready(function(req, res){ $.ajax({ url:"example.com/test.css", type : "GET", crossDomain ...

What could be the reason for the malfunction of the Bootstrap panel toggle feature in a ReactJS production build

In my ReactJS development, I have successfully added Bootstrap panel toggle functionality. However, I encountered an issue when deploying the React build code - the panel is not expanding. After investigating, I realized that the problem lies in using hr ...

Django Preload: Selective Loading of Images

I am currently working on a BlogApp and my goal is to have the Image Page load only a limited number of images when opened. My objective: I aim to Load only 9 images from the Server. Specifically, I want to display a few images at first when the user ope ...

What is the best way to retrieve a specific item from a dropdown menu?

Here is the HTML code snippet I am working with: <!DOCTYPE html> <html> <body> <select name="action" size="1" id="action-choice"> <option value="test">Test</option> <option value="run">Run</option> ...