CSS ::backdrop
The ::backdrop CSS pseudo-element is used to style the backdrop of an HTML element when that element is displayed in a modal or fullscreen context. It allows developers to create visually appealing overlays that can enhance the user experience by providing an appropriate background when a modal dialog or fullscreen element is active.
In this tutorial, we will use a fictional example of a modal window that appears when a user clicks a button. The modal will have a backdrop that dims the rest of the page while the modal is open.
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
left: 0;
top: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: auto; /* Enable scroll if needed */
background-color: rgba(0, 0, 0, 0.6); /* Black w/ opacity */
}
.modal-content {
background-color: white;
margin: 15% auto; /* 15% from the top and centered */
padding: 20px;
border: 1px solid #888;
width: 80%; /* Could be more or less, depending on screen size */
}
/* Styling the backdrop */
.modal:target::backdrop {
background-color: rgba(0, 0, 0, 0.8); /* Darken the backdrop */
}
</style>