Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

Bootstrap 4 Modal


Bootstrap 4 模态框

模态框组件是一个对话框/弹出窗口,它显示在当前页面的顶部


如何创建模态框

以下示例展示了如何创建一个基本的模态框

示例

<!-- 打开模态框的按钮 -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
  打开模态框
</button>

<!-- 模态框 -->
<div class="modal" id="myModal">
  <div class="modal-dialog">
    <div class="modal-content">

      <!-- 模态框标题 -->
      <div class="modal-header">
        <h4 class="modal-title">模态框标题</h4>
        <button type="button" class="close" data-dismiss="modal">&times;</button>
      </div>

      <!-- 模态框主体 -->
      <div class="modal-body">
        模态框主体..
      </div>

      <!-- 模态框页脚 -->
      <div class="modal-footer">
        <button type="button" class="btn btn-danger" data-dismiss="modal">关闭</button>
      </div>

    </div>
  </div>
</div>
自己试试 »

添加动画

使用 .fade 类来添加打开和关闭模态框时的淡入淡出效果

示例

<!-- 淡入淡出模态框 -->
<div class="modal fade"></div>

<!-- 没有动画的模态框 -->
<div class="modal"></div>
自己试试 »

模态框大小

通过添加 .modal-sm 类(用于小型模态框)、.modal-lg 类(用于大型模态框)或 .modal-xl 类(用于特大型模态框)来更改模态框的大小。

将大小类添加到具有 .modal-dialog 类的 <div> 元素

小型模态框

<div class="modal-dialog modal-sm">
自己试试 »

大型模态框

<div class="modal-dialog modal-lg">
自己试试 »

特大型模态框

<div class="modal-dialog modal-xl">
自己试试 »

默认情况下,模态框的大小为“中”。


居中模态框

使用 .modal-dialog-centered 类将模态框垂直和水平居中在页面内

示例

<div class="modal-dialog modal-dialog-centered">
自己试试 »

滚动模态框

当模态框内有大量内容时,页面上会添加一个滚动条。请参阅以下示例以了解其工作原理

示例

<div class="modal-dialog">
自己试试 »

但是,可以通过在 .modal-dialog 中添加 .modal-dialog-scrollable 来使模态框内滚动,而不是页面本身滚动

示例

<div class="modal-dialog modal-dialog-scrollable">
自己试试 »

完整的 Bootstrap 模态框参考

有关所有模态框选项、方法和事件的完整参考,请访问我们的 Bootstrap JS 模态框参考


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.