如何增加Bootstrap模态框的宽度?

164

我尝试过这个,但是出了点问题

以下是CSS代码

body .modal-ku {
width: 750px;
}

这是失败的结果

输入图像描述


4
在Bootstrap中,您可以使用modal-sm和modal-lg来控制模态框的大小。modal-sm用于小尺寸,modal-lg用于大尺寸。 - Leo the lion
你能分享一下你具体的HTML和CSS代码吗?有些样式可能被覆盖了。 - Nikhil Batra
你能分享你的完整代码吗?或者你可以看看这个例子,在这个模型中宽度是可变的。http://www.w3schools.com/bootstrap/tryit.asp?filename=trybs_modal&stacked=h - Har devgun
1
@Hardevgun 请解释一下在这个例子中哪里可以更改模态框的大小?那只是一个简单的演示,固定大小的模态框。 - ProfK
22个回答

349
在您的代码中,对于 modal-dialog div,请添加另一个类 modal-lg
<div class="modal-dialog modal-lg">

如果您想让模态对话框居中,请使用以下方法:

.modal-ku {
  width: 750px;
  margin: auto;
}

2
@WebNoob13 很好,谢谢。但是你的问题可能完全不同。这个答案是5年前写的,可能对你的情况甚至都不适用。请仔细考虑。 - Praveen Kumar Purushothaman
10
“modal-lg”对我有所帮助。您也可以使用“modal-xl”来创建更宽的模态框。 - takanuva15
@takanuva15 感谢您的留言。但是它是否适用于Bootstrap 3?我猜它是在较新版本的Bootstrap中添加的 - 我猜是4。 - Praveen Kumar Purushothaman
@PraveenKumarPurushothaman 确实可能无法与Bootstrap 3一起使用,但这是我在搜索此问题时着陆的第一个结果,因此其他人可能会发现它有用(特别是因为更多人将使用更新的Bootstrap)。 - takanuva15
1
这个答案在7.5年后仍然帮助了某人。感谢@PraveenKumarPurushothaman。 - Vivek K.
是的,它与Bootstrap 3.3.7完全兼容。 - andreszs

57

最简单的方法可能是在 modal-dialog div 上使用内联样式:

<div class="modal" id="myModal">
   <div class="modal-dialog" style="width:1250px;">
      <div class="modal-content">
            ...

        </div>
     </div>
 </div>

42
对于“响应式设计”这样的事情,为什么会有那么多的抱怨呢? - Abhinav Singh
@Abhinav Singh:你是对的。硬编码像素值并不推荐。建议使用百分比或计算值。 - Thinking
13
响应式设计:宽度:100%;最大宽度:1250像素。 - clamchoda

46
你可以在modal-lgmodal-xl类之间进行选择,或者如果你想要自定义宽度,那么可以在内联CSS中设置max-width属性。例如,
<div class="modal-dialog modal-xl" role="document">

或者
<div class="modal-dialog" style="max-width: 80%;" role="document">

'max-width'是秘密。小心百分号。这将导致对话框根据您的网页宽度进行手风琴式展开。 - Trevy Burgess

20

在我的情况下,

  1. 给模态框一个固定的宽度会影响其响应性能。
  2. modal-lg 类的宽度不够宽。

因此解决方案是

@media (min-width: 1200px) {
   .modal-xlg {
      width: 90%; 
   }
}

使用上述类别,而不是 modal-lg 类别。


请问是哪个先前的类? - John Max
@JohnMax 他的意思是“使用上面的类”。 - Mr Heelis
这似乎不适用于4.5.1,而且已经有一个内置的modal-xl类。 - Pedro Lobito
@JohnMax 在被接受的答案中,即 https://dev59.com/C1sX5IYBdhLWcg3wRdx_#34014955 - Zeeshanef
@Zeeshanef,为什么你要参考另一个答案?我不是那个意思,我的意思是我在我的答案中提到的类,而不是另一个答案中的类。 - Hakan Fıstık

13

当我将模态框的宽度增加时,我也遇到了同样的问题,即模态框没有居中显示。 经过一番尝试,我找到了以下解决方案。

.modal-dialog {
    max-width: 850px;
    margin: 2rem auto;
}

如果这对你有用,请点赞。编码愉快!


10

在Bootstrap 3中,您需要更改modal-dialog。因此,在这种情况下,您可以在modal-dialog所在的位置添加类modal-admin。

@media (min-width: 768px) {
  .modal-dialog {
    width: 600;
    margin: 30px auto;
  }
  .modal-content {
    -webkit-box-shadow: 0 5px 15px rgba(0, 0, 0, .5);
            box-shadow: 0 5px 15px rgba(0, 0, 0, .5);
  }
  .modal-sm {
    width: 300px;
  }
}

6
在Bootstrap中,modal-dialog的默认宽度为600px。但您可以明确地更改其宽度。例如,如果您想将它的宽度最大化到您选择的值,例如800px,则可以按照以下步骤进行操作:
.modal-dialog {
    width: 800px;
    margin: 30px auto;
}

注意:此更改适用于您在应用程序中使用的所有模态对话框。同时,我的建议是最好定义自己的CSS规则,而不要触及框架的核心。

如果您只想将其设置为特定的模态对话框,请使用自己的引人注目的类名作为modal-800,其宽度设置为800px,如下所示:

HTML

<div class="modal">
   <div class="modal-dialog modal-800">
      <div class="modal-content">

      </div>
   </div>
</div>

CSS

.modal-dialog.modal-800 {
    width: 800px;
    margin: 30px auto;
}

同样的,您可以按照宽度大小来命名类名,如 modal-700,其宽度为 700px
希望这对您有所帮助!

6

我有一个需要在模态框中显示的大网格,仅在body上应用宽度并不能正确工作,因为表格虽然具有bootstrap类,但仍会溢出。最终我在modal-bodymodal-content上应用了相同的宽度:

<!--begin::Modal-->
<div class="modal fade" role="dialog" aria-labelledby="" aria-hidden="true">
    <div class="modal-dialog modal-lg modal-dialog-centered" role="document">
        <div class="modal-content" style="width:980px;">
            <div class="modal-header">
                <h5 class="modal-title" id="">Title</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true" class="la la-remove"></span>
                </button>
            </div>
            <form class="m-form m-form--fit m-form--label-align-right">
                <div class="modal-body" style="width:980px;">
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-brand m-btn" data-dismiss="modal">Close</button>

                </div>
            </form>
        </div>
    </div>
</div>

<!--end::Modal-->

5

如果您想保持模态响应,请使用宽度的%而不是像素。您可以在行内(如下所示)或使用CSS进行操作。

<div class="modal fade" id="phpModal" role="dialog">
            <div class="modal-dialog modal-lg" style="width:80%;">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal">&times;</button>
                        <h4 class="modal-title">HERE YOU TITLE</h4>
                    </div>
                    <div class="modal-body helpModal phpModal">
                        HERE YOUR CONTENT
                    </div>
                </div>
            </div>
        </div>

如果您使用CSS,甚至可以针对modal-sm和modal-lg设置不同的百分比。

5

2021年9月Bootstrap 5支持的尺寸

<div class="modal-dialog modal-xl">...</div>
<div class="modal-dialog modal-lg">...</div>
<div class="modal-dialog modal-sm">...</div>

2
“modal-xl” 似乎无法正常工作。 - NicLovin
在 Bootstrap 4 中没有 modal-xl。请仔细检查是 Bootstrap 4 还是 5。 - CodeToLife

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接