<script type="text/javascript">
$(window).on('load',function(){
$('#loginerrormodal').modal('show');
});
</script>
<div class="modal fade" tabindex="-1" role="dialog" id="loginerrormodal">
<div class="modal-dialog" role="document">
<div class="modal-content bg-white">
<div class="modal-header">
<h5 class="modal-title">...</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Chiudi</button>
</div>
</div>
</div>
</div>
When I run the modal on mobile, the black background isn't responsive:
(the modal opes at page load so I don't have buttons to launch it)
How I can fix?
As it's possible to see in the live version you provided, there is an element exceeding the total width of the device, which is disturbing the modal.
Find the <div> with classes "card", "bg-white" and "mx-auto" and remove the inline style of width: 30rem for mobile devices breakpoint (480px).
If that's not possible, you can create a media query using !important to override the inline style, like this:
#media (max-width: 480px) { // the breakpoint that the problem is ocurring
.card.bg-white.mx-auto {
width: 100% !important; // it should work instead of 30rem
}
}
I fix with this:
.modal-backdrop {
height: 100%;
width: 100%;
}
Related
Is there a way to limit the size of bootstrap 5 modal dialog and backdrop?
The example below is occupying 100% of the screen, is it possible to make it cover only the parent main container?
<main class="container-fluid pt-4 px-4" style="height: 400px">
<!-- Button trigger modal -->
<button
type="button"
class="btn btn-primary"
data-bs-toggle="modal"
data-bs-target="#exampleModal"
>
Launch demo modal
</button>
<!-- Modal -->
<div
class="modal fade hide"
data-backdrop="false"
id="exampleModal"
tabindex="-1"
aria-labelledby="exampleModalLabel"
aria-hidden="true"
>
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5
class="modal-title"
id="exampleModalLabel"
>
Modal title
</h5>
<button
type="button"
class="btn-close"
data-bs-dismiss="modal"
aria-label="Close"
></button>
</div>
<div class="modal-body">...</div>
<div class="modal-footer">
<button
type="button"
class="btn btn-secondary"
data-bs-dismiss="modal"
>
Close
</button>
<button
type="button"
class="btn btn-primary"
>
Save changes
</button>
</div>
</div>
</div>
</div>
</main>
I added the CSS below but not working.
.modal-dialog, .modal-backdrop {
/* supposed to take the height and width of parent container */
height: 100% !important;
width: 100% !important;
}
How it works:
What I want (or expected behavior):
The modal component adds a the backdrop element (outside the .modal's parent) automatically, so if you want the modal to be completely contained inside the parent, start by disabling the backdrop with data-bs-backdrop="false".
By default the .modal has position: fixed so it gets rendered relative to the viewport without regards to its parent element(s). This behavior can be overridden by giving the (main) parent position: relative; and give the .modal child position: absolute;. You can also give the .modal an rgba() background-color to simulate an encapsulated, static backdrop within the parent only (clicking on a static backdrop does not close the modal).
To have the modal centered within the parent add .modal-dialog-centered to the .modal-dialog.
In the snippet below I have given both main and .modal a background to more clearly demonstrate:
main {
position: relative;
background: #EEE;
}
main .modal {
position: absolute;
background: rgba(0, 100, 0, 0.35);
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.2.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
<div class="container-fluid">
<div class="row">
<div class="col-2">
sidebar
</div>
<div class="col-10">
<div class="row">
<nav class="navbar bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="#">Navbar</a>
</div>
</nav>
</div>
<div class="row">
<main class="container-fluid pt-4 px-4" style="height: 400px">
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal">
Launch demo modal
</button>
<!-- Modal -->
<div class="modal fade hide" data-bs-backdrop="false" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">
Modal title
</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body flex-grow-1">...</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
Close
</button>
<button type="button" class="btn btn-primary">
Save changes
</button>
</div>
</div>
</div>
</div>
</main>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.2.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4" crossorigin="anonymous"></script>
I am using a bootstrap modal and the close button is an image. When the modal size changes depending on the viewport, I want the close button to always stay in the top right corner of the modal
<div id="myModal" class="modal modal fade">
<a class="close-modal" href="javascript:void(0)" onclick=closeModalBox()>
<img src="images/close.png" width="39" >
</a>
<div class="modal-dialog">
<div class="modal-content" style="position: relative;">
<div class="modal-body" >
<!-- code -->
</div>
</div>
</div>
</div>
The css:
.close-modal {
margin: 0;
position: absolute;
opacity: 1;
z-index: 10;
cursor: pointer;
top: 1%;
left: 77%;
}
.modal-content{
position: relative;
}
Any ideas? Thanks
There seems to be an issue with the html structure, see example below, to keep the close button in the correct position. also refer to this link. Modal
<div class="modal fade" id="exampleModalLong" tabindex="-1" role="dialog" aria-labelledby="exampleModalLongTitle" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
Do you mean to keep the button while scrolling down?
If that is the case use the "sticky" variable,
https://getbootstrap.com/docs/4.0/utilities/position/
You are using percentage values which will change visually when going between device sizes. Set these to fixed values so your close button will always be at a specific spot relative to the device size. Also, because you are using absolute positioning you can take advantage of the 'right' attribute instead.
https://www.w3schools.com/cssref/pr_pos_right.asp
.close-modal {
margin: 0;
position: absolute;
opacity: 1;
z-index: 10;
cursor: pointer;
top: 50px; // define your value here
right: 80px; // define your value here
}
I make the Scrollbar of page always visible by using the following code:
html {
overflow-y: scroll;
}
However, when opening Bootstrap modal, the main scrollbar is not available (visible but disabled) and when trying to some code as shown below does not solve the problem.
.modal-open {
overflow-y: scroll !important;
}
What I want to try the following approaches:
1) I want the main scrollbar is enabled even if modal is open
2) I want the scrollbar of the modal is visible automatically, but when I use overflow-y:scroll; the height of the modal-body cannot be increased automatically and scrollbar on the modal content seems to be disabled (when I set the height, scrollbar is enabled, but ı want to use auto height). Any idea?
Try to change this line:
.modal-open {
overflow-y: scroll !important;
}
to:
body.modal-open {
overflow: scroll !important;
}
The snippet:
body.modal-open {
overflow: scroll !important;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target=".bs-example-modal-sm">Small modal
</button>
<div class="modal fade bs-example-modal-sm" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel">
<div class="modal-dialog modal-sm" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
This is the modal
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<div style="height: 1600px;"></div>
I am trying to change the default width of the dialog but it's not working.I have tried solutions mentioned in this post. Here is my HTML code below:
<style>
#myDialog .modal-dialog{
width:80%;
}
</style>
<div id="myDialog" class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><i class="fa fa-times"></i></button>
<div class="modal-title" id="Dialog_title">Text Document</div>
</div>
<div class="modal-body">
<div id="my_doc_content"></div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal" onclick="rg.closeDialog('myDialog');">Close</button>
</div>
</div>
</div>
</div>
Override the .modal-dialog width
.modal-dialog{
width: 80%;
margin: auto;
}
Place this piece of code in after the bootstrap.min.css to override its default width.
Here is working fiddle
Here is a JSFiddle that may help you: Working JSFiddle
When CSS style is added on the class .modal instead of .modal-dialog, you can then simply change the width of the modal as you want by the following code:
.modal{
width: 80%; /* respsonsive width */
margin-left:-40%; /* width/2) */
}
$(function() {
$("#myDialog").modal("show");
});
.modal-dialog {
width: 80%;
margin: auto;
}
.modal{
width: 80%; /* respsonsive width */
margin-left:-40%; /* width/2) */
}
<div id="myDialog" class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><i class="fa fa-times"></i></button>
<div class="modal-title" id="Dialog_title">Text Document</div>
</div>
<div class="modal-body">
<div id="my_doc_content"></div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal" onclick="rg.closeDialog('myDialog');">Close</button>
</div>
</div>
</div>
</div>
Hope this helps you!
I always suggest to work with non-built files, working with sources you can take benefits from variables, mixins and, when needed, add/modify some default behaviour not explicitly exposed...
so, going to https://github.com/twbs/bootstrap/blob/v4.0.0-alpha.3/scss/_modal.scss you can see where sizes are defined and add/edit them as you need...
// example
#include media-breakpoint-up(lg) {
.modal-extralg { max-width: $modal-lg * 2; }
}
Just use id:
#myDialog {
width:80%;
}
If you using bootstrap 4+, there are default optional sizes,
check this
You can make modal-md, modal-lg in different grid sizes
Example:
div class="modal-dialog modal-xl modal-dialog-centered" role="document"
Hello I am using bootstrap to make my entire website but the only problem I have seen is that my modal does not scale and pushes towards the center which is not displayed on a mobile device.
<div class="modal hide fade" id="player_box" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">x</button>
<h3 id="player_box_header"></h3>
</div>
<div class="modal-body" id="player_box_content">
</div>
<div class="modal-footer">
<div class="pull-left" id="social_share_box"></div>
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
</div>
</div>
I am using default bootstrap CSS so I am not sure why it is not being responsive
try using the #media css3 types to refine based on the device resolution
for example
#media (max-width: 480px) and (max-height: 200px){.modal{ height: 150px}}