使用Bootstrap Modal 4.3的垂直对齐中心

时间:2019-02-20 20:08:37

标签: css3 bootstrap-4

我试图在身体的模态中心垂直对齐,但没有任何反应。怎么了?我使用的是 4.3版的引导程序。!

<section>
    <div id="search-shopkeeper-modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="shopkeeperModal" aria-hidden="true">
        <div class="modal-dialog modal-xl" role="document">
            <div class="modal-content">
                <div class="modal-header border-0 p-0 m-0">
                    <div class="container-fluid p-2">
                        <form action="">
                            <div class="form-row">
                                <div class="col-12">
                                    <div class="input-group input-group-lg">
                                        <div class="input-group-prepend">
                                            <span id="search-shopkeeper-addon" class="input-group-text bg-transparent border-0"><i class="fas fa-search"></i></span>
                                        </div>
                                        <input type="text" class="form-control rounded-0 border-0 shadow-none" placeholder="Digite o nome do Lojista..." aria-label="Username" aria-describedby="search-shopkeeper-addon">
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
                <div class="modal-body" style="border: 1px red solid">
                    <!-- @todo -->
                    <div class="container d-flex align-items-center" style="border: 1px red solid">
                        <div><h1 class="text-center">SEM RESULTADOS!</h1></div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default text-white shadow-none" data-dismiss="modal" style="background-color: #333333 !important">Fechar</button>
                </div>
            </div>
        </div>
    </div>
</section>

enter image description here

2 个答案:

答案 0 :(得分:1)

您要添加以下规则:

.modal-body {
  display: flex;
  align-items: center;
  justify-content: center;
}

这可以在下面看到:

.modal-content {
  height: 500px; /* Needed for this example to demonstrate the change */
}

.modal-body {
  display: flex;
  align-items: center;
  justify-content: center;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

<section>
  <div id="search-shopkeeper-modal" class="" tabindex="-1" role="dialog" aria-labelledby="shopkeeperModal" aria-hidden="true">
    <div class="modal-dialog modal-xl" role="document">
      <div class="modal-content">
        <div class="modal-header border-0 p-0 m-0">
          <div class="container-fluid p-2">
            <form action="">
              <div class="form-row">
                <div class="col-12">
                  <div class="input-group input-group-lg">
                    <div class="input-group-prepend">
                      <span id="search-shopkeeper-addon" class="input-group-text bg-transparent border-0"><i class="fas fa-search"></i></span>
                    </div>
                    <input type="text" class="form-control rounded-0 border-0 shadow-none" placeholder="Digite o nome do Lojista..." aria-label="Username" aria-describedby="search-shopkeeper-addon">
                  </div>
                </div>
              </div>
            </form>
          </div>
        </div>
        <div class="modal-body" style="border: 1px red solid">
          <!-- @todo -->
          <div class="container d-flex align-items-center" style="border: 1px red solid">
            <div>
              <h1 class="text-center">SEM RESULTADOS!</h1>
            </div>
          </div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default text-white shadow-none" data-dismiss="modal" style="background-color: #333333 !important">Fechar</button>
        </div>
      </div>
    </div>
  </div>
</section>

答案 1 :(得分:1)

只需将“ d-flex align-items-center”类添加到模态主体

p.s这是使用引导程序的选项,但是您可以通过css进行操作,如上面的注释

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#search-shopkeeper-modal">
  Launch demo modal
</button>
<section>
    <div id="search-shopkeeper-modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="shopkeeperModal" aria-hidden="true">
        <div class="modal-dialog modal-xl" role="document">
            <div class="modal-content">
                <div class="modal-header border-0 p-0 m-0">
                    <div class="container-fluid p-2">
                        <form action="">
                            <div class="form-row">
                                <div class="col-12">
                                    <div class="input-group input-group-lg">
                                        <div class="input-group-prepend">
                                            <span id="search-shopkeeper-addon" class="input-group-text bg-transparent border-0"><i class="fas fa-search"></i></span>
                                        </div>
                                        <input type="text" class="form-control rounded-0 border-0 shadow-none" placeholder="Digite o nome do Lojista..." aria-label="Username" aria-describedby="search-shopkeeper-addon">
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
                <div class="modal-body d-flex align-items-center" style="border: 1px red solid; height: 500px;">
                    <!-- @todo -->
                    <div class="container d-flex align-items-center" style="border: 1px red solid">
                        <div><h1 class="text-center">SEM RESULTADOS!</h1></div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default text-white shadow-none" data-dismiss="modal" style="background-color: #333333 !important">Fechar</button>
                </div>
            </div>
        </div>
    </div>
</section>