Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

106
Views
How to create element with absolute position

I want Date to appear above Second section. plz guide how to achieve it

    var sectionTwo = document.getElementById("sectionTwo");
    var x = sectionTwo.getBoundingClientRect();
    var b = document.createElement("div");
    b.style.position = 'fixed';
    b.innerHTML = new Date();
    document.body.appendChild(b);
    b.style.top = (x.top - b.style.height) + 'px';

enter image description here

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

One way to do it is like this:

HTML code:

<div id="sectionTwo">

</div>

JS code:

var sectionTwo = document.getElementById("sectionTwo");
var b = document.createElement("div");
b.innerHTML = new Date();
sectionTwo.insertAdjacentElement("beforebegin", b);

CSS code (just to for visualization):

#sectionTwo {
  width: 80%;
  height: 50px;
  background-color: gray;
}
about 4 years ago · Juan Pablo Isaza Report

0

<div>
<div style="height:800px;"></div>
<div id="sectionOne">First Section</div>
<div id="sectionTwo">Second Section</div>
<button onclick="showDate();">toggle</button></div>



<script> function showDate() {
    var sectionTwo = document.getElementById("sectionTwo");
    var x = sectionTwo.getBoundingClientRect();
    var b = document.createElement("div");
    b.style.position = 'absolute';
    b.innerHTML = new Date();
    document.body.appendChild(b);
    b.style.bottom = 0;
    b.style.top = (x.top - b.offsetHeight) + 'px';
}</script>

enter image description here

about 4 years ago · Juan Pablo Isaza Report

0

Maybe this could help you better.

var sectionTwo = document.getElementById("sectionTwo");
var b = document.createElement("div");
b.innerHTML = new Date();
sectionTwo.insertAdjacentElement("beforebegin", b);
#sectionTwo {

  height: 35px; width:35px;
  background-color: pink;
}
<div id="sectionTwo">

</div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!