模態(tài)框(Modal)是覆蓋在父窗體上的子窗體。通常,目的是顯示來(lái)自一個(gè)單獨(dú)的源的內(nèi)容,可以在不離開(kāi)父窗體的情況下有一些互動(dòng)。子窗體可提供信息交互等。
以下實(shí)例創(chuàng)建了一個(gè)簡(jiǎn)單的模態(tài)框效果 :
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap5 實(shí)例</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>點(diǎn)擊按鈕打開(kāi)模態(tài)框</p>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#myModal">
打開(kāi)模態(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)框標(biāo)題</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 類(lèi)可以設(shè)置模態(tài)框彈出或關(guān)閉的效果:
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap5 實(shí)例</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>點(diǎn)擊按鈕打開(kāi)模態(tài)框</p>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#myModal">
打開(kāi)模態(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)框標(biāo)題</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>
我們可以通過(guò)添加 .modal-sm 類(lèi)來(lái)創(chuàng)建一個(gè)小模態(tài)框,.modal-lg 類(lèi)可以創(chuàng)建一個(gè)大模態(tài)框。
尺寸類(lèi)放在 <div>元素的 .modal-dialog 類(lèi)后 :
小模態(tài)框
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap5 實(shí)例</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>點(diǎn)擊按鈕打開(kāi)模態(tài)框</p>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#myModal">
打開(kāi)模態(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)框標(biāo)題</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 實(shí)例</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>點(diǎn)擊按鈕打開(kāi)模態(tài)框</p>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#myModal">
打開(kāi)模態(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)框標(biāo)題</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 實(shí)例</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>點(diǎn)擊按鈕打開(kāi)模態(tài)框</p>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#myModal">
打開(kāi)模態(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)框標(biāo)題</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 類(lèi)可以讓模態(tài)框全屏幕顯示:
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap5 實(shí)例</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>點(diǎn)擊按鈕打開(kāi)模態(tài)框</p>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#myModal">
打開(kāi)模態(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)框標(biāo)題</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-*-* 類(lèi)可以控制在什么尺寸下全屏幕顯示:
類(lèi) | 描述 |
---|---|
.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 類(lèi)可以設(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>點(diǎn)擊按鈕打開(kāi)模態(tài)框</p>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#myModal">
打開(kāi)模態(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)框標(biāo)題</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>
默認(rèn)情況下模態(tài)框如果包含很多內(nèi)容,頁(yè)面會(huì)自動(dòng)生成一個(gè)滾動(dòng),模態(tài)框隨著頁(yè)面的滾動(dòng)而滾動(dòng):
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap5 實(shí)例</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)框滾動(dòng)條實(shí)例</h3>
<p>點(diǎn)擊按鈕打開(kāi)模態(tài)框</p>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#myModal">
打開(kāi)模態(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)框標(biāo)題</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è)置一個(gè)滾動(dòng)條,可以使用 .modal-dialog-scrollable 類(lèi):
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap5 實(shí)例</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)框滾動(dòng)條實(shí)例</h3>
<p>點(diǎn)擊按鈕打開(kāi)模態(tài)框</p>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#myModal">
打開(kāi)模態(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)框標(biāo)題</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>
更多建議: