如何增加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个回答

3

首先,要成为姐妹类的是: modal-dialog

原生Bootstrap选项可以是:

<div class="modal-dialog modal-sm">

或者

<div class="modal-dialog modal-lg">

或者

<div class="modal-dialog modal-xl">

3

前往 modal-dialog div 并添加

 style="width:70%"

根据你想要的尺寸而定


3
宽度为70%不起作用,但如果设置min-width:70%,则可以正常工作。回答前请进行测试。 - FilT

2

实际上,你是在模态框上应用CSS。

你必须对.modal-dialog应用CSS。

例如,参见以下代码。

<div class="modal" id="myModal">
 <div class="modal-dialog" style="width:xxxpx;"> <!-- Set width of div which you want -->
      <div class="modal-content">
           Lorem Ipsum some text...content 

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

Bootstrap还提供了用于设置div宽度的类。

对于小型模态框,请使用modal-sm

对于大型模态框,请使用modal-lg


2

最简单的方法是:

$(".modal-dialog").css("width", "80%");

我们可以通过百分比来指定模态框的宽度,以适应屏幕大小。

以下是一个可行的示例,以演示相同的效果:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
  <script type="text/javascript">
  $(document).ready(function () {
    $(".modal-dialog").css("width", "90%");
  });
  </script>
</head>
<body>

<div class="container">
  <h2>Modal Example</h2>
  <!-- Trigger the modal with a button -->
  <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>

  <!-- Modal -->
  <div class="modal fade" id="myModal" role="dialog">
    <div class="modal-dialog">
    
      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">Modal Header</h4>
        </div>
        <div class="modal-body">
          <p>Some text in the modal.</p>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>
      
    </div>
  </div>
  
</div>

</body>
</html>


1

默认对话框大小为500像素。如果你想让你的对话框看起来比默认大小大,那么你应该添加class="modal-dialog modal-lg modal-dialog-centered",你会看到差别。


1

对于我而言,使用Bootstrap 5.2.1的方法是:

<div class="modal-dialog MyDialogWideningClass">

并且

.MyDialogWideningClass
{
    --bs-modal-width: 600px;
}

1
我知道这已经过时了,但对于任何寻找的人,现在您需要设置max-width属性。
.modal-1000 {
    max-width: 1000px;
    margin: 30px auto;
}

0
在你的代码中,对于modal-dialog div,添加另一个类,modal-lg:
使用modal-xl。

3
不要抄袭他人的答案。 - Talha Rahman

0

我想这是由于模态框的最大宽度设置为500像素和modal-lg的最大宽度设置为800像素导致的。我认为将其设置为自适应将使您的模态框具有响应性。


0

你可以定义modal-xl,但它的最大宽度仅限于1140像素,因此如果你需要定义更多的宽度,你也可以在modal-dialog div中定义行内样式max-width,就像这样:

<div class="modal-dialog modal-xl" role="document" style="max-width:1400px;">

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