3 ballons with pink background

CSS-Only Modal Lightbox Pop-Up on Page Load

In my earlier post, I shared a way to add a CSS-only modal/lightbox. Many readers asked how to make the modal pop up automatically on page load—without clicking a button. Here’s the exact approach.

I’ve shared a way to add a pop-up or modal lightbox with CSS only in my earlier post, Easy Modal Lightbox Pop-Up. I’ve received many requests on how to make this modal pop up on page load instead of using a button. So, here’s the how-to:

HTML

Copy the code below and paste it into your HTML document.

<div id="modal-1" class="modal animate-opacity">
   <div class="modal-content">
      <div class="modal-inner">
         <span onclick="document.getElementById('modal-1').style.display='none'" class="modal-close">&times;</span>
         <h4>Modal Headline</h4>
         <p>Modal description goes here.</p>
      </div> 
   </div>
</div>

CSS

Copy the code below and paste it into your CSS file.

.modal {
	z-index: 10;
	display: flex;
        align-items: center;
        justify-content: center;
	position: fixed;
	left: 0;
	top: 0;
	width: 100%;
	height: 100%;
	overflow: auto;
	background-color: rgb(0,0,0);
	background-color: rgba(0,0,0,0.5)
}
.modal-content {
	margin: auto;
	background-color: #fff;
	position: relative;
	padding: 0;
	outline: 0;
	max-width: 600px
}
.modal-inner { padding: 20px 30px; }
.modal-close {
	-webkit-touch-callout: none;
	-webkit-user-select: none;
	-khtml-user-select: none;
	-moz-user-select: none;
	-ms-user-select: none;
	user-select: none;
	position: absolute;
	right: 0;
	top: 0;
	background: #ccc;
	padding: 6px 10px;
}
.animate-opacity { animation: opac 0.8s }@keyframes opac{from{opacity:0} to{opacity:1}}

.animate-opacity is optional; it handles the open animation. You can tweak the speed—it’s currently opac 0.8s.

Troubleshooting (if the pop-up doesn’t show)

  • Ensure CSS is loaded: the modal relies on display: block and the fixed overlay styles—confirm your theme doesn’t override them.
  • Check z-index conflicts: headers or sticky bars with higher z-index can cover the modal; increase .modal { z-index: 9999; } if needed.
  • No hidden rules: some themes/plugins set .modal{display:none}. Search your CSS for overrides.
  • Place the HTML once per page: duplicate IDs or multiple instances can cause odd behavior.
  • Cache/CDN: purge cache if changes don’t appear.

Note: The close button above uses an inline onclick. If you want a zero-JavaScript variant, I can add a checkbox/label pattern as an optional section—just say the word.

Leave a Comment

Your email address will not be published. Required fields are marked *