
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How I can show JavaScript alert box in the middle of the screen?
To show the JavaScript alert box in the middle, you need to use the custom alert box. Under that, style it accordingly and position it to the center. Use the “top” and “left” CSS properties to achieve this. Set them as 50%, but as you can see a button below, use the property to 40% to align it with the button:
Example
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script> function functionAlert(msg, myYes) { var confirmBox = $("#confirm"); confirmBox.find(".message").text(msg); confirmBox.find(".yes").unbind().click(function() { confirmBox.hide(); }); confirmBox.find(".yes").click(myYes); confirmBox.show(); } </script> <style> #confirm { display: none; background-color: #F3F5F6; color: #000000; border: 1px solid #aaa; position: fixed; width: 300px; height: 100px; left: 40%; top: 40%; box-sizing: border-box; text-align: center; } #confirm button { background-color: #FFFFFF; display: inline-block; border-radius: 12px; border: 4px solid #aaa; padding: 5px; text-align: center; width: 60px; cursor: pointer; } #confirm .message { text-align: left; } </style> </head> <body> <div id="confirm"> <div class="message">This is a warning message.</div><br> <button class="yes">OK</button> </div> <input type="button" value="Click Me" onclick="functionAlert();" /> </body> </html>
- Related Articles
- How I can change the style of alert box using JavaScript?
- How I can set the width and height of a JavaScript alert box?
- How to show image in alert box using JavaScript?
- How I can replace a JavaScript alert pop up with a fancy alert box?
- How can I change the styling of JavaScript alert button?
- How to change the color of the alert box in JavaScript?
- How do I display image in alert/confirm box in JavaScript?
- How can I show a euro or other HTML entity in JavaScript alert windows?
- How to center the JavaScript alert message box?
- How to customize the position of an alert box using JavaScript?
- How to show hyperlinks in JavaScript alert?
- How to provide new line in JavaScript alert box?
- How to display JavaScript variable value in alert box?
- How to edit a JavaScript alert box title?
- What is an alert box in JavaScript?

Advertisements