模態(tài)框(Modal)是覆蓋在父窗體上的子窗體。通常,目的是顯示來自一個單獨的源的內(nèi)容,可以在不離開父窗體的情況下有一些互動。子窗體可提供信息交互等。
以下實例創(chuàng)建了一個簡單的模態(tài)框效果 :
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap5 實例</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="stylesheet">
<script src="https://cdn.staticfile.org/twitter-bootstrap/5.1.1/js/bootstrap.bundle.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
</head>
<body>
<div class="container mt-3">
<h3>模態(tài)框?qū)嵗?lt;/h3>
<p>點擊按鈕打開模態(tài)框</p>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#myModal">
打開模態(tài)框
</button>
</div>
<!-- 模態(tài)框 -->
<div class="modal" id="myModal">
<div class="modal-dialog">
<div class="modal-content">
<!-- 模態(tài)框頭部 -->
<div class="modal-header">
<h4 class="modal-title">模態(tài)框標題</h4>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<!-- 模態(tài)框內(nèi)容 -->
<div class="modal-body">
模態(tài)框內(nèi)容..
</div>
<!-- 模態(tài)框底部 -->
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-bs-dismiss="modal">關(guān)閉</button>
</div>
</div>
</div>
</div>
</body>
</html>
使用 .fade 類可以設(shè)置模態(tài)框彈出或關(guān)閉的效果:
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap5 實例</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="stylesheet">
<script src="https://cdn.staticfile.org/twitter-bootstrap/5.1.1/js/bootstrap.bundle.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
</head>
<body>
<div class="container mt-3">
<h3>模態(tài)框?qū)嵗?lt;/h3>
<p>點擊按鈕打開模態(tài)框</p>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#myModal">
打開模態(tài)框
</button>
</div>
<!-- 模態(tài)框 -->
<div class="modal fade" id="myModal">
<div class="modal-dialog">
<div class="modal-content">
<!-- 模態(tài)框頭部 -->
<div class="modal-header">
<h4 class="modal-title">模態(tài)框標題</h4>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<!-- 模態(tài)框內(nèi)容 -->
<div class="modal-body">
模態(tài)框內(nèi)容..
</div>
<!-- 模態(tài)框底部 -->
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-bs-dismiss="modal">關(guān)閉</button>
</div>
</div>
</div>
</div>
</body>
</html>
我們可以通過添加 .modal-sm 類來創(chuàng)建一個小模態(tài)框,.modal-lg 類可以創(chuàng)建一個大模態(tài)框。
尺寸類放在 <div>元素的 .modal-dialog 類后 :
小模態(tài)框
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap5 實例</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="stylesheet">
<script src="https://cdn.staticfile.org/twitter-bootstrap/5.1.1/js/bootstrap.bundle.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
</head>
<body>
<div class="container mt-3">
<h3>模態(tài)框?qū)嵗?lt;/h3>
<p>點擊按鈕打開模態(tài)框</p>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#myModal">
打開模態(tài)框
</button>
</div>
<!-- 模態(tài)框 -->
<div class="modal fade" id="myModal">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<!-- 模態(tài)框頭部 -->
<div class="modal-header">
<h4 class="modal-title">模態(tài)框標題</h4>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<!-- 模態(tài)框內(nèi)容 -->
<div class="modal-body">
模態(tài)框內(nèi)容..
</div>
<!-- 模態(tài)框底部 -->
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-bs-dismiss="modal">關(guān)閉</button>
</div>
</div>
</div>
</div>
</body>
</html>
大模態(tài)框
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap5 實例</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="stylesheet">
<script src="https://cdn.staticfile.org/twitter-bootstrap/5.1.1/js/bootstrap.bundle.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
</head>
<body>
<div class="container mt-3">
<h3>模態(tài)框?qū)嵗?lt;/h3>
<p>點擊按鈕打開模態(tài)框</p>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#myModal">
打開模態(tài)框
</button>
</div>
<!-- 模態(tài)框 -->
<div class="modal fade" id="myModal">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<!-- 模態(tài)框頭部 -->
<div class="modal-header">
<h4 class="modal-title">模態(tài)框標題</h4>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<!-- 模態(tài)框內(nèi)容 -->
<div class="modal-body">
模態(tài)框內(nèi)容..
</div>
<!-- 模態(tài)框底部 -->
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-bs-dismiss="modal">關(guān)閉</button>
</div>
</div>
</div>
</div>
</body>
</html>
超大模態(tài)框
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap5 實例</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="stylesheet">
<script src="https://cdn.staticfile.org/twitter-bootstrap/5.1.1/js/bootstrap.bundle.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
</head>
<body>
<div class="container mt-3">
<h3>模態(tài)框?qū)嵗?lt;/h3>
<p>點擊按鈕打開模態(tài)框</p>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#myModal">
打開模態(tài)框
</button>
</div>
<!-- 模態(tài)框 -->
<div class="modal fade" id="myModal">
<div class="modal-dialog modal-xl">
<div class="modal-content">
<!-- 模態(tài)框頭部 -->
<div class="modal-header">
<h4 class="modal-title">模態(tài)框標題</h4>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<!-- 模態(tài)框內(nèi)容 -->
<div class="modal-body">
模態(tài)框內(nèi)容..
</div>
<!-- 模態(tài)框底部 -->
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-bs-dismiss="modal">關(guān)閉</button>
</div>
</div>
</div>
</div>
</body>
</html>
使用 .modal-fullscreen 類可以讓模態(tài)框全屏幕顯示:
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap5 實例</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="stylesheet">
<script src="https://cdn.staticfile.org/twitter-bootstrap/5.1.1/js/bootstrap.bundle.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
</head>
<body>
<div class="container mt-3">
<h3>模態(tài)框?qū)嵗?lt;/h3>
<p>點擊按鈕打開模態(tài)框</p>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#myModal">
打開模態(tài)框
</button>
</div>
<!-- 模態(tài)框 -->
<div class="modal fade" id="myModal">
<div class="modal-dialog modal-fullscreen">
<div class="modal-content">
<!-- 模態(tài)框頭部 -->
<div class="modal-header">
<h4 class="modal-title">模態(tài)框標題</h4>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<!-- 模態(tài)框內(nèi)容 -->
<div class="modal-body">
模態(tài)框內(nèi)容..
</div>
<!-- 模態(tài)框底部 -->
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-bs-dismiss="modal">關(guān)閉</button>
</div>
</div>
</div>
</div>
</body>
</html>
使用 .modal-fullscreen-*-* 類可以控制在什么尺寸下全屏幕顯示:
類 | 描述 |
---|---|
.modal-fullscreen-sm-down
|
576px 以下尺寸全屏幕顯示 |
.modal-fullscreen-md-down
|
768px 以下尺寸全屏幕顯示 |
.modal-fullscreen-lg-down
|
992px 以下尺寸全屏幕顯示 |
.modal-fullscreen-xl-down
|
1200px 以下尺寸全屏幕顯示 |
.modal-fullscreen-xxl-down
|
1400px 以下尺寸全屏幕顯示 |
使用 .modal-dialog-centered 類可以設(shè)置模態(tài)框水平和垂直方向都居中顯示:
<!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="external nofollow" target="_blank" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/js/bootstrap.bundle.min.js" rel="external nofollow" ></script>
</head>
<body>
<div class="container mt-3">
<h3>水平和垂直方向都居中顯示</h3>
<p>點擊按鈕打開模態(tài)框</p>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#myModal">
打開模態(tài)框
</button>
</div>
<!-- 模態(tài)框 -->
<div class="modal" id="myModal">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<!-- 模態(tài)框頭部 -->
<div class="modal-header">
<h4 class="modal-title">模態(tài)框標題</h4>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<!-- 模態(tài)框內(nèi)容 -->
<div class="modal-body">
模態(tài)框內(nèi)容..
</div>
<!-- 模態(tài)框底部 -->
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-bs-dismiss="modal">關(guān)閉</button>
</div>
</div>
</div>
</div>
</body>
</html>
默認情況下模態(tài)框如果包含很多內(nèi)容,頁面會自動生成一個滾動,模態(tài)框隨著頁面的滾動而滾動:
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap5 實例</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="stylesheet">
<script src="https://cdn.staticfile.org/twitter-bootstrap/5.1.1/js/bootstrap.bundle.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
</head>
<body>
<div class="container mt-3">
<h3>模態(tài)框滾動條實例</h3>
<p>點擊按鈕打開模態(tài)框</p>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#myModal">
打開模態(tài)框
</button>
</div>
<!-- 模態(tài)框 -->
<div class="modal" id="myModal">
<div class="modal-dialog">
<div class="modal-content">
<!-- 模態(tài)框頭部 -->
<div class="modal-header">
<h4 class="modal-title">模態(tài)框標題</h4>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<!-- 模態(tài)框內(nèi)容 -->
<div class="modal-body">
模態(tài)框內(nèi)容..<br />
模態(tài)框內(nèi)容..<br />
模態(tài)框內(nèi)容..<br />
模態(tài)框內(nèi)容..<br />
模態(tài)框內(nèi)容..<br />
模態(tài)框內(nèi)容..<br />
模態(tài)框內(nèi)容..<br />
模態(tài)框內(nèi)容..<br />
模態(tài)框內(nèi)容..<br />
模態(tài)框內(nèi)容..<br />
模態(tài)框內(nèi)容..<br />
模態(tài)框內(nèi)容..<br />
模態(tài)框內(nèi)容..<br />
模態(tài)框內(nèi)容..<br />
模態(tài)框內(nèi)容..<br />
模態(tài)框內(nèi)容..<br />
模態(tài)框內(nèi)容..<br />
模態(tài)框內(nèi)容..<br />
模態(tài)框內(nèi)容..<br />
模態(tài)框內(nèi)容..<br />
模態(tài)框內(nèi)容..<br />
模態(tài)框內(nèi)容..<br />
模態(tài)框內(nèi)容..<br />
模態(tài)框內(nèi)容..<br />
</div>
<!-- 模態(tài)框底部 -->
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-bs-dismiss="modal">關(guān)閉</button>
</div>
</div>
</div>
</div>
</body>
</html>
如果我們只想在模態(tài)框里頭設(shè)置一個滾動條,可以使用 .modal-dialog-scrollable 類:
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap5 實例</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="stylesheet">
<script src="https://cdn.staticfile.org/twitter-bootstrap/5.1.1/js/bootstrap.bundle.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
</head>
<body>
<div class="container mt-3">
<h3>模態(tài)框滾動條實例</h3>
<p>點擊按鈕打開模態(tài)框</p>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#myModal">
打開模態(tài)框
</button>
</div>
<!-- 模態(tài)框 -->
<div class="modal" id="myModal">
<div class="modal-dialog modal-dialog-scrollable">
<div class="modal-content">
<!-- 模態(tài)框頭部 -->
<div class="modal-header">
<h4 class="modal-title">模態(tài)框標題</h4>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<!-- 模態(tài)框內(nèi)容 -->
<div class="modal-body">
模態(tài)框內(nèi)容..<br />
模態(tài)框內(nèi)容..<br />
模態(tài)框內(nèi)容..<br />
模態(tài)框內(nèi)容..<br />
模態(tài)框內(nèi)容..<br />
模態(tài)框內(nèi)容..<br />
模態(tài)框內(nèi)容..<br />
模態(tài)框內(nèi)容..<br />
模態(tài)框內(nèi)容..<br />
模態(tài)框內(nèi)容..<br />
模態(tài)框內(nèi)容..<br />
模態(tài)框內(nèi)容..<br />
模態(tài)框內(nèi)容..<br />
模態(tài)框內(nèi)容..<br />
模態(tài)框內(nèi)容..<br />
模態(tài)框內(nèi)容..<br />
模態(tài)框內(nèi)容..<br />
模態(tài)框內(nèi)容..<br />
模態(tài)框內(nèi)容..<br />
模態(tài)框內(nèi)容..<br />
模態(tài)框內(nèi)容..<br />
模態(tài)框內(nèi)容..<br />
模態(tài)框內(nèi)容..<br />
模態(tài)框內(nèi)容..<br />
</div>
<!-- 模態(tài)框底部 -->
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-bs-dismiss="modal">關(guān)閉</button>
</div>
</div>
</div>
</div>
</body>
</html>
更多建議: