Responsive modal or popup are common features in a web application. They are used to show extra information or a form on the same page as popup above existing content upon clicking to a link or button. This article will help you to create a responsive modal or popup with CSS only.
I have also explained this pure CSS concept along with. So that you can understand how does this thing work. It will help you to change the CSS rule to organize responsive modal in your own way.
Responsive Modal or Popup – HTML
First of all write the modal html.
<!-- Start Modal -->
<div class="ft-modal" id="ft-demo-modal">
<h3>Popup | Modal | PURE CSS</h3>
<p align="justify">Hi, you can create a responsive modal with some knowledge of CSS. The CSS used here is minimal. You need to add CSS rules to maintain the appearance of content in popup.</p>
<h3>A link or button is also needed</h3>
<p align="justify">Just don't forget to add a link or button to click outside the modal html to popup these content.</p>
<p><strong>Thank you for reading!</strong></p>
<a class="ft-modal-close" href="#">[✖] Close Modal</a>
<!-- End Modal -->
<!-- Link in page to show modal on click-->
<a href="#ft-demo-modal">Open modal</a>
Here is a sample of html. Also don’t forget to place a link somewhere in your page which will trigger the responsive modal upon click. I have put Line #25 out of the popup html for the same.
The most important thing is place the modal html either in just beginning of body or just before closing body tag (outside page wrapper). So that your modal appearance won’t get influenced by CSS of the container or any parent wrapper holding regular page content.
Responsive Modal or Popup – The CSS
You need to place the following minimal CSS in your .css file or in between style tag of head section.
transition: all .3s ease-in-out;
-moz-transition: all .3s ease-in-out;
-webkit-transition: all .3s ease-in-out;
margin: 70px auto;
box-shadow: 0 0 6px rgba(0, 0, 0, 0.2);
Here is the minimum CSS required to control responsive modal. You might need additional rules to organize content inside popup. Duh! your theme would be already playing them fine. Right? 😶
If you want to put modal close button at top right, just uncomment line #36 to 43.
Now all is done. Just hit the link or button we’ve already created to pop the modal. Also resize your browser to test the responsiveness. Hope you’re feeling excited!
Responsive Modal or Popup – CSS Explanation
Hey! It’s the most important part I think. You should know that how this CSS only solution is working to popup the responsive modal. Let’s understand major things.
The core class is
ft-modal which content a full screen layer behind modal as well as content within. The
overflow property handles modal scrolling if you have much content to show in this pure CSS based responsive modal.
The z-index is required to keep the modal layer above the regular page content.
The top margin is set to -250px and zero when targeted (in :target pseudo rule, read about pseudo selectors). It stats that when the modal will be targeted, it would give a nice Slide In animation from top, through the transition properties given. The
opacity adds a smooth Fade effect in responsive popup animation.
ft-modal-content class is modal content container. The
max-width property ensures maximum width of responsive modal to 600px, no matter how wider the device is. Also given values of
margin give 70px top distance from document upper boundary and same 70px form lower boundary if content within modal is much. The classic auto values for left & right margins brings the responsive modal to exact horizontal center.
You should play with
margin property or can add more classes to
ft-modal-content div to have a big and a small sized modal. I leave it for you as exercise for this CSS only popup/modal.
Hope the solution above has made you understanding the responsive modal or pure CSS based responsive popup behavior. Your interest about CSS only responsive modal, feedback or questions are most welcome in comment.