How to adjust the size of a generic Modal Bootstrap window according to the desired View

Asked

Viewed 1,895 times

1

I have a generic bootstrap modal window that will open several views. The problem is that each of them will have a different screen size and currently, my settings remain fixed(style="width: 800px; height: 500px;")... Is there any way I can change both parts of the code to meet this requirement?

(Generic Modal where all Views are open)

<div class="modal fade modal-primary" id="modalGenerica" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" data-backdrop="static">
    <div class="modal-dialog modal-dialog-centered" style="width: 800px; height: 500px;">
        <div class="modal-content">
            <div id="contentModal"></div>
        </div>
    </div>
</div>

(Example of a view with parts of the Modal structure)

@model Retaguarda.Application.ViewModels.CustomerViewModel
@{
    ViewData["Title"] = "Register new Customer";
    Layout = null;
}
<div>
    <form asp-action="Create">
        @Html.AntiForgeryToken()
        
            <div class="modal-shadow">
                <div class="modal-header modal-header-primary">
                    <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
                    <h4 class="modal-title text-center"> @ViewData["Title"] </h4>
                </div>
                <div class="form-horizontal">
                    <vc:summary />
                    <div class="panel-body">
                        <div class="form-group">
                            <label asp-for="Name" class="col-md-2 control-label"></label>
                            <div class="col-md-10">
                                <input asp-for="Name" class="form-control" />
                                <span asp-validation-for="Name" class="text-danger"></span>
                            </div>
                        </div>
                        <div class="form-group">
                            <label asp-for="Email" class="col-md-2 control-label"></label>
                            <div class="col-md-10">
                                <input asp-for="Email" class="form-control" />
                                <span asp-validation-for="Email" class="text-danger"></span>
                            </div>
                        </div>
                        <div class="form-group">
                            <label asp-for="BirthDate" class="col-md-2 control-label"></label>
                            <div class="col-md-10">
                                <input asp-for="BirthDate" class="form-control" />
                                <span asp-validation-for="BirthDate" class="text-danger"></span>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-md-4">
                                <select data-plugin="selectpicker" id="mySelect2" data-style="btn-primary">
                                    <option>Mustard</option>
                                    <option>Ketchup</option>
                                    <option>Relish</option>
                                </select>
                            </div>
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-md-offset-2 col-md-10">
                            <input type="submit" value="Create" class="btn btn-success" />
                            <a asp-action="Index" class="btn btn-info">Back to List</a>
                        </div>
                    </div>
                </div>

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



<script src="~/lib/jquery-validation/dist/jquery.validate.js"></script>
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js"></script>
<script>
    //Resolve problema de exibição no Modal
    $('#mySelect2').selectpicker({
        dropdownParent: $('#modalGenerica')
    });
</script>

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

inserir a descrição da imagem aqui

  • The modal occupies the whole view?

  • tah ok. Let’s say the view has 1366x768... what would be the size of the modal?

  • I updated the Post with the image of the modal according to the code I quoted. There are views that will have more fields and will need a size that occupies twice or more space...

  • If it is larger than the view height, you will have scroll bar?

  • I wasn’t thinking about this resolution detail... it’s just that I don’t know very well about this subject... The bad thing is that it depends on the screen resolution of each pc you access...

  • Yes... With Scroll Bar...

Show 1 more comment

1 answer

0


You can use a modal with view-based responsive dimensions (see comments in CSS). Example:

.modal-content{
   height: 60%; /*altura da modal*/
}

.modal-dialog{
   max-width: 50%; /*largura da modal*/
   height: 100%; /*altura da view da modal (não alterar)*/
   margin: 0 auto !important; /*centralizar modal horizontalmente*/
}

#contentModal{
    overflow: auto; /*habilita o overflow no corpo da modal*/
}

#modalGenerica{
   overflow: hidden; /*evitar overflow da view da modal*/
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">

<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#modalGenerica">
  Abrir modal
</button>

<!-- Modal -->
<div class="modal fade modal-primary" id="modalGenerica" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" data-backdrop="static">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div id="contentModal">
               Conteúdo da modal
            </div>
        </div>
    </div>
</div>

  • Thanks @dvd!!!!! I got your suggestion. Regarding the cited css, I can create it in a *.css file and then refer it to my page that will work right?

  • Yes, it’s possible. But you can also put it right on the Divs if you want to.

  • I just got the... It is that the system will be quite large with many screens and to not have to move one by one I prefer to centralize this information in a place, because the day that needs to move, just configure once... Thanks for the @dvd strength!!! A hug!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.