"Responsive modal with dynamic height based on percentage and a minimum height constraint

My goal is to design a modal dialog that adjusts its height based on a percentage of the browser window height. The modal should maintain a minimum height to ensure it doesn't become too small, and it should dynamically grow, shrink, and re-center as the window size changes. This modal includes a header and footer elements that sandwich the main content.

Although I have made progress in creating this modal, there seems to be an issue where the content area and footer do not extend all the way to the bottom of the modal container (represented by the red box).

I'm curious about what might be causing this issue and how I can resolve it. Any suggestions?

- - CHECK OUT THE FIDDLE HERE - -

HTML:

<div class="modal">
    <div class="modal-header">Header</div>
    <div class="modal-body">
        [...content...]
    </div>
    <div class="modal-footer">Footer</div>
</div>

CSS:

.modal {
    border:1px solid red;
    width:600px;
    position:absolute;
    top:50%;
    left:50%;
}

.modal-header, .modal-body, .modal-footer {
    padding:10px;
}

.modal-header, .modal-footer {
    background:#ccc;
    font-weight:bold;
    font-size:14px;
}

.modal-body {
    height:inherit;
    overflow:auto;
    line-height:1.75em;
}

jQuery:

$(function() {
    $('.modal').css('height','50%');
    $('.modal').css( {
        'margin-top': (-1*$('.modal').height()/2),
        'margin-left': (-1*$('.modal').width()/2)
    })
});

$(window).resize(function() {
    $('.modal').css( {
        'margin-top': (-1*$('.modal').height()/2),
        'margin-left': (-1*$('.modal').width()/2)
    })
});

Answer №1

It may be a bit messy, but I managed to accomplish it by utilizing a mixture of overflow:hidden, position:fixed, multiple instances of position:absolute, and a few other tweaks. This should help point you in the right direction.

Check out this link for more details and examples!

Answer №2

I'm not very familiar with this topic, but check out this updated link.

All you need to do is adjust the footer by adding bottom:0 and resizing the height of the body.

Answer №3

You may need to adjust the sizing of your inner boxes for proper alignment.

Here's what I noticed: One box is set to 50%, while the other two are set to their content size. This means that sometimes there is too much space and other times not enough.

To fix this, try setting all three boxes to 25% each;

In addition, default margins on p elements might cause them to overflow outside the box.

Setting height and box-sizing properties on the header and footer will help contain them.
Here's a revised version of your CSS:

* {
    font-family:Arial, sans-serif;
    font-size:12px;
}

.modal {
    border:1px solid red;
    width:600px;
    position:absolute;
    top:50%;
    left:50%;
    min-height:200px;
}

.modal-header, .modal-body, .modal-footer {
    padding:10px;
    height:25%;
    box-sizing: border-box
}

.modal-header, .modal-footer {
    background:#ccc;
    font-weight:bold;
    font-size:14px;

}

.modal-body {
    height:inherit;
    padding:1px;
    overflow:auto;
    line-height:1.75em;
}

Consider setting a min-height for the modal container as well :)

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

Unable to include a controller in AngularJS that is declared in a different file

I am new to using Angular. Currently, all of my controllers are defined in the controllers.js file following these patterns: function A($scope){.....} function B($scope){.....} function MainCtrl($http) {.....} . . . angular .module('myApp') ...

What is the best way to incorporate a search feature within Bootstrap cards?

I've been struggling to incorporate a search feature within my bootstrap cards. Despite trying various online methods, none have proven successful for me so far. Below is my HTML code - any assistance in implementing a search filter into my app would ...

What could be causing the submit action to not work when using PHP's isset function?

This is an HTML registration form with a PHP action triggered by the submit button. <html> <head> <title>Registration Form</title> </head> <body> <h1 align="center"> REGISTRATION FORM </h1> <form ...

What techniques can I use to merge alpha channels with compositing in images?

Utilizing an HTML5 Canvas along with the KineticJS(KonvaJS) canvas library, I have successfully incorporated an Image element. My current objective is to erase specific pixels while maintaining a certain level of transparency. The red dot erases pixels at ...

Events Unavailable for Viewing - Full Calendar - Database Error Encountered

I am currently developing a calendar application. While I have managed to successfully display the calendar and add events, I am facing an issue with displaying events on the monthly view. Here is a snippet of the HTML code being used: <head> &l ...

Revise Swagger UI within toggle button switch

My project aims to showcase three distinct OpenApi definitions within a web application, enabling users to explore different API documentation. The concept involves implementing a toggle button group with three buttons at the top and the Swagger UI display ...

A more efficient method for passing a function as an argument using the keyword "this"

When it comes to passing a function as an argument that uses the this keyword, is there a preferred method or correct way to do so? An example would be helpful in understanding this concept better: For instance, if I want to fade out an object and then r ...

Is it possible to manipulate videos embedded in an iframe using javascript?

It's common knowledge that JavaScript commands from Google can be used to control YouTube videos, while Vimeo provides its own JavaScript commands for their videos. Both videos are typically embedded within 'iframes' on websites. I'm ...

Changing the Image Source in HTML with the Power of Angular2

Despite my efforts, I'm unable to display the updated image in my HTML file. In my TypeScript file, the imageUrl is updating as expected and I've verified this in the console. However, the HTML file is not reflecting these changes. In my researc ...

Is there a way for me to choose all the classNames that conclude with a specific word within the MUI sx property?

I am currently working with MUI and I have a need to modify certain properties that are prefixed with random IDs. How can I target, for instance, the first one using: '& endsWith(MuiAccordionDetails-root)' I wish to achieve this because the ...

What are some ways to reuse an angular component multiple times?

I am utilizing an angular component to dynamically load charts into my widget: Below is an example of the component: angular.module("generalApp").component("chartPie", { template: "<div class=Pie></div>", bindings: { data: "=", }, control ...

Sending an array via Ajax using jQuery

I'm trying to send 4 data elements via ajax post - 3 text strings and a multidimensional array of text strings. However, when I send the ajax request, only the 3 text strings are received, not the array. Here's my function that includes the ajax ...

Creating a JSON string that contains HTML output from PHP

My PHP code generates JSON output. <?php $html = utf8_encode($gegevens['tekst']); $html = htmlentities($html); //$html = htmlspecialchars($gegevens['tekst'], ENT_QUOTES, 'UTF-8'); echo json_encode(array( 'titel' ...

String casting for large JavaScript integers may require rounding to function properly

When trying to pass a large integer to a function from an onclick event in HTML, I always encounter issues with rounding. Despite using bigInt libraries, I still struggle to pass the full number accurately and would prefer a simple string casting method. ...

Dynamic div with height controlled by the content of the background image

I'm trying to figure out how to make a div with a background image of 100% width always maintain the aspect ratio of the image for its height. Is there a way to achieve this? In simpler terms, when I resize the page, I want the div to scale down whil ...

What is the best way to manage the back button functionality on pages that use templates?

I am currently developing a website using angularjs. The layout consists of two main sections: the menu and the content area. For instance This is an example page: /mainpage <div> <div id="menu"> <div ng-click="setTemplate('fi ...

I am experiencing an issue where the close button image on tap is not closing on the first tap on

Why does the page close on the second tap instead of the first tap when using an iPhone? The image changes on tap but doesn't close as expected. $("#privacy-close-btn").mouseenter(function() { $("#privacy-close-btn").css("display", "none"); $(" ...

Uh-oh, Ajax encountered a 500 Internal Server Error!

Utilizing ajax to fetch events from my database has hit a snag. Instead of displaying the results, there is nothing visible on the screen, and the console is showing this error message: POST http://www.example.com/system/live_filter.php 500 (Internal Se ...

Connect the blade.php file with CSS in Laravel version 5.x

I placed my view.blade.php file in resources\views\admin\login\view.blade.php and used the following code to link it to the CSS file: <link href="{!! HTML::style('css/style.css') !!}" rel="stylesheet">. My CSS file is lo ...

What could be causing the HelloWorld program in AngularJS to malfunction?

To access the codes, you can visit http://jsfiddle.net/hh1mye5b/1/ Alternatively, you can refer to: <!doctype html> <html ng-app="dcApp"> <head> </head> <body> <div> <div ng-controller="SpeciesController"> ...