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 3287108
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:27:22+00:00 2026-05-17T20:27:22+00:00

I am trying to add a <div> using JavaScript and then change its width/height/top/left

  • 0

I am trying to add a <div> using JavaScript and then change its width/height/top/left attributes. But when I use the XHTML 1 Transitional doctype, it stops working.

This is how I am creating the <div>:

var div=document.createElement("div");
document.body.appendChild(div);
div.innerHTML='<div id="myID" style="z-index:9998;border: 1px solid #000000;
border-radius:3px;position: absolute; display: block;top:-10000;left:-10000; 
width:400; height:400;"></div>';

So initially myDiv is not visible to user as its left and top are out of the screen

Then on some click action I am doing the following:

var myDiv=document.getElementById("myID");
myDiv.style.top=200;
myDiv.style.left=200;

This works fine if I do not put a doctype in the HTML. As soon as I put the doctype, it stops working.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

With the doctype, it stops accepting top/left/width/height values. But if I set the width/height/top/left values with units (i.e. px) then it works fine.

myDiv.style.top="200px";
myDiv.style.left="200px";

So, what is the impact of the doctype on the execution of JavaScript while modifying the style (or may be other things as well)?

  • 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-17T20:27:23+00:00Added an answer on May 17, 2026 at 8:27 pm

    Using raw numbers is technically invalid, even if the browser is letting you get away with it when you’re not using a doctype (browsers do lots of funky things when you don’t use a doctype).

    The style properties really are like the CSS properties, strings with units on them. So “200px” is correct, 200 is incorrect. The properties (left, for instance) can be lengths like “200px” or “10em”, or percentages (or auto or inherit). (Those links are to the CSS 2.1 spec.)

    You also need to include units in your string when creating the myID div:

    div.innerHTML = '<div id="myID" style="z-index:9998; ' +
        'border: 1px solid #000000; ' +
        'border-radius:3px;' +
        'position: absolute;' +
        'display: block;' +
        'top:-10000px;' +     // here
        'left:-10000px;' +    // here
        'width:400px;' +      // here
        'height:400px;"' +    // and here
        '></div>';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying add a tab to my web page that looks like this: Using
I'm trying to add support for stackoverflow feeds in my rss reader but SelectNodes
What I was trying to do was simply center a div using CSS. and
I'm trying to construct the following div element using jQuery: <div> <p>Hello</p> <p>Stack Overflow</p>
Trying to add an onclick handler to my tabs, and can't seem to get
trying to add an ExpiresDefault ExpiresByType to content on my website so that way
I was trying to add a favicon to a website earlier and looked for
I am trying to add a project reference or swc to papervision in FlashDevelop
I am trying to add some custom build steps to my headless build process
I was trying to add the XML schema to an existing EJB project. JAXB

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.