Popup Modal Dialog with Javascript and HTML

Hussein Nasser · Beginner ·🌐 Frontend Engineering ·7y ago

Key Takeaways

This video demonstrates how to build a popup modal dialog from scratch using HTML, CSS, and JavaScript, covering the basic structure, styling, and functionality of the popup.

Full Transcript

what's up y'all this is the same NASA from IgM 3 where we discuss software engineering by example and in this episode we will discuss how to build a pop-up in HTML CSS and JavaScript so a pop-up is its look like an illusion dialog that show up on your page and you can interact with it is that's that's simple so we're gonna show something like that so that's that's what we're building here so I'll pop up so JavaScript and HTML doesn't have pop-ups out of the box right so what we're building here is an illusion of a pop-up so what you're seeing here is when I click on that button I essentially have one big div element that stretches the whole screen that's the grayed out black transfer thing that you see in the back end and another rectangle or another div elements or two developments the second div element is the content the first one is the container and then the content we start showing the message or the dialog that's it that's that's exactly up what a pop-up is so it's very simple alright so and then if I click on any outside on that div element that is kind of hidden there I I hide I had the whole thing so how about that let's let's just start to build that how about that guys alright so we'll start from scratch I'm gonna use Visual Studio code very nice editor right so as we call it pop-up just pop up with HTML and let's just start building HTML here that's very simple stuff head I have fat fingers because I just got my finger so at the bandaid there so what we need to do is to the body and we'll just try to copy that latin text right here and I'll just paste it I wanna warn what I'm doing alright so we'll just I'll take that alright that's a simple thing all right oh but we start pulling usour here that's much better all right so the new page is test pop-up page right so that's that's the page right imagine this is your webpage that's the website day you already have and you want to add a dialog to it when you click up on it so first obviously you need a button right so let's add a button here called show and it's named show and let's refresh that well what does nothing what I want to do if I click that I want to show that dialogue order so the dialogue consists of two developments as we agreed right guys so there is a div called let's call daddy pop-up container and inside that container there is another pop-up content right so that's your message that's where this is my pop-up right that's your pop-up message and you can do fancy stuff I don't know make it hit or to me whatever right so you can do with a message actually here and that is the pop-up container will be the whole thing so obviously we don't have any code that does that now but we will assign classes styles to those puppies right now okay let's do a style about that guys do a style and I'm gonna call my first style pop-up container guess what pop-up container the second style is guess what pop-up content right and the public container you guess that guys right so has to fill the whole screen so 0 left top 0 width 100% height only percent all right position obviously not relative we need it fixed right because we were specifying those numbers and we need a color so we have give it and a black color RGB 0 0 0 which as in for black but I'm gonna give it like 50 percent transparency okay I think that's all I need right wait we need one more thing yeah so in that container hope I still have the old one yes so if you notice in the old container we have this space right here so that container is padded on the top with a certain number and that's what we add right so you can just add padding top I don't know 300 pixels so you can pad didn't spill that because I didn't have that all right so that's the pop-up container the bubble content no much there to say really but you can just add a background color and I won't add a color of yeah chartreuse why not okay and maybe I give it a certain height of 200 pixels that's the dialogue remember guys and the width of this maybe I want to 80% so that's like I want it to be like 80% of the screen okay the next thing we want to do is assign those guys right so the class of this pop-up container is pop-up content and this guy is pop-up content and what happened here you are just another okay well good so now if I go back to my thing here you notice right it just like added that thing immediately right and it's like that pop-up is like on to the left and that's because we don't have a margin assigned once you assign a margin for that content you say hey style or cell phone margin so it says it's like go in the center essentially that's what it does right so that's like this is my pop-up right but if I click anywhere it's not closing why because we don't have any JavaScript code that does first of all first why when I open the page I don't want it to start immediately right so that means that container should be guess what guys hidden that's display:none don't show it to me by default okay so I do that it's it's not showing but if I click this nothing also showing because guess what we did ad code JavaScript code that says hey when use someone click on this you know I go ahead and show it we didn't do that so I'm gonna create a variable cost show called document I'll get the element by ID show and then show dot add eventlistener click and then you do e add a function if someone clicks on this button go ahead and show that pop-up container but guess what we don't have a pop-up container object do e right so we have to create a pop-up container ID let's get it by ID there guys and all we have to do is style dot display because guess what block go ahead and show it block means show nan means hi right that shows it so let's refresh and see show nice nice but I can't close the guys I need to close it so I guess what if I want to click anywhere here on this gray thingy right I want to close it so what is that great thingy it's nothing but the pop-up container right so I wanna add an event listener if someone to click on me go ahead and close me which is essentially hide me which is essentially Paul container that style the display equal men all right I'm gonna share the code guys for in the description below so don't worry about it if I type too fast and then look at that that's the pop-up right there guys that's Edie alright guys that's all for me today you can play with this style to change the color of the background color like you can make it let loads for quasi or something else so it doesn't have to be like black but black like that why the grayed out think gives an illusion of a disabled right button so that's what we want here that's why I made it like it's like slightly gray alright guys that's all for me today check out the other content in software engineering by example and I geometry we talked about a lot of software engineering we have a lot of tutorials JavaScript VB C sharp and a lot of GIS that's my specialty geographic information system check out the other content guys and I'm gonna see you in the next one you guys stay awesome

Original Description

A popup is a dialog that allow users to interact with it and perform several tasks or show a message. HTML/CSS don’t have modal popups dialogs by default (Javascript does have alerts and prompts but they are not pretty in my opinion :) ). So we are building a modal popup dialog from scratch. To build a popup we need to trick the user by adding two DIV elements, a container div and a content div representing the popup. We hide and show them giving the illusion of a popup while preserving our website. Source code: https://github.com/hnasr/playground/blob/master/popup.html Stay Awesome! Learn Javascript from scratch here by example https://www.youtube.com/playlist?list=PLQnljOFTspQW9tY3eqBrmbY7CqxNrW-g3
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Hussein Nasser · Hussein Nasser · 59 of 60

1 Extending ArcObjects (IGeometry) - 01 - Getting Started
Extending ArcObjects (IGeometry) - 01 - Getting Started
Hussein Nasser
2 Extending ArcObjects  (IGeometry) - 02 - The Document, The Map and The Layers
Extending ArcObjects (IGeometry) - 02 - The Document, The Map and The Layers
Hussein Nasser
3 Channel Update - New Book, New Job, New Videos
Channel Update - New Book, New Job, New Videos
Hussein Nasser
4 Learn Programming with VB.NET - 01 - Getting Started
Learn Programming with VB.NET - 01 - Getting Started
Hussein Nasser
5 Learn Programming with VB.NET - 02 - Classes and Objects (Part 1)
Learn Programming with VB.NET - 02 - Classes and Objects (Part 1)
Hussein Nasser
6 Learn Programming with VB.NET - 03 - Classes and Objects (Part 2)
Learn Programming with VB.NET - 03 - Classes and Objects (Part 2)
Hussein Nasser
7 Learn Programming with VB.NET - 04 - User Interface
Learn Programming with VB.NET - 04 - User Interface
Hussein Nasser
8 Learn Programming with VB.NET - 05 - By Value v. By Reference
Learn Programming with VB.NET - 05 - By Value v. By Reference
Hussein Nasser
9 Learn Programming with VB.NET - 06 - Variable size, 32 bit vs 64 bit
Learn Programming with VB.NET - 06 - Variable size, 32 bit vs 64 bit
Hussein Nasser
10 Learn Programming with VB.NET - 07 - Conditional Statements
Learn Programming with VB.NET - 07 - Conditional Statements
Hussein Nasser
11 Learn Programming with VB.NET - 08 - Inheritance
Learn Programming with VB.NET - 08 - Inheritance
Hussein Nasser
12 Learn Programming with VB.NET - 09 - Strategy Design Pattern
Learn Programming with VB.NET - 09 - Strategy Design Pattern
Hussein Nasser
13 Learn Programming with VB.NET - 10 -  How did I learn programming
Learn Programming with VB.NET - 10 - How did I learn programming
Hussein Nasser
14 IGeometry 2016 Retrospective - Channel Update
IGeometry 2016 Retrospective - Channel Update
Hussein Nasser
15 Javascript by Example - The Vook
Javascript by Example - The Vook
Hussein Nasser
16 Vlog - Keep your servers close and your database closer
Vlog - Keep your servers close and your database closer
Hussein Nasser
17 Vlog - Client/Server Programming Languages
Vlog - Client/Server Programming Languages
Hussein Nasser
18 Javascript By Example L1E01 - Getting Started
Javascript By Example L1E01 - Getting Started
Hussein Nasser
19 Persistent Connections (Pros and Cons)
Persistent Connections (Pros and Cons)
Hussein Nasser
20 Javascript By Example L1E02 - Building the Calculator Interface
Javascript By Example L1E02 - Building the Calculator Interface
Hussein Nasser
21 Happy new Year from IGeometry!
Happy new Year from IGeometry!
Hussein Nasser
22 Synchronous v. Asynchronous
Synchronous v. Asynchronous
Hussein Nasser
23 Javascript By Example L1E03 - Displaying the Digits on Calculator Screen
Javascript By Example L1E03 - Displaying the Digits on Calculator Screen
Hussein Nasser
24 Show Your Work. Blog, Vlog, Write, Create and Develop!
Show Your Work. Blog, Vlog, Write, Create and Develop!
Hussein Nasser
25 Relational Database Atomicity Explained By Example
Relational Database Atomicity Explained By Example
Hussein Nasser
26 Javascript By Example L1E04 - Operators, All Clear with Arrow Functions
Javascript By Example L1E04 - Operators, All Clear with Arrow Functions
Hussein Nasser
27 What Comes First, User Experience or Software Architecture?
What Comes First, User Experience or Software Architecture?
Hussein Nasser
28 Javascript By Example L1E05 -  Evaluate the Calculator Expressions with eval
Javascript By Example L1E05 - Evaluate the Calculator Expressions with eval
Hussein Nasser
29 Fastest Way to Learn Programming Language or Technology
Fastest Way to Learn Programming Language or Technology
Hussein Nasser
30 Javascript By Example L1E06 -  Fix Leading Zero Bug with Conditions
Javascript By Example L1E06 - Fix Leading Zero Bug with Conditions
Hussein Nasser
31 Stateful vs Stateless Applications (Explained by Example)
Stateful vs Stateless Applications (Explained by Example)
Hussein Nasser
32 Javascript By Example L1E07 - Running our Calculator on the Mobile Phone
Javascript By Example L1E07 - Running our Calculator on the Mobile Phone
Hussein Nasser
33 Advice for New Software Engineers and Developers
Advice for New Software Engineers and Developers
Hussein Nasser
34 Why JSON is so Popular?
Why JSON is so Popular?
Hussein Nasser
35 Building Scalable Software - SLA, HS, VS
Building Scalable Software - SLA, HS, VS
Hussein Nasser
36 Vlog (Istanbul) - Datacenter Proximity
Vlog (Istanbul) - Datacenter Proximity
Hussein Nasser
37 Should Software Engineers Learn Bleeding-Edge Technologies?
Should Software Engineers Learn Bleeding-Edge Technologies?
Hussein Nasser
38 Do Developers Build Bad User Interfaces/Experience?
Do Developers Build Bad User Interfaces/Experience?
Hussein Nasser
39 Learn By Doing.
Learn By Doing.
Hussein Nasser
40 I Wrote Bad Front-End Code That Broke Chrome
I Wrote Bad Front-End Code That Broke Chrome
Hussein Nasser
41 My Story
My Story
Hussein Nasser
42 Vlog - Horizontal vs Vertical Scaling
Vlog - Horizontal vs Vertical Scaling
Hussein Nasser
43 Can User Experience Help Build Better Rest API?
Can User Experience Help Build Better Rest API?
Hussein Nasser
44 Reverse engineering Instagram in flight mode
Reverse engineering Instagram in flight mode
Hussein Nasser
45 The Benefits of the 3-Tier Architecture (e.g. REST API)
The Benefits of the 3-Tier Architecture (e.g. REST API)
Hussein Nasser
46 Stateless v. Stateful Architecture (Podcast)
Stateless v. Stateful Architecture (Podcast)
Hussein Nasser
47 The evolution from virtual machines to containers
The evolution from virtual machines to containers
Hussein Nasser
48 Proxy vs. Reverse Proxy (Explained by Example)
Proxy vs. Reverse Proxy (Explained by Example)
Hussein Nasser
49 Canary Deployment (Explained by Example)
Canary Deployment (Explained by Example)
Hussein Nasser
50 No Excuses
No Excuses
Hussein Nasser
51 Synchronous vs Asynchronous Applications (Explained by Example)
Synchronous vs Asynchronous Applications (Explained by Example)
Hussein Nasser
52 What is an Asynchronous service?
What is an Asynchronous service?
Hussein Nasser
53 Difference between Client Polling vs Server Push in Notifications
Difference between Client Polling vs Server Push in Notifications
Hussein Nasser
54 Software vs. Hardware AdBlockers (Explained by Example)
Software vs. Hardware AdBlockers (Explained by Example)
Hussein Nasser
55 HTTP Caching with E-Tags -  (Explained by Example)
HTTP Caching with E-Tags - (Explained by Example)
Hussein Nasser
56 Simple Object Access Protocol Pros and Cons (Explained by Example)
Simple Object Access Protocol Pros and Cons (Explained by Example)
Hussein Nasser
57 Nodejs Express "Hello, World"
Nodejs Express "Hello, World"
Hussein Nasser
58 Reverse Engineering Instagram feed
Reverse Engineering Instagram feed
Hussein Nasser
Popup Modal Dialog with Javascript and HTML
Popup Modal Dialog with Javascript and HTML
Hussein Nasser
60 MIME and Media Type sniffing explained and the type of attacks it leads to
MIME and Media Type sniffing explained and the type of attacks it leads to
Hussein Nasser

This video teaches how to build a popup modal dialog using HTML, CSS, and JavaScript, covering the basic structure, styling, and functionality of the popup.

Key Takeaways
  1. Create the basic HTML structure for the popup
  2. Add CSS styles to the popup
  3. Use JavaScript event listeners to show and hide the popup
  4. Manipulate the DOM to create the popup effect
💡 The popup modal dialog is created using a combination of HTML, CSS, and JavaScript, with the CSS styles providing the visual appearance and the JavaScript event listeners controlling the functionality.

Related Reads

Up next
How to Speed Up Your WordPress Website with WP Rocket ⚡Tutorial 2026
Matt Tutorials
Watch →