Search code examples
modal-dialogmaterialize

materialize css valign-wrapper not working within modal


I want to create a modal that I can use to present an indeterminate loading bar. The loading bar is the only element within the modal. I want the loading bar to be vertically centered but the valign-wrapper isn't working for this case. How can I get valign-wrapper to work as expected?

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<!-- Compiled and minified CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">


 </head>
 <body>

   <div
      class="modal valign-wrapper"
      id="loading-modal"
      style="width: 50% !important ; height: 50% !important ;"
   >
     <div class="progress valign">
       <div class="indeterminate" style="width: 50%"></div>
     </div>
   </div>


   <!-- Compiled and minified JavaScript -->
   <script
     src="https://code.jquery.com/jquery-3.3.1.js"
     integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="
     crossorigin="anonymous">
   </script>
   <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
   <script>
       $(document).ready(function(){
         $('.modal').modal();
         $('.modal').modal('open');
       });
   </script>
 </body>
</html>

JS Bin: https://jsbin.com/yuzonipexu/edit?html,output


Solution

  • You can put your progress in a wrapper tag and and center that vertically :

    html:

    <div class="progress-wrapper">
      <div class="progress valign">
        <div class="indeterminate" style="width: 50%"></div>
      </div>
    </div> 
    

    css:

    .progress-wrapper {
      position: relative;
      top: 50%;
    }
    
    .progress-wrapper .progress {
      margin: 0;
    }