模態(tài)框(Modal)是覆蓋在父窗體上的子窗體。通常,使用模態(tài)框的目的是用來(lái)顯示來(lái)自一個(gè)單獨(dú)的源的內(nèi)容,而且可以在不離開父窗體的情況下有一些互動(dòng)。
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap4 實(shí)例</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" >
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
<script src="https://cdn.bootcss.com/popper.js/1.12.5/umd/popper.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
<script src="https://cdn.bootcss.com/bootstrap/4.1.0/js/bootstrap.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
</head>
<body>
<div class="container">
<h2>模態(tài)框?qū)嵗?lt;/h2>
<!-- 按鈕:用于打開模態(tài)框 -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
打開模態(tài)框
</button>
<!-- 模態(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="close" data-dismiss="modal">×</button>
</div>
<!-- 模態(tài)框主體 -->
<div class="modal-body">
模態(tài)框內(nèi)容..
</div>
<!-- 模態(tài)框底部 -->
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">關(guān)閉</button>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
我們可以通過(guò)添加 .modal-sm 類來(lái)創(chuàng)建一個(gè)小模態(tài)框,.modal-lg 類可以創(chuàng)建一個(gè)大模態(tài)框。
尺寸類都放在 <div>元素的 .modal-dialog 類后 :
實(shí)例:實(shí)例 - 大模態(tài)框
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap4 實(shí)例</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" >
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
<script src="https://cdn.bootcss.com/popper.js/1.12.5/umd/popper.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
<script src="https://cdn.bootcss.com/bootstrap/4.1.0/js/bootstrap.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
</head>
<body>
<div class="container">
<h2>模態(tài)框?qū)嵗?lt;/h2>
<!-- 按鈕:用于打開模態(tài)框 -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
打開模態(tài)框
</button>
<!-- 模態(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="close" data-dismiss="modal">×</button>
</div>
<!-- 模態(tài)框主體 -->
<div class="modal-body">
模態(tài)框內(nèi)容..
</div>
<!-- 模態(tài)框底部 -->
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">關(guān)閉</button>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
實(shí)例 - 大模態(tài)框
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap4 實(shí)例</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" >
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
<script src="https://cdn.bootcss.com/popper.js/1.12.5/umd/popper.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
<script src="https://cdn.bootcss.com/bootstrap/4.1.0/js/bootstrap.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
</head>
<body>
<div class="container">
<h2>模態(tài)框?qū)嵗?lt;/h2>
<!-- 按鈕:用于打開模態(tài)框 -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
打開模態(tài)框
</button>
<!-- 模態(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="close" data-dismiss="modal">×</button>
</div>
<!-- 模態(tài)框主體 -->
<div class="modal-body">
模態(tài)框內(nèi)容..
</div>
<!-- 模態(tài)框底部 -->
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">關(guān)閉</button>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
更多建議: