Welcome to this exciting CSS tutorial where we'll learn how to create a modal or popup! By the end of this guide, you'll have a practical understanding of modals and be able to add this essential feature to your own web projects.
A modal or popup is a window that appears on top of the web page, usually used to display additional information, form, or prompt the user for some interaction.
Here's the basic HTML structure for our modal:
<div id="myModal" class="modal">
<!-- Modal content -->
<div class="modal-content">
<span class="close">×</span>
<p>Some text in the Modal.</p>
</div>
</div>#myModal is the modal container..modal-content contains the actual content of the modal..close is a button to close the modal.Now let's style our modal using CSS:
.modal {
display: none; /* Hide the modal by default */
position: fixed; /* Modal must stay in place */
z-index: 1; /* Modal must be above other elements */
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto; /* Enable scrolling if the content is too long */
background-color: rgb(0,0,0); /* Black background */
background-color: rgba(0,0,0,0.4); /* Semi-transparent black background */
}
.modal-content {
background-color: #fefefe;
margin: 15% auto; /* 15% from the top and centered */
padding: 20px;
border: 1px solid #888;
width: 80%; /* Adjust the width as needed */
}
.close {
position: absolute;
top: 15px;
right: 35px;
color: #000;
font-size: 35px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: red;
cursor: pointer;
}Now, you should see a semi-transparent black modal box covering the entire page, with a white content box in the center and a close button on the top right.
Here's an example of a modal with form input:
<div id="myModal" class="modal">
<!-- Modal content -->
<div class="modal-content">
<span class="close">×</span>
<form>
<div class="modal-body">
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="exampleInputPassword1">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary">Submit</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</form>
</div>
</div>.modal-body {
max-height: 300px; /* Limit the height of the modal content */
overflow-y: auto; /* Enable vertical scrolling if the content is too long */
}
.form-group {
margin-bottom: 15px;
}This example includes a login form inside the modal. The form includes two input fields for email and password, as well as submit and close buttons.
What does the `z-index` property do in CSS?
We hope you enjoyed this CSS tutorial on creating modals/popups! As you practice and experiment with modals, you'll gain a deeper understanding of their functionality and how to use them in various projects. Happy coding! 😊