将bootstrap模态宽度设置为屏幕的百分比并居中

时间:2016-12-22 03:24:50

标签: css twitter-bootstrap modal-dialog

有人可以帮助我将自举模式的宽度设置为屏幕宽度的75%并水平居中吗?到目前为止,我已经尝试过:

  <div class="container">
    <!-- Modal -->
    <div class="modal fade" id="mymodal" tabindex="-1" role="dialog" aria-labelledby="createTemplateModal" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <!--Content-->
            <div class="modal-content">
                <!--Header-->
                <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">My Modal</h4>
                </div>
                <!--Body-->
                <div class="modal-body">
                  <h1>
                  Modal Body
                  </h1>
                <!--Footer-->
                <div class="modal-footer">
                    <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>
  </div>

        $('#mymodal').modal('toggle');
        $('#mymodal').on('shown.bs.modal', function() {
          var width = $(window).width() * 0.75;
          console.log('width is: ' + width);
          $(this).find(".modal-body").css("width", width);
        });

然而,这1)并没有将宽度设置为我的屏幕宽度的75%,2)它使模态体的一部分内容水平溢出。这是我的小提琴:http://jsfiddle.net/dc46o5p9/

提前致谢!

1 个答案:

答案 0 :(得分:4)

以下是摘录。

&#13;
&#13;
$("#mymodal").modal('toggle');
&#13;
.modal-dialog {
  width: 75%;
  margin: 0 auto;
}
&#13;
<link href="http://getbootstrap.com/dist/css/bootstrap.css" rel="stylesheet">
<div class="container">
<!-- Modal -->
    <div class="modal fade" id="mymodal" tabindex="-1" role="dialog" aria-labelledby="createTemplateModal" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <!--Content-->
            <div class="modal-content">
                <!--Header-->
                <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">My Modal</h4>
                </div>
                <!--Body-->
                <div class="modal-body">
                  <h1>
                  Modal Body
                  </h1>
                <!--Footer-->
                <div class="modal-footer">
                    <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="http://getbootstrap.com/dist/js/bootstrap.min.js"></script>
&#13;
&#13;
&#13;