Search code examples
csspositioning

Repositiong absolute elements with javascript


May I ask whether or not it would be considered 'bad practise' to reposition a div using javascript based on the height of another absolutely-positioned div which I want it to be behind (z-indexed)?

The main content div is positioned absolutely, and has variable height on each page depending on the 'dynamically generated' content. I positioned it absolutely and not relatively because then I ended up with a gap at the bottom of my page where the content div still takes up its original space on the page.

Behind and underneath that content div, with a slight overlap, I want another div. Since the content div is absolutely positioned and of variable height, I can't relatively or absolutely position my bottom div. So I'm planning to use javascript/jquery to get the height of my content div and position the bottom/behind div relative to it on the page (using absolute positioning).

Is that bad practise, or is that the right thing to do?

Thank you!

Edit: I would assume this is an objective answer, either it is the right way to do things or not...


Solution

  • I would not consider it bad per se, but I would try to prevent having to use JavaScript to do the grand layout of the page. The reason is that layout is something CSS was invented for and most of the time that is enough to get the job done. Sometimes however you just have to use JavaScript because CSS will just not cut it.

    Fot things like this I usually use a css framework that does all this heavy lifting. I absolutely prefer yaml for this.