How to center a Modal Box Horizontally and Vertically with CSS3? Is it possible?

Asked

Viewed 8,851 times

4

Man Modalbox(.modal-fade on Boostrap), is appearing at the top of the page! I can center it on Horizontal and Vertical using only CSS3?

If yes, what would be the right way?

Follow the code of my Modal.

<div class="container">
          <!-- Modal -->
          <div class="modal fade" id="myModal" tabindex="-1" role="dialog" 
                 aria-labelledby="myModalLabel" aria-hidden="true">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <!-- Modal Header -->
                        <div class="modal-header">
                            <button type="button" class="close" 
                               data-dismiss="modal">
                                   <span aria-hidden="true">&times;</span>
                                   <span class="sr-only">Close</span>
                            </button>

                            <h4 class="modal-title" id="myModalLabel">Comunicado Importante!</h4>

                        </div>

                        <!-- Modal Body -->
                        <div class="modal-body">
                            <h5>Preciso centralizar esse ModalBox, usando CSS3!</h5>
                        </div>

                        <!-- Modal Footer -->
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>

1 answer

4


You can do it like this:

.modal {
  text-align: center;
}
@media screen and (min-width: 768px) {
  .modal:before {
    display: inline-block;
    vertical-align: middle;
    content: " ";
    height: 100%;
  }
}
.modal-dialog {
  display: inline-block;
  text-align: left;
  vertical-align: middle;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>


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

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <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"><span aria-hidden="true">&times;</span>
        </button>
        <h4 class="modal-title" id="myModalLabel">Modal Centralizada</h4>
      </div>
      <div class="modal-body">
        conteúdo..
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Sair</button>
        <button type="button" class="btn btn-primary">Salvar</button>
      </div>
    </div>
  </div>
</div>

See Running in: jsfiddle

  • 1

    Gabriel, that’s it, man! It worked first here. I still don’t understand the code. I’ll have to look a little calmly to understand what he did. But that’s it!! It worked; Thank you very much and Big Hug.

  • @Angeloscali later I’ll edit it by putting what each part of css does so that you understand I answered it very quickly because I’m busy at the moment, I’m glad that solved your problem ;)

  • msm thanks if you can do that. It will be another nice help. Abs

  • It helped a lot! Thanks for the tip.

Browser other questions tagged

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