I'm experiencing an issue with the display of my Pop Up Window

I'm currently working on a website redesign project just for fun. One issue I've encountered is with a pop-up window that appears after clicking a button. Unfortunately, the layout of the window and button is quite strange - the button is positioned all the way to the left, and the text is scattered across the screen. If you'd like to take a look at the entire code, it's available on CodePen: http://codepen.io/sibraza/pen/wWgqBO

Here is the HTML:

<!--- This is what the user sees when they click the button -->
<span class="msg"><button class="btn btn-danger" data-js="open">Subscribe to our Newsletter</button></span>
</section>

<!-- this is what the user sees when the popup is displayed -->
<div class="popup">
 <h2>Subscribe to the Newletter:</h2>
    <button name="close">Close Pop-up</button>
</div>

Here is the CSS:

button {
  font-family: "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif;
  background: lightcoral;
  border: 0;
  border-radius: 4px;
  padding: 7px 15px;
  font-size: 16px;
  color: #FFFFFF;
  cursor: pointer;
}
button:focus {
  outline: none;
}
button:hover {
  background: #f39797;
}

.popup {
  background: rgba(255, 255, 255, 0.8);
  position: fixed;
  display: none;
  z-index: 5000;
  height: 100%;
  width: 100%;
  left: 0;
  top: 0;
}
.popup > div {
  border-radius: 4px;
  position: fixed;
  background: #FFFFFF;
  box-shadow: 0px 0px 12px #666666;
  padding: 30px 15px;
  /* Width of popup can be changed */
  width: 80%;
  max-width: 600px;
  z-index: 5001;
  -webkit-transform: translate(-50%, -50%);
  -moz-transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
  -o-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
  left: 50%;
  top: 50%;
  text-align: center;
}

Here is the JavaScript:

function handlePopup(popup) {

    /* Add div inside popup for layout if one doesn't exist */
    if ($(".wrapper").length == 0){
        $(popup).wrapInner("<div class='wrapper'></div>");
    }

    /* Open popup */
    $(popup).show();

    /* Close popup if user clicks on background */
    $(popup).click(function(e) {
        if ( e.target == this ) {
            if ($(popup).is(':visible')) {
                $(popup).hide();
            }
        }
    });

    /* Close popup and remove errors if user clicks on cancel or close buttons */
    $(popup).find("button[name=close]").on("click", function() {
        if ($(".formElementError").is(':visible')) {
            $(".formElementError").remove();
        }
        $(popup).hide();
    });
}

$(document).ready(function () {
    $("[data-js=open]").on("click", function() {
        handlePopup($(".popup"));
    });
});

Answer №1

Feel free to test out this code snippet, which includes a basic form. Simply adjust the CSS of either the popup or the form to suit your requirements.

function toggleOn(){
  $('body, #menu, #navbar, #content').toggleClass('on');
}

$(document).ready(function (){
  $('#menu').click(function(){ toggleOn(); });
  $('#content').click(function(){
    if ($('#navbar').hasClass('on')) toggleOn();
  });
});
// Popup functionality 
function popupOpenClose(popup) {

/* Add div inside popup for layout if one doesn't exist */
if ($(".wrapper").length == 0){
$(popup).wrapInner("<div class='wrapper'></div>");
}

/* Display popup */
$(popup).show();

/* Close popup when clicking on background */
$(popup).click(function(e) {
if ( e.target == this ) {
if ($(popup).is(':visible')) {
$(popup).hide();
}
}
});

/* Close popup and remove errors when user clicks on cancel or close buttons */
$(popup).find("button[name=close]").on("click", function() {
if ($(".formElementError").is(':visible')) {
$(".formElementError").remove();
}
$(popup).hide();
});
}

$(document).ready(function () {
$("[data-js=open]").on("click", function() {
popupOpenClose($(".popup"));
});
});

// Search bar functionality 
$(document).on('ready', function(){

var $wrap = $('[js-ui-search]');
var $close = $('[js-ui-close]');
var $input = $('[js-ui-text]');

$close.on('click', function(){
$wrap.toggleClass('open');
});

$input.on('transitionend webkitTransitionEnd oTransitionEnd', function(){
console.log('triggered end animation');
if ($wrap.hasClass('open')) {
$input.focus();
} else {
return;
}
});
});
// Pop up window
[The CSS code was provided]
<script src="[jQuery CDN link]"></script>
<head>
  [Import other necessary resources]
</head>
[Pop-up content, navigation menu, search bar, products section, footer content]

Explore the JSFiddle demo for practical application.

Answer №2

To properly display your popup, you will need to format it in the following way:

<div class="popup">
 <h2>Subscribe to the Newsletter:</h2><br>
  <center><button name="close">Close Pop-up</button></center>
</div>

It's important to specify how you want your popup to be displayed, such as center alignment or any other instructions. This code snippet works for me and provides a visually pleasing result.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The stylesheet link for "contact.css" is unresponsive

I have been reorganizing my website portfolio by categorizing CSS, images, and other files into different folders. <link rel="stylesheet" href="css/style.css"> <link rel="stylesheet" href="css/contact.css"> However, I am facing an issue where ...

How can I successfully add an element to a nested array without making any mistakes in saving it?

Hello everyone, I'm new here. I previously posted about a similar issue, but now I have a different one. I am encountering this object: singleChat = [ { "chatid": 10000414, "connected": true, "index": 0, ...

Troubleshooting: React.js State not updating properly on forms

New to React and facing a challenge with changing the state of form inputs. Here's what I'm trying to do: constructor(props) { super(props); this.state = { pwd:'', email:'', value:'' ...

What could be causing my PHP code to not execute properly alongside the HTML?

I am currently sharpening my skills in PHP by working on a simple web form project. To put it simply, I need to develop a basic web form that accepts a two-dimensional array of European cities along with the distance in kilometers between each pair of citi ...

Flexslider doesn't adjust the height of the viewport when displaying a shorter image in the slideshow

Is Flexslider not resizing its viewport height according to the current image on your site? The fixed height seems to be causing blank white space under shorter images. Are you inadvertently overriding Flexslider's responsive height function? Is there ...

CSS grid-box experiencing issues due to interference from bootstrap styling

When attempting to create a grid, I noticed that the first image element is not showing up in the first position as expected. Instead, it appears in the second position, leaving the first spot blank. After inspecting the issue, I realized that the styles f ...

Implement the anti-flickering script for Google Optimize in a NextJS/ReactJS environment

While working on a NextJS/ReactJS project, I am experimenting with setting up Google Optimize for some tests. One issue I have encountered is the flickering effect that occurs when Optimize changes visual elements during experiments. To address this probl ...

Utilizing a npm module in a web browser following importing in Node.js

I recently installed an npm module, but I'm having trouble figuring out how to use it in the browser after importing it. First, I installed the npm module using the following command (using a dummy example module): npm install sample-module In my p ...

Having trouble with CSS webkit radial not working on iPad's Safari Mobile browser?

I'm feeling confused right now. I have this gradient code background-image: -webkit-radial-gradient(50% 65%, ellipse cover, #f2f2f4, #201935 55%); It's working on Safari when I change the User Agent to Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3 ...

What is the correct way to utilize ng-if/ng-show/ng-hide to hide or show HTML elements within the app.run function

I am currently working on developing an app that loads views correctly. HTML: <body> <loading outerWidth='1000' outerHeight='1000' display='isReady'></loading> <div class='wrapper' ng-sho ...

Looking to switch up the background color while scrolling, any tips?

I'm trying to change the background color of my navbar section on scroll, but I've had no luck so far. Below is the code I have been using: const [colorChange, setColorChange] = useState(false); const changeColor = () => { if (window.scro ...

The ideal caret position while utilizing flexbox for centering within a contenteditable element

After testing on OSX Chrome 45, it appears that align-items: center; works for content alignment, but there is an issue with caret positioning in an empty editable field until text is typed. Is there a solution to this problem that doesn't involve ad ...

Issues with Noty plugin arising from compatibility problems with Internet Explorer 8 and Prototype.js libraries

Currently in the process of developing a webapp, and I have integrated a fantastic jQuery notification plugin from . Though it functions smoothly across most browsers, I'm encountering compatibility issues with Internet Explorer 8. It appears that the ...

Remove feature in HTML file upload form

I have a basic upload form set up like this: <form method="post" action="" enctype="multipart/form-data"> <table border="0"> <tr> <td>Select XML file to upload ...

Why is my post not complete in Jquery ajax due to encoding/datatype issues?

I have implemented Ajax in my code editor. The POST request contains a lot of coding syntax to handle: $.ajax({ type: "POST", url: "process.php", data: dataString, success: function() { $('#messagebox').html("<div id=&ap ...

What methods can be used to automatically log out a user or force a session timeout in an ASP.NET MVC 2 application when the app performs an auto-save function?

I've come across this question in various forms, and existing solutions are often tailored for different languages that may not apply to ASP.NET MVC 2. In my scenario, I am utilizing Jquery and Jquery forms to automatically save user data at regular ...

What is the best way to automatically log out a user when a different user logs in on the same browser?

Currently, I am encountering an issue where there are two separate dashboards for different types of users - one for admin and the other for a merchant. The problem arises when an admin logs in on one tab and then a merchant logs in on another tab in the s ...

Locate and eliminate the item containing specific content

There are many <p> &nbsp </p> tags scattered throughout the description. I need to locate and delete any tags that contain only &nbsp. The description is enclosed in a container with the class name of desc_container. Below is an exampl ...

How to use OBJLoader/Three.js to load a specific URL in JavaScript

I am encountering an issue with a JavaScript code snippet from the three.js OBJloader. I should mention that my experience level in JS and PHP is limited as I am just starting out with WordPress websites. My problem lies in utilizing the setPath and load ...

Send error messages directly to the client side or retrieve status codes and messages

When responding to an AJAX request, encountering an app error like data validation failure can be tricky. How can we effectively communicate this to the user? 1. Returning a Status Code and Fetching Message with JS $.ajax(options).done(function(response) ...