Incorrect modal placement

I am new to CSS and currently working on customizing a modal window. My main issue lies with its positioning, especially when the browser resolution changes, causing a misalignment as seen in the screenshot. I would like to specify the width and height dimensions in percentages.

See Screenshot

Modal HTML:

<div class="modal fade" id="erroAdocaoAindaNaoConcluida" tabindex="-1" role="dialog" aria-labelledby="erroAdocaoAindaNaoConcluidaLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header" align="center">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>

                <span class="titulo_modal" id="erroAdocaoAindaNaoConcluidaLabel">Error Detected</span>

            </div>

            <div class="modal-body" align="center">
                <p class="titulo_form"> You can only adopt one animal at a time.</p>
            </div>

            <div class="modal-footer">

                <button type="button" class="tamanho_btn_adotar btn-primary" data-dismiss="modal">Close</button>

            </div>
        </div>
    </div>
</div>

modal-content CSS:

.modal-content{
   position:relative;width:45%; 
   background-color:#fff;
   -webkit-background-clip:padding-box;
   background-clip:padding-box;
   border:1px solid #999;
   border:1px solid rgba(0,0,0,.2);
   border-radius:6px;
   outline:0;
   -webkit-box-shadow:0 3px 9px rgba(0,0,0,.5);
   box-shadow:0 3px 9px rgba(0,0,0,.5)
}

.modal-content{
   -webkit-box-shadow:0 5px 15px rgba(0,0,0,.5);
   box-shadow:0 5px 15px rgba(0,0,0,.5)
}

modal-body CSS:

.modal-body{position:relative;padding:15px}

modal-dialog CSS:

.modal-dialog{width:600px;margin:30px auto}
.modal-dialog{
   -webkit-transform:translate(0,0);
   -ms-transform:translate(0,0);
   -o-transform:translate(0,0);
   transform:translate(0,0)
 }

.modal-dialog{
   position:relative;
   width:auto; 
   top:20px; 
   left:8%;
   margin:10px
}

Answer №1

I have made some modifications to your CSS and added additional elements. Please test the code snippet below:

.modal-content {
position: relative;
width: 45%;
background-color: #fff;
-webkit-background-clip: padding-box;
background-clip: padding-box;
border: 1px solid #999;
border: 1px solid rgba(0,0,0,.2);
border-radius: 6px;
outline: 0;
-webkit-box-shadow: 0 3px 9px rgba(0,0,0,.5);
box-shadow: 0 3px 9px rgba(0,0,0,.5);
margin: 0 auto;
}
.modal-content {
-webkit-box-shadow: 0 5px 15px rgba(0,0,0,.5);
box-shadow: 0 5px 15px rgba(0,0,0,.5)
}
.modal-dialog {
width: 600px;
margin: 30px auto
}
.modal-dialog {
-webkit-transform: translate(0, 0);
-ms-transform: translate(0, 0);
-o-transform: translate(0, 0);
transform: translate(0, 0)
}
.modal-dialog {
position: relative;
width: auto;
top: 20px;
margin: 10px
}
.modal-body {
position: relative;
padding: 15px
}
@media (max-width:480px) {
.modal-content {
width: 100%;
}
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal"> Launch demo modal </button>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="erroAdocaoAindaNaoConcluidaLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header" align="center">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <span class="titulo_modal" id="erroAdocaoAindaNaoConcluidaLabel">Detectamos um erro</span> </div>
      <div class="modal-body" align="center">
        <p class="titulo_form"> Você só pode adotar um animal por vez.</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="tamanho_btn_adotar btn-primary" data-dismiss="modal">Fechar</button>
      </div>
    </div>
  </div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> 

Answer №2

Set the positioning of your parent div to absolute.

Answer №3

@Isaac Meneses Please update the following CSS properties in your code.

.modal-dialog {
    position: absolute;
    margin: 20px auto;
    width: 50%;
}

.modal-content {
    position: absolute;
    background-color: #f9f9f9;
    -webkit-background-clip: content-box;
    background-clip: content-box;
    border: 2px solid #555;
    border: 2px solid rgba(0,0,0,.3);
    border-radius: 8px;
    outline: 1;
    -webkit-box-shadow: 0 5px 12px rgba(0,0,0,.6);
    box-shadow: 0 5px 12px rgba(0,0,0,.6);
}

Answer №4

Efficient adaptability. Can function seamlessly even when the width and height are designated as auto.

.overlay {
  /* Example styles for demonstration */
  /* specifying width and height is not necessary */
  width: 300px;
  height: 300px;
  border: 1px solid blue;
  
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
<div class="overlay">
</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

Divergent find function behavior in jQuery when applied to div or tbody

I've encountered an issue while using the jQuery find selector by Id with a div and tbody. Let me simplify my problem for better understanding. HTML <div id='iamdiv'>HELLO</div> <table> <tbody id='iamtbody' ...

Managing the width of an HTML table column based on its text content dynamically

Here is an example of an html table : <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="480px" class="societe" ><div style="float:left;font-size:12px;padding-top:2px" ><?php echo $_SESSION ...

Mobile view doesn't quite center using flexbox, even though it works fine on desktop

Utilizing flexbox, the two distinct sentences that form the content of the page are centered. However, upon viewing the website on my mobile phone, the second sentence appears aligned to the left side. Here's how the site appears on a phone I've ...

List items that are not in any particular order spilling over onto the next

Having an issue with an unordered list where the list items aren't all on the same line. The 5th element is dropping to the next line even though each item is set to 20% of the container. The math adds up, so not sure what's causing the problem. ...

In order to maintain a custom tooltip in an open state until it is hovered over, while also controlling its

When hovering over the first column of the table, a tooltip will appear. Within each material tooltip, I would like to include a button at the bottom right after the JSON data. When this button is clicked, it should open an Angular Material dialog. <ng ...

"Creating a navigational bar using Bootstrap in a one-page application

I am facing an issue with Bootstrap's navbar in my single-page application. The dropdown navigation menu for mobile is not collapsing properly after clicking. Although I tried to fix it by adding data-toggle="collapse" data-target="#navbar" attributes ...

Python makes sure that HTML values remain constant even after a button is clicked by Selenium

I am currently utilizing Soup and Selenium to access a particular page . My objective is to extract a comprehensive list of pricing and ratings for different types of packaging available on this webpage. https://i.sstatic.net/3gbJ7.png Displayed below is ...

Toggle the visibility of a div based on the user's session status

Having an admin link with the div id "admin" and starting sessions when a user is logged in helps distinguish between normal users and admins. While normal users are restricted access to files designated for admin only, they can still view the admin link. ...

Issue with locating an item in a dropdown menu while using Selenium

I am currently attempting to locate an element within a dropdown list in my Selenium test. Despite identifying the xpath using Firebug, the Selenium code is unable to find it. My goal is to pinpoint option value number 2 in the following html snippet: < ...

The TypeAhead feature fails to display any search results for the entered query

Feeling really desperate right now! I'm facing a frustrating issue with Bootstrap Typeahead. Here is the HTML Markup: <div class="form-group"> <label for="">Recipients</label> <input id="recipients" name="recipients" ...

Issue of Modal not hiding persists despite attempted solutions using JS, jQuery, and CSS

I am attempting to display a modal exclusively on mobile devices. Despite my confidence in the correctness of my JavaScript, it is not functioning as expected. Would anyone be willing to review and provide assistance? <div class="col-5 col-sm-2 ml-au ...

Activate the textbox without utilizing `.focus()` method

I am encountering an issue with a small iframe on my page. The content within the iframe is larger than the window itself, requiring users to scroll around to view it in its entirety. Within this iframe, there is a button that, when clicked, triggers an an ...

Is it possible to utilize CSS to form a triangle outline on the right-hand side of a Bootstrap list group?

https://i.sstatic.net/vSoa0.png Hey everyone! I'm trying to achieve a triangle shape using CSS at the end of a list item. I've utilized the list group component from Bootstrap to display my list. Below is a snippet of my code. I've als ...

Embed fashion and graph elements into a CSV document (generated from <script>)

Encountering an issue with my code. I am looking to export an HTML table to a CSV file (specifically Libre Office Calc, regardless of csv, xls or xlsx format, as long as it runs on a Linux Server). Found a script online that works well after some modificat ...

The functionality of the "this" keyword appears to be malfunctioning

I've been grappling with the concept of the this keyword in JavaScript and decided to create this script to test it out: function click(){ this.innerHTML="changed"; } However, when I tried to use it in this HTML: <button id="clicker" onclick ...

Updating backgroundPosition for dual background images within an HTML element using Javascript

Issue at Hand: I am currently facing a challenge with two background images positioned on the body tag; one floating left and the other right. The image on the left has a padding of 50px on the left side, while the image on the right has a padding of 50px ...

The Angular bootstrap datetimepicker doesn't support disabling past dates

Has anyone successfully disabled past dates on an angular bootstrap datetimepicker before? I've tried using the date-disabled attribute, but I can't seem to get it to work. <datetimepicker class="calendar-format" data-ng-model ...

What are the steps to add 8 columns to the second row in my table?

this is an example of HTML code that showcases a table structure with multiple rows and columns. You can view the full code snippet here. The table includes different sections, such as section1, section2, section3, and section4 in the first row (tr). In t ...

Is there a way to adjust the spacing between cards in Bootstrap?

I am attempting to replicate the layout shown in the attached picture. How can I adjust this property? Expected output: I would like the elements to be closer together and centered on the page, but I am struggling to achieve this. I have tried adjusting ...

Is there a way to avoid overlapping in CSS3 transforms?

Check out my jsfiddle demo: http://jsfiddle.net/WLJUC/ I have noticed that when you click to rotate the larger container, it overlaps the smaller one. This poses a problem as I am attempting to create a picture gallery where users can rotate and enlarge ...