:modal
:modal CSS 伪类匹配处于某种状态的元素,在该状态下,它会阻止所有与自身外部元素的交互,直到该交互被解除。:modal 伪类可以同时选择多个元素,但其中只有一个会处于活动状态并能够接收输入。
试一试
button {
display: block;
margin: auto;
width: 10rem;
height: 2rem;
}
:modal {
background-color: beige;
border: 2px solid burlywood;
border-radius: 5px;
}
p {
color: black;
}
<p>Would you like to see a new random number?</p>
<button id="showNumber">Show me</button>
<dialog id="favDialog">
<form method="dialog">
<p>Lucky number is: <strong id="number"></strong></p>
<button>Close dialog</button>
</form>
</dialog>
const showNumber = document.getElementById("showNumber");
const favDialog = document.getElementById("favDialog");
const number = document.getElementById("number");
showNumber.addEventListener("click", () => {
number.innerText = Math.floor(Math.random() * 1000);
favDialog.showModal();
});
语法
css
:modal {
/* ... */
}
用法说明
会阻止用户与页面其余部分交互并会被 :modal 伪类选中的元素包括:
- 使用
showModal()API 打开的dialog元素。 - 使用
requestFullscreen()API 打开时,由:fullscreen伪类选中的元素。
示例
模态对话框样式
此示例为当“更新详细信息”按钮被激活时打开的模态对话框设置样式。此示例是基于 <dialog> 元素示例构建的。
CSS
css
:modal {
border: 5px solid red;
background-color: yellow;
box-shadow: 3px 3px 10px rgb(0 0 0 / 50%);
}
结果
规范
| 规范 |
|---|
| HTML # 选择器-modal |
| 选择器 Level 4 # 选择器定义-modal |
浏览器兼容性
加载中…
另见
dialog元素- 其他元素显示状态伪类:
:fullscreen和:picture-in-picture - 伪类完整列表