Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 501765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:13:23+00:00 2026-05-13T06:13:23+00:00

I generate a div for my javascript automated validation script . It is generated

  • 0

I generate a div for my javascript automated validation script. It is generated with below code:

var alertbox = document.createElement("div");

Now how do i center that div programatically both horizontally and vertically?

Note: Currently I am showing an alert box but once i know how to center a div, i will
replace the alertbox with this dynamically generated div which will also provide some
lightbox-like effect.

Thanks

Note: For reference, you can download latest version of this script here.

Please note that i dont want to use external css for this because this is validation script and it should be able to do it programatically. For example:
using something like this can be helpful:

 alertbox.style.position: whatever
 ....
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T06:13:23+00:00Added an answer on May 13, 2026 at 6:13 am

    It’s a matter of applying the same CSS rules as you would have with static content. Horizontal centering can be achieved as described in this article – basically you give the div a fixed with and set left and right margins to auto. Vertical centering is a bit trickier – a few approaches are discussed here and detailed in this this blog post.

    The tidiest way is probably to define a CSS class that takes care of the centering and then apply that class to the dynamically generated element like this:

    alertbox.className = "myCssClass";
    

    Update:
    Since you are already using JavaScript for creating the div, you could of course use it for the centering as well (in combination with CSS absolute positioning) – that would actually probably be a cleaner solution (due to the hackishness of the CSS vertical centering). Exactly how you do this depends a bit on what tools you are using – it’s probably much easier to achieve with a JS framework such as Prototype or jQuery than with “raw” JavaScript since browsers handle window/browser heights a bit differently.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a javascript that displays a generated text into a div: document.getElementById('phrase').innerHTML =
I have this code : HTML <a href="javascript:void(0);" id="showDiv">Show Agency</a> <div id="invisibleDiv"> <script src="http://platform.linkedin.com/in.js"
I have written a javascript to generate textbox dynamically. Code: function addtextbox(val) { var
Below script will downlaod the facebook sdk to generate the code to create the
I'm trying to generate the following html code using cl-who: <html> <body> <div id=cnt_1></div>
The following jQuery Javascript code is included on an otherwise empty page. var element;
I am trying to generate a simple jQuery code snippet in JavaScript and append
This code should not generate any HTML button dynamically. Coz there is no div
i'm using this to generate a textbox dynamically:` <html> <head> <title>Dynamic Form</title> <script type=text/javascript
here i generate a div from javascript and position over the text box. i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.