473,386 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

DOM created image expected position shifts

On window load I'm running some javascript to add an image to a DIV. The
image is being added/created but it is about 18 or so pixels further
down the screen than expected. If I code the IMG tag, including a style
attribute, directly into the DIV's HTML the image displays where I
expect it to i.e. in the top left corner.

Is there something about DOM methods that affects positioning or is
there something else I've missed?
The relevant js looks like this:

if (document.getElementById("up")) {
myimg = document.createElement("IMG");
} else {
return;
}
document.getElementById("up").appendChild(myimg);
myimg.src = "q.png";
myimg.style.position = "absolute";
The relevant HTML is simply this:

<div id="up" style="position:absolute; left:40px; top:40px; width:600px;
height:80px;>
</div>
Andrew Poulos
Jul 23 '05 #1
2 1279
VK
Your code works just fine for me under IE 6.0 and Firefox 1.0.4 (which
is your browser? I guess by posting props). There must be someting else
in the outer code.

Jul 23 '05 #2


Andrew Poulos wrote:
On window load I'm running some javascript to add an image to a DIV. The
image is being added/created but it is about 18 or so pixels further
down the screen than expected. If I code the IMG tag, including a style
attribute, directly into the DIV's HTML the image displays where I
expect it to i.e. in the top left corner.

Is there something about DOM methods that affects positioning or is
there something else I've missed?


Perhpaps there are other child nodes already in the <div> so that you
should better use
divElement.insertBefore(myimg, divElement.firstChild);
if you want to make sure the image sits as the first element.
If you don't specify left/top on the absolutely positioned <img> then it
takes its place in the normal flow thus if there are any child nodes
before it it can end up below the top left corner.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Csaba2000 | last post by:
I want to be able to programatically click on the center of an <INPUT type=image ...> element (I only care about IE 5.5+). This should work regardless of whether IE has focus. Normally you would...
1
by: Lam | last post by:
hi i have a problem with position of image i use this code in a "alert" function : document.getElementById("lienx").offsetLeft + " , " + document.getElementById("lienx").offsetTop with : ...
4
by: Mimo Zus | last post by:
I'm hoping that someone can explain what's going on; better yet provide a workaround. I'm designing a centered CSS site based on a 550 pixel wide vertical background image. Onto this background...
6
by: nichughes | last post by:
Hello all, A question relating to http://www.entrust-systems.net/ Just for a change I have run into a problem that seems to be OS specific rather than browser specific - the box image that is...
4
by: Billy | last post by:
Hi all, I'm building a text file from a database table using the ASP Write Method and would like to position the cursor in a specific column position before writing the fields. As I loop through...
6
by: Seth Illgard | last post by:
Hi, Im writting a custom CMS and everything looks great, except when I see the results in IE. What im trying to do is: *Have an image in a layer (or relative positioned, or just margined). The...
6
by: -Lost | last post by:
Anyone know offhand how to center a text within the bounding box of an image (width-wise at least) without the Freetype library? GD- or ImageMagick-specifc code would do nicely. -- -Lost...
1
by: rsteph | last post by:
I've got some product information pages, with images and text, all setup within a table. I'm trying to add a small image in the upper right hand corner of the content div (where all the important...
4
by: Bob Altman | last post by:
Hi all, I posted an earlier question to the microsoft.public.inetserver.asp.general newsgroup asking how to set the background of a client-side table cell to a gradient, and I received this...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.