Begin by creating a variable called mod
and store the modal in it;
var mod = $('.modal');
Next, insert a div
<div class="insidemodal"></div>
inside the container where you want to display the modal
Utilize the Bootstrap 4 modal event listener, specifically show.bs.modal
, and place var mod = $('.modal');
within it. Load the modal into the insidemodal
selector when the modal is about to be displayed.
$('#myModal').on('show.bs.modal', function () {
var mod = $('.modal'); //Create variable and load modal in it
$('.insidemodal').html(mod); //Load modal to `insidemodal` Selector
});
Implement the following CSS changes in the Modal
.modal-backdrop {
display: none //<---Disabling the modal backdrop
}
.modal-backdrop.in {
opacity: 0;
}
.modal {
z-index: inherit !important; //<--Overwrite default modal z-index: 1050
position: relative; //<Change position from absolute
}
The final code will look like this
JS
$(document).ready(function () {
$('#myModal').on('show.bs.modal', function () {
var mod = $('.modal');
$('.insidemodal').html(mod);
});
});
CSS
.title {
background: green;
color: #fff;
padding: 5px;
text-align: center;
border: 1px solid;
}
.menu {
background: blue;
color: #fff;
padding: 5px;
text-align: center;
border: 1px solid;
}
.insidemodal {
background: red;
border: 1px solid;
padding: 5px;
}
.modal-backdrop {
display: none;
}
.modal-backdrop.in {
opacity: 0;
}
.modal {
z-index: inherit !important;
position: relative;
}
HTML
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/css/bootstrap.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/js/bootstrap.min.js"></script>
<div class="container">
<div class="row">
<div class="col-sm-12">
<div class="title">This Is Title</div>
</div>
</div>
<div class="row">
<div class="col-sm-4">
<div class="menu">Left Menu
<br>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">Modal</button>
</div>
</div>
<div class="col-sm-8">
<div class="insidemodal"></div>
</div>
</div>
</div>
<!-- Modal -->
<div class="modal fade" id="myModal">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">×</span>
<span class="sr-only">Close</span>
</button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">...</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal" aria-label="Close">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
Access the Working Fiddle Example Here
Note: If there are multiple modals, making the above CSS changes directly in the Modal CSS could affect other modals' behavior. Therefore, it's advisable to use a custom selector instead of altering the default modal selector.