有没有办法限制Bootstrap 5模式对话框和背景的大小?

下面的example占据了example%的屏幕,可以让它只覆盖父main容器吗?

<main class="container-fluid pt-4 px-4" style="height: 400px">
                <!-- Button trigger modal -->
                <button
                    type="button"
                    class="btn btn-primary"
                    data-bs-toggle="modal"
                    data-bs-target="#exampleModal"
                >
                    Launch demo modal
                </button>

                <!-- Modal -->
                <div
                    class="modal fade hide"
                    data-backdrop="false"
                    id="exampleModal"
                    tabindex="-1"
                    aria-labelledby="exampleModalLabel"
                    aria-hidden="true"
                >
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <div class="modal-header">
                                <h5
                                    class="modal-title"
                                    id="exampleModalLabel"
                                >
                                    Modal title
                                </h5>
                                <button
                                    type="button"
                                    class="btn-close"
                                    data-bs-dismiss="modal"
                                    aria-label="Close"
                                ></button>
                            </div>
                            <div class="modal-body">...</div>
                            <div class="modal-footer">
                                <button
                                    type="button"
                                    class="btn btn-secondary"
                                    data-bs-dismiss="modal"
                                >
                                    Close
                                </button>
                                <button
                                    type="button"
                                    class="btn btn-primary"
                                >
                                    Save changes
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
            </main>

我添加了下面的css,但不起作用.

.modal-dialog, .modal-backdrop {
/* supposed to take the height and width of parent container */
       height: 100% !important; 
       width: 100% !important;
 }

How it works: enter image description here

What I want (or expected behavior): enter image description here

推荐答案

The modal component adds a the backdrop element (outside the .modal's parent) automatically, so if you want the modal to be completely contained inside the parent, start by disabling the backdrop with data-bs-backdrop="false".
By default the .modal has position: fixed so it gets rendered relative to the viewport without regards to its parent element(s). This behavior can be overridden by giving the (main) parent position: relative; and give the .modal child position: absolute;. You can also give the .modal an rgba() background-color to simulate an encapsulated, static backdrop within the parent only (clicking on a static backdrop does not close the modal).
To have the modal centered within the parent add .modal-dialog-centered to the .modal-dialog.
In the snippet below I have given both main and .modal a background to more clearly demonstrate:

main {
  position: relative;
  background: #EEE;
}

main .modal {
  position: absolute;
  background: rgba(0, 100, 0, 0.35);
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">

<div class="container-fluid">
  <div class="row">
    <div class="col-2">
      sidebar
    </div>
    <div class="col-10">
      <div class="row">
        <nav class="navbar bg-light">
          <div class="container-fluid">
            <a class="navbar-brand" href="#">Navbar</a>
          </div>
        </nav>
      </div>
      <div class="row">
        <main class="container-fluid pt-4 px-4" style="height: 400px">
          <!-- Button trigger modal -->
          <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal">
            Launch demo modal
          </button>

          <!-- Modal -->
          <div class="modal fade hide" data-bs-backdrop="false" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
            <div class="modal-dialog modal-dialog-centered">
              <div class="modal-content">
                <div class="modal-header">
                  <h5 class="modal-title" id="exampleModalLabel">
                    Modal title
                  </h5>
                  <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body flex-grow-1">...</div>
                <div class="modal-footer">
                  <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
                    Close
                  </button>
                  <button type="button" class="btn btn-primary">
                    Save changes
                  </button>
                </div>
              </div>
            </div>
          </div>
        </main>

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



<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4" crossorigin="anonymous"></script>

Html相关问答推荐

如何防止下拉菜单内的Bootstrap列表行包装?

CSS动画积分计数器

电话号码验证器(Angular )

正文不能填满浏览器100%的高度

Django HTML标记-Merge for Loop with Conditional语句

如何显示自定义按钮以将产品添加到WooCommerce购物车?

如何使链接组件内的内容不重新路由Next.js13

如何使背景图像在面包屑中相互重叠

如何在ASP.NET Core中添加换行符

根据Tailwind CSS中的子计数而非子宽度进行对齐,并水平对齐

如何使用多边形制作如图所示的背景

将箭头图标添加到工具提示包装器时遇到问题

四开将一个 qmd 文档中的单词链接到另一个 qmd 文档中的单词

Tailwind 网格行高度可防止拉伸到最高行的所有相同高度

在Firefox中使用keySplines时,SVG: <animateMotion>不起作用

当作为常规图片输入时,Cloudfront分布式图像可显示,但作为背景图像不显示

CSS:第一个类型的伪类没有按预期工作

如何使用 css 将图像变成黑色并使文本出现在悬停时?

列宽等于最宽列宽度的无界容器

如何阻止网格项目拉伸?