3

I have the following code:

let dynel = document.createElement('div');
dynel.className = 'foo';

dynel.style.width = '5px';
dynel.style.height = '5px';
dynel.style.backgroundColor = 'blue';

document.body.appendChild(dynel);

This code works as I expect it to, after appending the dynamic element to the document, a 5 x 5 blue box appears. The problem starts when I try to access the element via its className to style it further:

var foo = document.getElementsByClassName('foo');

foo[0].style.top = '50px';
foo[0].style.left = '200px';

This code should position the box but it does nothing, what am I doing wrong? Preferably I'm looking for a pure JS solution so no JQuery.

Thanks in advance :)

krecik2000
  • 47
  • 7

4 Answers4

4

Problem

The default value of the property position of a div is static. When you try to set left/right [..] on static element it has no effect on that element.

static : every element has a static position by default, so the element will stick to the normal page flow. So if there is a left/right/top/bottom/z-index set then there will be no effect on that element. relative : an element's original position remains in the flow of the document, just like the static value.

Solution

  • You have to set the position property in your created element to absolute, fixed or relative.

let dynel = document.createElement('div');
dynel.className = 'foo';

dynel.style.position = "absolute"
dynel.style.width = '5px';
dynel.style.height = '5px';
dynel.style.backgroundColor = 'blue';

document.body.appendChild(dynel);

var foo = document.getElementsByClassName('foo');

foo[0].style.top = '50px';
foo[0].style.left = '200px';
Aalexander
  • 4,987
  • 3
  • 11
  • 34
  • Wow, that was so simple, I can't believe I overlooked the position. I'm kinda ashamed now, many thanks, Idk how long I'd be stuck on that :) – krecik2000 Jan 09 '21 at 12:21
  • Wonderful Alex....Only 1 think that Alex missed is position:relative; thing – Imran Rafiq Rather Jan 09 '21 at 12:22
  • @ImranRafiqRather relative worked I thought it will not, I have updated it – Aalexander Jan 09 '21 at 12:37
  • CHEERS :) Only this part was missing... And a crucial one as well... Why because fixed stays where ever it is used. absolute also depending on how we use it comes out of the normal flow...But relative is the only case where we can use top,left,right, bottom and yet the element stays in normal page flow :) – Imran Rafiq Rather Jan 09 '21 at 13:01
1

You need to ensure that foo[0] has the position: absolute; style set.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Agnitor44
  • 23
  • 3
  • 1
    @krecik2000: Note that with position:absolute: your element will come out of normal flow of execution :) Just wanted to add that :) If this is you implementation that's fine :) If not use position:relative; insteal :) Cheers – Imran Rafiq Rather Jan 09 '21 at 12:24
1

top, left, right, bottom works on those elements in which position property is not static. But either absolute or relative or fixed.

As per your requirement use any of the 3 values in your CSS and styles will start to apply. Something like this would work (But it depends how you want you implemention of the code)

var foo = document.getElementsByClassName('foo');

foo[0].style.position = "relative";
foo[0].style.top = '50px';
foo[0].style.left = '200px';

NOTE: position:fixed and position:absolute would get your element out of the normal code flow and you will have to adjust accordingly.

Imran Rafiq Rather
  • 7,677
  • 1
  • 16
  • 35
  • just verify that 'relative' works the same as 'static'. Only 'absolute' and 'fixed' work with top, left, right, bottom. Not 'relative' – hoogw Jan 17 '22 at 16:42
  • @hoogw No!!! relative does take top, bottom, left, right properties. Do cross check dear. https://developer.mozilla.org/en-US/docs/Web/CSS/position Here is the Demo for you. Enjoy https://codepen.io/emmeiWhite/pen/PoJgoQd?editors=0100 First thing is understanding how `position:relative` works. Spend some time around and practice. You will learn more hopefully. CHEERS !!! :) – Imran Rafiq Rather Jan 18 '22 at 03:25
  • 1
    Hi, Imran Rafiq Rather , I follow your advise, Spend some time around and practice, I did learn more, see below. I create 3 example for each, in below answer. Thanks. I gave you credit in my answer at the beginning. – hoogw Jan 19 '22 at 16:53
  • @hoogw Glad that you practised and learned more :) – Imran Rafiq Rather Jan 22 '22 at 21:19
1

I was inspired by @Imran Rafiq Rather to give the details example for each situation:

  1. relative

If you just want to adjust position a little bit relative to its default position, use key word "relative"

Default position is 'static' position

var foo = document.getElementsByClassName('foo');

foo[0].style.position = "relative";  // works almost the same as default or 'static', because 'relative' is relative to it's 'static' position. 

foo[0].style.top = '50px';
foo[0].style.left = '200px';

https://codepen.io/hoogw/pen/QWqRdBY

enter image description here

  1. absolute

we usually want to relative to its parent div, should use 'absolute'

var foo = document.getElementsByClassName('foo');

foo[0].style.position = 'absolute';  // relative to its parent div

foo[0].style.top = '50px';
foo[0].style.left = '200px';

https://codepen.io/hoogw/pen/GRMaWKB?editors=1100 enter image description here

  1. fixed

If you want to relative to whole page, use 'fixed'

var foo = document.getElementsByClassName('foo');

foo[0].style.position = 'fixed';  // relative to whole page, whole html body document

foo[0].style.top = '50px';
foo[0].style.left = '200px';

https://codepen.io/hoogw/pen/yLzWMvJ enter image description here

enter image description here Here is link to document https://developer.mozilla.org/en-US/docs/Web/CSS/position

hoogw
  • 4,982
  • 1
  • 37
  • 33