Here's a plain-JavaScript example:

var modal = document.getElementById('modal');
var shade = document.getElementById('shade');
document.getElementById('start').onclick = function() {
  modal.style.display = shade.style.display = 'block';
};
document.getElementById('close').onclick = function() {
  modal.style.display = shade.style.display = 'none';
};

// This code is a workaround for IE6's lack of support for the
// position: fixed style.
//
if (!('maxHeight' in document.body.style)) {
  function modalsize() {
    var top = document.documentElement.scrollTop;
    var winsize = document.documentElement.offsetHeight;
    var docsize = document.documentElement.scrollHeight;
    shade.style.height = Math.max(winsize, docsize) + 'px';
    modal.style.top = top + Math.floor(winsize / 3) + 'px';
  };
  modal.style.position = shade.style.position = 'absolute';
  window.onscroll = window.onresize = modalsize;
  modalsize();
}
body {
  margin: 0;
}

#shade,
#modal {
  display: none;
}

#shade {
  position: fixed;
  z-index: 100;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

#modal {
  position: fixed;
  z-index: 101;
  top: 33%;
  left: 25%;
  width: 50%;
}

#shade {
  background: silver;
  opacity: 0.5;
  filter: alpha(opacity=50);
}
<div id="shade"></div>
<div id="modal">
  <textarea rows="5" cols="25"></textarea>
  <button id="close">Close</button>
</div>

<p>
  <button id="start">Start</button>
</p>

There are various improvements you can make from there, such as iframe hacks to fix IE z-indexing, or encapsulating it in a reusable object, but that's the basic way it's done.

Answer from bobince on Stack Overflow
🌐
W3Schools
w3schools.com › howto › howto_css_modals.asp
W3Schools.com
Fullscreen Video Modal Boxes Delete Modal Timeline Scroll Indicator Progress Bars Skill Bar Range Sliders Color Picker Email Field Tooltips Display Element Hover Popups Collapsible Calendar HTML Includes To Do List Loaders Badges Star Rating User Rating Overlay Effect Contact Chips Cards Flip Card Profile Card Product Card Alerts Callout Notes Labels Ribbon Tag Cloud Circles Style HR Coupon List Group List Group with Badges List Without Bullets Responsive Text Cutout Text Glowing Text Fixed Footer Sticky Element Equal Height Clearfix Responsive Floats Snackbar Fullscreen Window Scroll Drawing
🌐
W3Schools
w3schools.com › w3css › w3css_modal.asp
W3.CSS Modal
Web Intro Web HTML Web CSS Web ... W3.CSS Downloads · ❮ Previous Next ❯ · A modal is a dialog box/popup window that is displayed on top of the current page: Open Modal ·...
🌐
W3Schools
w3schools.com › bootstrap › bootstrap_modal.asp
Bootstrap Modal Plugin
The .modal-body class is used to define the style for the body of the modal. Add any HTML markup here; paragraphs, images, videos, etc.
🌐
W3Schools
w3schools.com › js › js_project_modal_popup.asp
JavaScript Modal Popup Project
JavaScript Modal Popup A modal popup window that appears on top of the page. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, ...
🌐
W3Schools
w3schools.com › howto › howto_js_popup.asp
How To Create Popups
Fullscreen Video Modal Boxes Delete Modal Timeline Scroll Indicator Progress Bars Skill Bar Range Sliders Color Picker Email Field Tooltips Display Element Hover Popups Collapsible Calendar HTML Includes To Do List Loaders Badges Star Rating User Rating Overlay Effect Contact Chips Cards Flip Card Profile Card Product Card Alerts Callout Notes Labels Ribbon Tag Cloud Circles Style HR Coupon List Group List Group with Badges List Without Bullets Responsive Text Cutout Text Glowing Text Fixed Footer Sticky Element Equal Height Clearfix Responsive Floats Snackbar Fullscreen Window Scroll Drawing
Top answer
1 of 6
19

Here's a plain-JavaScript example:

var modal = document.getElementById('modal');
var shade = document.getElementById('shade');
document.getElementById('start').onclick = function() {
  modal.style.display = shade.style.display = 'block';
};
document.getElementById('close').onclick = function() {
  modal.style.display = shade.style.display = 'none';
};

// This code is a workaround for IE6's lack of support for the
// position: fixed style.
//
if (!('maxHeight' in document.body.style)) {
  function modalsize() {
    var top = document.documentElement.scrollTop;
    var winsize = document.documentElement.offsetHeight;
    var docsize = document.documentElement.scrollHeight;
    shade.style.height = Math.max(winsize, docsize) + 'px';
    modal.style.top = top + Math.floor(winsize / 3) + 'px';
  };
  modal.style.position = shade.style.position = 'absolute';
  window.onscroll = window.onresize = modalsize;
  modalsize();
}
body {
  margin: 0;
}

#shade,
#modal {
  display: none;
}

#shade {
  position: fixed;
  z-index: 100;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

#modal {
  position: fixed;
  z-index: 101;
  top: 33%;
  left: 25%;
  width: 50%;
}

#shade {
  background: silver;
  opacity: 0.5;
  filter: alpha(opacity=50);
}
<div id="shade"></div>
<div id="modal">
  <textarea rows="5" cols="25"></textarea>
  <button id="close">Close</button>
</div>

<p>
  <button id="start">Start</button>
</p>

There are various improvements you can make from there, such as iframe hacks to fix IE z-indexing, or encapsulating it in a reusable object, but that's the basic way it's done.

2 of 6
5

you can also use native HTML5.1 dailog. currently dialog element is only supported in Chrome 37+, Safari 6+ and Opera 24+.

var dailog = document.getElementById("dialog"); 

function openModal() { 
   // dailog.show(); 
      dailog.showModal();
} 

function closeModal() { 
    dailog.close(); 
} 
#dialog{width:300px;}
.right{float:right}
<button onclick="openModal()">Show dialog</button>


<dialog id="dialog">This is a dialog window<br/><br/><br/>
<button onclick="closeModal()" class="right">Close</button>
</dialog>

🌐
W3Schools
w3schools.com › howto › howto_css_modal_images.asp
How To Create Modal Images
A modal is a dialog box/popup window that is displayed on top of the current page.
🌐
W3Schools
w3schools.com › css › css3_images_modal.asp
CSS Responsive Modal Images
When a user clicks on a modal image, it shows a popup window that appears on top of the main content of the webpage, often with a semi-transparent background. The modal must be closed by the user, typically with a "close" button or an "X" sign ...
Find elsewhere
🌐
Sabe
sabe.io › tutorials › how-to-create-modal-popup-box
How To Create a Modal Popup Box with CSS and JavaScript
December 23, 2021 - In this tutorial, we learn how to make a modal with CSS and JavaScript. Our goal is to create a lightweight and simple modal popup box that does not use any third-party libraries and is cross-browser compatible. We will be using vanilla JavaScript without jQuery.
🌐
W3Schools
w3schools.com › bootstrap5 › bootstrap_modal.php
Bootstrap 5 Modal
The Modal component is a dialog box/popup window that is displayed on top of the current page: Open modal
🌐
W3Schools
w3schools.com › howto › howto_js_popup_form.asp
How To Create a Popup Form With CSS
Fullscreen Video Modal Boxes Delete Modal Timeline Scroll Indicator Progress Bars Skill Bar Range Sliders Color Picker Email Field Tooltips Display Element Hover Popups Collapsible Calendar HTML Includes To Do List Loaders Badges Star Rating User Rating Overlay Effect Contact Chips Cards Flip Card Profile Card Product Card Alerts Callout Notes Labels Ribbon Tag Cloud Circles Style HR Coupon List Group List Group with Badges List Without Bullets Responsive Text Cutout Text Glowing Text Fixed Footer Sticky Element Equal Height Clearfix Responsive Floats Snackbar Fullscreen Window Scroll Drawing
🌐
W3Schools
w3schools.com › bootstrap4 › bootstrap_modal.asp
Bootstrap 4 Modals
The Modal component is a dialog box/popup window that is displayed on top of the current page: Open modal
🌐
W3Schools
w3schools.com › bootstrap › bootstrap_ref_js_modal.asp
Bootstrap JS Modal Reference
The Modal plugin is a dialog box/popup window that is displayed on top of the current page.
🌐
Bootstrap
getbootstrap.com › docs › 4.0 › components › modal
Modal · Bootstrap
Have a bunch of buttons that all trigger the same modal with slightly different contents? Use event.relatedTarget and HTML data-* attributes (possibly via jQuery) to vary the contents of the modal depending on which button was clicked.
🌐
W3Schools
w3schools.com › tags › tag_dialog.asp
HTML dialog Tag
The <dialog> element makes it easy to create popup dialogs and modals on a web page. The numbers in the table specify the first browser version that fully supports the element. The <dialog> tag also supports the Global Attributes in HTML.
🌐
w3collective
w3collective.com › category: tutorial › create a responsive popup modal with css & javascript
Create a responsive popup modal with CSS & JavaScript - w3collective
February 18, 2023 - That’s all for this tutorial. By following along you’ll have learnt how to build a modal popup without having to rely on any external JavasScript plugins or libraries.
🌐
Medium
medium.com › @nerdplusdog › a-how-to-guide-for-modal-boxes-with-javascript-html-and-css-6a49d063987e
A How-To Guide for Modal Boxes with Javascript, HTML, and CSS | by Gabbie Piraino | Medium
February 12, 2019 - w3schools: How To — CSS/JS Modal · Sabe: How To Create a Modal Popup Box with CSS and JavaScript · JavaScript · HTML · CSS · How To · Popups · 110 followers · ·7 following · Current Tech Geek | Previous Book Nerd | Always Doggo Lifestyle · See all responses ·
🌐
Bootstrap
getbootstrap.com › docs › 5.0 › components › modal
Modal · Bootstrap v5.0
Have a bunch of buttons that all trigger the same modal with slightly different contents? Use event.relatedTarget and HTML data-bs-* attributes to vary the contents of the modal depending on which button was clicked.