What strategies can I implement to ensure my modal dialog box remains responsive? Adjusting the window size causes the modal box to malfunction and lose its structure

Whenever I adjust the size of the browser window, the elements inside the modal box become misaligned.

HTML

  <div class='modal'>
  <div class='modal-content'>
  </div>
  </div>

Below is the CSS for the modal box:

 .modal {
      display: none; /* Initially Hidden */
      position: absolute; /* Fixed Positioning */
      z-index: 1; /* On Top of Other Elements */
      left: 0;
      top: 0;
      width: 100%; /* Full Width */
      max-width: 100%;
      height: 100%; /* Full Height */
      overflow: auto; /* Scroll if Necessary */
      background-color: rgb(0,0,0); /* Fallback Color */
      background-color: rgba(0,0,0,0.4); /* Black with Opacity */
      padding-top: 60px;
    }

  .modal-content {
      background-color: #ffffff;
      margin: 5% auto 15% auto; /* Centered Vertically */
      border: 1px solid #888;
      width: 22%; /* Responsive Width */
      height: 55%;
    }

Answer №1

Instead of using relative height and width (%) in your CSS, try using viewport units (vw and vh) like this:

.modal {
      display: none; /* Initially hidden */
      position: absolute; /* Positioned fixed */
      z-index: 1; /* On top */
      left: 0;
      top: 0;
      width: 100vw; /* Full width */
      max-width: 100vw;
      height: 100vh; /* Full height */
      overflow: auto; /* Enable scrolling if needed */
      background-color: rgb(0,0,0); /* Fallback color */
      background-color: rgba(0,0,0,0.4); /* Black with opacity */
      padding-top: 60px;
    }

  .modal-content {
      background-color: #ffffff;
      margin: 5% auto 15% auto; /* Margin from top and bottom, centered horizontally */
      border: 1px solid #888;
      width: 22vw; /* Adjust based on screen size */
      height: 55vw;
    }

You can see a live example here.

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

Encountered a CastError in Mongoose when trying to cast the value "Object" to a string

I am struggling with a Mongoose CastError issue within my Node.js API. The problem arises at a specific route where data is being returned appended with some additional information. Despite finding various solutions for similar problems, my scenario seems ...

Refresh needed for Material UI styles to load correctly in Next JS

UPDATE: Reproducible issue https://github.com/ganavol409/next-material-ui-classes-bug Issue seems to be related to Higher Order Components and importing useStyles from Material UI Implemented Solution: https://github.com/mui-org/material-ui/blob/master/ ...

Steps to create a typewriter effect using a collection of words

I managed to create a looping typewriter effect, but I'm facing an issue where the first word in the array is not being typed again after the first cycle. Additionally, I am seeing 'undefined' displayed after the last word before it repeats ...

What is the top choice for creating a shallow copy of an array

While delving into the vue source code today, I stumbled upon a method of writing that left me puzzled. view source const deduped = [...new Set(pendingPostFlushCbs)] My initial thought is that it is a shallow copy of the array. But why is there a need t ...

JQuery Datatables: Struggling to make JQuery function work following AJAX update

Watch this screencast to get a clearer understanding of the issue.... I'm currently working on my first project involving AJAX, and I'm encountering some obstacles. The datatable is loading user details from a JSON output using AJAX. Each row ...

Script malfunctioning following an AJAX request

My page consists of a header where all my javascript is located. Within the body of the page, there is a link that triggers an ajax http request using vanilla JavaScript (not jQuery). This request retrieves a PHP form and injects it onto my page. The PHP ...

Discover the art of implementing linear gradient backgrounds in Tailwind css effortlessly

Is there a way to incorporate this specific color scheme into my background? linear-gradient(355.45deg, #FFFFFF 11.26%, rgba(255, 255, 255, 0) 95.74%) I have knowledge on how to implement this color using regular CSS, but I'm curious about how it can ...

Conceal the scrollbar when the expansion panel is in its collapsed state

One issue I am encountering is that the scroll-bar appears when the expansion panel is collapsed, but not when it's expanded. Here are the references: Collapsed Expanded <mat-expansion-panel class="panel"> <mat-expansion-panel-he ...

Ways to display a US map using d3.js with state names positioned outside each state and pointing towards it

Currently, I am working with d3.js and d3-geo to create a map of the USA. My goal is to display the names of some states inside the state boundaries itself, while others should have their names positioned outside the map with lines pointing to the correspo ...

Troubles with showcasing icons on a website

I need some help with a minor issue that I'm stuck on. I'm trying to display an information icon that, when clicked, opens a pdf. The strange thing is that the icon doesn't show up on the page even though it exists on the server. However, wh ...

Exploring and Troubleshooting HTML and JavaScript with Visual Studio Code Debugger

Visual Studio Code Version 1.10.2 Windows 10 I am currently experimenting with VS Code and would like to debug some basic HTML and JavaScript code. The instructional video found at http://code.visualstudio.com/docs/introvideos/debugging mentions that ...

I am having trouble with my 'SELECT DISTINCT' command, it's not functioning correctly

When I attempted to populate my drop down menu with values from the database using 'Select DISTINCT', it only retrieved some of the data instead of all of it. Below is my PHP code: <? $sqlretrive = mysql_query("Select DISTINCT type from ...

Retrieve JSON encoded data from a separate AJAX request

Currently utilizing prestashop 1.6, I have a specific ajax function where I encode data that is successfully retrieved on the console using echo. Now, my task is to decode this JSON data in another ajax function in order to extract a value for a particular ...

Adjust the parent element's height based on the child element's height, taking into consideration their respective positions

Is there a way to adjust the height of the parent element that has a relative position based on the height of child elements with absolute position? In the example below, the height of the .parent element is displaying as 0px Note: I am looking for a so ...

Having trouble installing Angular 4 with npm?

After installing Angular, I encountered an error when trying to use the command line "ng -v". Please refer to the attached jpeg file. My node version is 6.10.3. Does anyone have a solution? ...

The transition feature is not functioning properly in Firefox

I have a basic HTML and CSS setup below. It seems to be working fine in Chrome, but I'm having trouble with the transition effect in Firefox. I am currently using Firefox version 18. I tried looking up solutions on Google and Stack Overflow, but none ...

Issue with Displaying Local Server Image in Angular 2 HTML

I am facing an issue with my Angular 2 Application. It retrieves items from a local database where the server stores the image of the item and the database stores the path to that image stored on the server. While I can retrieve all the items without any p ...

JavaScript slowness

Currently, I am developing a test page that consists of buttons triggering various scripts. One of the functionalities I am trying to implement is changing the background color every second for 5 seconds, cycling through a total of 5 different colors. Desp ...

Tips for repairing damaged HTML in React employ are:- Identify the issues

I've encountered a situation where I have HTML stored as a string. After subsetting the code, I end up with something like this: <div>loremlalal..<p>dsdM</p> - that's all How can I efficiently parse this HTML to get the correct ...

Tips for saving data obtained from an ajax call

My jquery ajax function has a callback that creates an array from retrieved json data. Here's an example: success: function (response) { callback(response); }, The callback function, in this case createQuestionsArray(), populates ...