CSS - 伪元素 - ::backdrop



CSS 中的 ::backdrop 伪元素用于创建完全覆盖视口的背景,并显示或渲染在 <dialog> 或进入全屏模式或位于顶层的元素的正下方。

可以在以下情况下看到此伪元素的效果:

  • 所有以全屏模式显示的元素。

  • 所有在顶层渲染的 <dialog> 元素。

  • 所有在顶层渲染的弹出式元素。

概述

  • 如果多个元素位于顶层,则每个元素都有其自己的 ::backdrop 伪元素。

  • ::backdrop 伪元素能够隐藏、设置样式或完全隐藏顶层元素下方的所有内容。

  • ::backdrop 伪元素不能被任何其他元素继承或继承。

  • 对应用于此伪元素的任何属性或所有属性没有限制。

语法

selector::backdrop { /* ... */ }

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

CSS ::backdrop 示例

这是一个示例,展示了在 <dialog> 元素上使用 ::backdrop 伪元素:

Open Compiler
<html> <head> <style> dialog::backdrop { background-image: url("images/border.png"); } * { box-sizing: border-box; } body { margin: 0; font-family: 'Lucida Sans', 'Lucida Sans Regular', 'Lucida Grande', 'Lucida Sans Unicode', Geneva, Verdana, sans-serif; background-color: #3d3e3e; color: white; } .container { max-width: 100%; margin: 0 auto; padding: 2rem; } button { display: inline-block; border: 1px solid #007bff; padding: 5px; font-size: 1rem; color: black; background-color: #bfc2c5; cursor: pointer; } @supports not selector(::backdrop) { body::before { box-sizing: border-box; content: ''; } } </style> </head> <body> <div class="container"> <p>pseudo-element backdrop used to create a background</p> <button onclick="openDialog()">Click to Open dialog</button> <dialog> <p>See the backdrop</p> <button onclick="closeDialog()">Close</button> </dialog> </div> <script> var dialog = document.querySelector('dialog'); function openDialog() { dialog.showModal(); } function closeDialog() { dialog.close(); } </script> </body> </html>
广告