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

  • Home
  • SEARCH
  • 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

Ask A Question

Stats

  • Questions 534k
  • Answers 534k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It depends on what you're targeting. There are loads of… May 17, 2026 at 12:51 am
  • Editorial Team
    Editorial Team added an answer If you only have a single definition of equality for… May 17, 2026 at 12:51 am
  • Editorial Team
    Editorial Team added an answer When are you initializing lines? Typically, collection properties only have… May 17, 2026 at 12:51 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

The following jQuery Javascript code is included on an otherwise empty page. var element;
I'm using JavaScript to dynamically generate a dialogue box (it's a div element), containing
My app uses javascript to generate all the HTML for my website. The index.html
I'm wondering is there a JavaScript library available that would allow me to generate
i need some help with this, i'm trying to generate some html with javascript,
Why does this: <%= update_page_tag do |page| page[femenino].hide end %> Generates this? <script type=text/javascript>
Which is the more efficient way to create the following, hardcoded HTML or Javascript
I'm doing a little experiment trying to randomly place images inside a div using
I have some javascript which will create some sort of widget on a page.
I'm trying to generate a Google Map based off of results from a database.

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.