如何更改Twitter Bootstrap模式框的默认宽度?

372 javascript jquery twitter-bootstrap-2

我尝试了以下方法:

   <div class="modal hide fade modal-admin" id="testModal" style="display: none;">
        <div class="modal-header">
          <a data-dismiss="modal" class="close">×</a>
          <h3 id='dialog-heading'></h3>
        </div>
        <div class="modal-body">
            <div id="dialog-data"></div>
        </div>
        <div class="modal-footer">
          <a data-dismiss="modal" class="btn" >Close</a>
          <a class="btn btn-primary" id="btnSaveChanges">Save changes</a>
        </div>
    </div>
Run Code Online (Sandbox Code Playgroud)

这个Javascript:

    $('.modal-admin').css('width', '750px');
    $('.modal-admin').css('margin', '100px auto 100px auto');
    $('.modal-admin').modal('show')
Run Code Online (Sandbox Code Playgroud)

结果不是我的预期.模态左上方位于屏幕中央.

谁能帮我.有没有人试过这个.我认为这不是一件不寻常的事情.

Mar*_*sen 371

更新:

bootstrap 3您需要更改模态对话框.因此,在这种情况下,您可以modal-adminmodal-dialog看台所在的位置添加该类.

原始答案(Bootstrap <3)

是否有某种原因你试图用JS/jQuery改变它?

您可以使用CSS轻松完成,这意味着您无需在文档中进行样式设置.在您自己的自定义CSS文件中,添加:

body .modal {
    /* new custom width */
    width: 560px;
    /* must be half of the width, minus scrollbar on the left (30px) */
    margin-left: -280px;
}
Run Code Online (Sandbox Code Playgroud)

在你的情况下:

body .modal-admin {
    /* new custom width */
    width: 750px;
    /* must be half of the width, minus scrollbar on the left (30px) */
    margin-left: -375px;
}
Run Code Online (Sandbox Code Playgroud)

我将body放在选择器之前的原因是它比默认值具有更高的优先级.这样您就可以将其添加到自定义CSS文件中,而无需担心更新Bootstrap.

  • Bootstrap 3使这更容易.`.modal .modal-dialog {width:800px; }`左侧位置自动计算. (46认同)
  • 这是Bootstrap 3的自定义模式宽度示例 - http://bootply.com/85213 (5认同)
  • 感谢@NickN,向下滚动以获得更好(响应)的答案! (2认同)

Nic*_* N. 269

如果你想让它只用CSS响应,请使用:

.modal.large {
    width: 80%; /* respsonsive width */
    margin-left:-40%; /* width/2) */ 
}
Run Code Online (Sandbox Code Playgroud)

注1:我使用了一个.large类,你也可以在正常情况下这样做.modal

注2:在Bootstrap 3中,可能不再需要负边距 (未经个人确认)

/*Bootstrap 3*/
.modal.large {
     width: 80%;
}
Run Code Online (Sandbox Code Playgroud)

注3:在Bootstrap 3和4中,有一个 modal-lg类.所以这可能就足够了,但是如果你想让它响应,你仍然需要我为Bootstrap 3提供的修复.

在一些应用程序fixed中使用模态,在这种情况下你可以尝试width:80%; left:10%;(公式:左= 100 - 宽度/ 2)

  • Cofiem,使用媒体查询来解决这个问题 (5认同)
  • `modal-lg`也适用于Bootstrap 3,非常有用. (4认同)

arc*_*ree 108

如果您正在使用Bootstrap 3,则需要更改模态对话框div,而不是像接受的答案中所示的模态div.此外,为了保持Bootstrap 3的响应特性,使用媒体查询编写覆盖CSS非常重要,因此模式在较小的设备上将是全宽的.

看到这个JSFiddle来试验不同的宽度.

HTML

<div class="modal fade">
    <div class="modal-dialog custom-class">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h3 class="modal-header-text">Text</h3>
            </div>
            <div class="modal-body">
                This is some text.
            </div>
            <div class="modal-footer">
                This is some text.
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->
Run Code Online (Sandbox Code Playgroud)

CSS

@media screen and (min-width: 768px) {
    .custom-class {
        width: 70%; /* either % (e.g. 60%) or px (400px) */
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 为bootstrap3更正一个.工作没有适应保证金!谢谢 (3认同)

mlu*_*noe 79

如果你想要一些不破坏相对设计的东西,试试这个:

body .modal {
  width: 90%; /* desired relative width */
  left: 5%; /* (100%-width)/2 */
  /* place center */
  margin-left:auto;
  margin-right:auto; 
}
Run Code Online (Sandbox Code Playgroud)

正如@Marco Johannesen所说,选择器之前的"主体"使得它比默认值具有更高的优先级.

  • 当我省略.modal.fade.in规则时,这对我很有用,它将模态一直移动到Bootstrap V2.2.2的底部. (6认同)
  • 我建议只使用`margin-left:auto; margin-right:auto;`而不是`auto`一路走来 (4认同)

Tum*_*Tum 41

在Bootstrap 3+中,更改模式对话框大小的最合适方法是使用size属性.下面是一个例子,注意modal-sm沿着modal-dialog类,指示一个小模态.它可以包含sm小型,md中型和lg大型的值.

<div class="modal fade" id="ww_vergeten" tabindex="-1" role="dialog" aria-labelledby="modal_title" aria-hidden="true">
  <div class="modal-dialog modal-sm"> <!-- property to determine size -->
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title" id="modal_title">Some modal</h4>
      </div>
      <div class="modal-body">

        <!-- modal content -->

      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-primary" id="some_button" data-loading-text="Loading...">Send</button>
      </div>
    </div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

  • 或modal-lg for large,请参阅http://getbootstrap.com/javascript/#modals-sizes (4认同)
  • 这不应该是最好的答案吗?我想最好的答案可以帮助您自定义它,但大多数用例应该由Bootstrap的内置类满足. (3认同)

Dav*_*Sag 34

对于Bootstrap 3这里是如何做到这一点.

modal-wide在HTML标记中添加样式(根据Bootstrap 3文档中的示例改编)

<div class="modal fade modal-wide" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 id="myModalLabel" class="modal-title">Modal title</h4>
      </div>
      <div class="modal-body">
        <p>One fine body&hellip;</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->
Run Code Online (Sandbox Code Playgroud)

并添加以下CSS

.modal-wide .modal-dialog {
  width: 80%; /* or whatever you wish */
}
Run Code Online (Sandbox Code Playgroud)

没有必要覆盖margin-leftBootstrap 3得到这个现在被居中.

  • 为了避免任何混淆,没有技术上的理由来添加`modal-wide`类,这意味着它不是Bootstrap"内部"类.你可以简单地做`#myModal .modal-dialog {width:80%; }` (3认同)
  • 是的,我明白了.很多时候,引导类和自定义类之间的区别很难,所以我想记下它. (2认同)

Alu*_*tha 20

Bootstrap 3中,您只需要这个

<style>
    .modal .modal-dialog { width: 80%; }
</style>
Run Code Online (Sandbox Code Playgroud)

以上大多数答案对我没有用!!!

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />

<style>
  #myModal1 .modal-dialog {
    width: 80%;
  }
  #myModal2 .modal-dialog {
    width: 50%;
  }
</style>

<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal1">
  80%
</button>

<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal2">
  50%
</button>
<center>
  <!-- Modal -->
  <div class="modal fade" id="myModal1" 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 title</h4>
        </div>
        <div class="modal-body">
          custom width : 80%
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
          <button type="button" class="btn btn-primary">Save changes</button>
        </div>
      </div>
    </div>
  </div>


  <!-- Modal -->
  <div class="modal fade" id="myModal2" 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 title</h4>
        </div>
        <div class="modal-body">
          custom width : 50%
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
          <button type="button" class="btn btn-primary">Save changes</button>
        </div>
      </div>
    </div>
  </div>

</center>
Run Code Online (Sandbox Code Playgroud)


Rom*_*man 17

解决方案来自页面

$('#feedback-modal').modal({
    backdrop: true,
    keyboard: true
}).css({
    width: 'auto',
    'margin-left': function () {
        return -($(this).width() / 2);
    }
});
Run Code Online (Sandbox Code Playgroud)


Dav*_*ave 15

在Bootstrap的第3版中,有一种简单的方法可以使模态更大.只需在modal-dialog旁边添加类modal-lg即可.

<!-- My Modal Popup -->
<div id="MyWidePopup" class="modal fade" role="dialog">
    <div class="modal-dialog modal-lg"> <---------------------RIGHT HERE!!
        <!-- Modal content-->
        <div class="modal-content">
Run Code Online (Sandbox Code Playgroud)


小智 13

Bootstrap 3:为了维护小型和小型设备的响应功能,我做了以下工作:

@media (min-width: 768px) {
.modal-dialog-wide
{ width: 750px;/* your width */ }
}
Run Code Online (Sandbox Code Playgroud)


小智 7

这就是我所做的,在我的custom.css中我添加了这些行,就是这样.

.modal-lg {
    width: 600px!important;
    margin: 0 auto;
}
Run Code Online (Sandbox Code Playgroud)

显然,您可以更改宽度的大小.


小智 7

如果Bootstrap> 3,只需为您的模态添加样式.

<div class="modal-dialog" style="width:80%;">
  <div class="modal-content">
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)


小智 6

我发现这个解决方案对我来说效果更好.你可以用这个:

$('.modal').css({
  'width': function () { 
    return ($(document).width() * .9) + 'px';  
  },
  'margin-left': function () { 
    return -($(this).width() / 2); 
  }
});
Run Code Online (Sandbox Code Playgroud)

或者这取决于您的要求:

$('.modal').css({
  width: 'auto',
  'margin-left': function () {
     return -($(this).width() / 2);
  }
});
Run Code Online (Sandbox Code Playgroud)

请参阅我发现的帖子:https: //github.com/twitter/bootstrap/issues/675


Gav*_*vin 5

FYI Bootstrap 3自动处理左侧属性.因此,只需添加此CSS将更改宽度并使其保持居中:

.modal .modal-dialog { width: 800px; }
Run Code Online (Sandbox Code Playgroud)