How To Create A Popup Modal With CSS + JavaScript

How To Create A Popup Modal With CSS + JavaScript

A modal is a popup window that is displayed in front of the current page when pressed a button. Here’s my example

A modal is a popup window that is displayed in front of the current page when pressed a button.

Here’s my example, I have recently created a Twitter-like website using Django.

Image for post

First step: style your parent section

The first thing you want to do is create a semi-transparent black background. To do that you need to create a parent element which wraps your content section.

<div class=”bg-modal”>
 <div class=”modal-content”>

 </div>
 </div>

Let’s style this modal now:

.bg-modal{
 width:100%;
 height:100%;
 background-color: black;
 }

You also need to tell the background modal to lay over top of the content.

position: absolute;

When you say absolute, you need to say where it is.

top:0px;

The problem here is if we do opacity: 0.7for decreasing opacity of background,this opacity value will transfer to the child element no matter whether you specify the child element’s opacity or not. The solution is not to specify opacity in here but in the background colour.

bacground-color: rgba(0,0,0,0.7) // the 4th number will be the opacity

Let’s look at what we have so far:

.bg-modal{
 width: 100%;
 height:100%;
 background-color: rgba(0,0,0,0.5); /* make it half transparent */
 position: absolute;
 top:0px;
 z-index: 1;
 display: none;  /* It will remain invisible until you open */
 justify-content: center; /*center horizontally*/ 
 align-items: center ; /* center vertically */}

javascript coding css

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Make Calculator Using HTML, CSS, and JavaScript(With Code)

How To Make Calculator Using HTML, CSS, and JavaScript? The calculator is one of the most complex lessons in the JavaScript programming language.

A Step-by-Step Introduction to CSS Preprocessors with VS Code

It is fun to build cool and good looking pages when you know CSS like the back of your hand. But when you have a few hundred lines of code, this is exactly when things begin to get complicated and messy. This is where CSS preprocessors come to play, helping you to manage and improve the maintainability of your code.

4 Ways You Can Get Rid of Dirty Side Effects for Cleaner Code in JavaScript

4 Ways You Can Get Rid of Dirty Side Effects for Cleaner Code in JavaScript. Bugs are born in many ways. Creating side effects is one of them. Some people say side effects are evil, some say they’re not.

Static Code Analysis: What It Is? How to Use It?

Static code analysis is a method of debugging by examining source code before a program is run. It's done by analyzing a set of code against a set (or multiple sets) of coding rules. Static code analysis and static analysis are often used interchangeably, along with source code analysis.

Who Else Wants to Write Clean JavaScript Code?

Who Else Wants to Write Clean JavaScript Code? 7 Tips to Make Your Coworkers Fall in Love With Your Code.