Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting top and left CSS attributes

For some reason I'm unable to set the "top" and "left" CSS attributes using the following JavaScript.

var div = document.createElement('div'); div.style.position = 'absolute'; div.style.top = 200; div.style.left = 200; document.body.appendChild(div); 

Using Firebug I can see that the div gets the position set to "absolute" but the top and left attributes are not set!

This is for Firefox 3.6.

like image 983
Deniz Dogan Avatar asked Feb 06 '10 19:02

Deniz Dogan


People also ask

What does Top and Left do in CSS?

Types of positioningThe top and bottom properties specify the vertical offset from its normal position; the left and right properties specify the horizontal offset. An absolutely positioned element is an element whose computed position value is absolute or fixed .

What is left attribute in CSS?

The left property in CSS is used to specify the horizontal position of a positioned element. It has no effect on non-positioned elements. Note: If position property is absolute or fixed, the left property specifies the distance between the element left edge and the left edge of its containing block.

What is Z Index 9999 in CSS?

In CSS code bases, you'll often see z-index values of 999, 9999 or 99999. This is a perhaps lazy way to ensure that the element is always on top. It can lead to problems down the road when multiple elements need to be on top. Most of the time you'll find that a z-index of 1 or 2 will suffice for your needs.


1 Answers

Your problem is that the top and left properties require a unit of measure, not just a bare number:

div.style.top = "200px"; div.style.left = "200px"; 
like image 123
Lyubomyr Shaydariv Avatar answered Sep 19 '22 04:09

Lyubomyr Shaydariv