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

IT技术 javascript jquery twitter-bootstrap-2
2021-03-09 03:24:23

我尝试了以下方法:

   <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>

而这个 Javascript:

    $('.modal-admin').css('width', '750px');
    $('.modal-admin').css('margin', '100px auto 100px auto');
    $('.modal-admin').modal('show')

结果不是我所期望的。模态左上角位于屏幕中央。

谁能帮我。有没有其他人试过这个。我想这不是一件不寻常的事情。

6个回答

更新:

bootstrap 3你需要改变模式,对话框。所以在这种情况下,您可以modal-adminmodal-dialog站立的地方添加类

原始答案(引导程序 < 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;
}

在你的情况下:

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

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

感谢@NickN,向下滚动以获得更好的(响应式)答案!
2021-05-08 03:24:23
@尼克。杀手!谢谢尼克
2021-05-10 03:24:23
实际上,这对我不起作用。@ZimSystem 在评论中给出的解决方案做到了。
2021-05-10 03:24:23
@FloatingRock 你可以让它响应,看看我的回答
2021-05-12 03:24:23
Bootstrap 3 使这更容易。.modal .modal-dialog { width: 800px; }左边的位置是自动计算的。
2021-05-12 03:24:23

如果您只想使用 CSS 使其响应,请使用以下命令:

.modal.large {
    width: 80%; /* respsonsive width */
    margin-left:-40%; /* width/2) */ 
}

注 1:我使用了一个.large类,您也可以在正常情况下执行此操作.modal

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

/*Bootstrap 3*/
.modal.large {
     width: 80%;
}

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

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

@尼克。我的媒体查询没有解决它。你能添加一个例子吗?无视,我懂了。我有最大宽度而不是最小宽度。 @media (min-width: 400px) { body .modal-lrg{ width: 80%; /* respsonsive width */ margin-left:-40%; /* width/2) */ } }
2021-04-17 03:24:23
Cofiem,使用媒体查询来解决这个问题
2021-05-02 03:24:23
我发现仅使用 % 并不能像使用已内置到引导程序中的列那样提供足够的控制。
2021-05-08 03:24:23
@Alex 我认为它为您提供了更多控制权,您将如何处理现有列?
2021-05-14 03:24:23
这似乎导致模态在非常窄的屏幕上离屏幕一半。
2021-05-15 03:24:23

如果您使用的是 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 -->

CSS

@media screen and (min-width: 768px) {
    .custom-class {
        width: 70%; /* either % (e.g. 60%) or px (400px) */
    }
}
它有效,但没有像 mlunoe 或 Dave 的回答那样响应。
2021-04-20 03:24:23
我删除了答案中的 id 属性,因为它不是必需的。
2021-04-22 03:24:23
正确的 bootstrap3。无需调整余量即可工作!谢谢
2021-04-29 03:24:23

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

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

正如@Marco Johannesen 所说,选择器之前的“主体”使其具有比默认值更高的优先级。

我建议只使用margin-left:auto; margin-right:auto;,而不是auto一路走去
2021-04-16 03:24:23
这适用于 ANGULAR-UI 模态示例...只需将代码放入 css 文件并加载到页面中... angular-ui.github.io/bootstrap/#/modal ... 在plnkr.co运行示例/edit/GYmc9s?p=预览
2021-04-16 03:24:23
当我省略 .modal.fade.in 规则时,这对我很有效,该规则将模态一直移动到 Bootstrap V2.2.2 的底部。
2021-04-22 03:24:23
在某些情况下可能需要top: 30%,百分比取决于里面的内容。
2021-04-29 03:24:23
太好了,此解决方案适用于 Internet Explorer 8、9、10 中的 2.3.2 bootstrap 和 MAIN!!!!带有 margin-left:-40% 的解决方案在 Internet Explorer 中是不正确的!!!
2021-05-07 03:24:23

在 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>
将其添加到我的答案中:)
2021-04-15 03:24:23
这不应该是最佳答案吗?我想最佳答案可以帮助您自定义它,但 Bootstrap 的内置类应该可以满足大多数用例。
2021-05-05 03:24:23
我正在使用 Bootstrap 3.x,但我似乎没有modal-md上课。
2021-05-12 03:24:23