[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How do I position two DIV's at the same coordinate?

Posted on 2005-05-12
6
Medium Priority
?
454 Views
Last Modified: 2008-03-17
Hi Experts!
I'm trying to place a <div></div> over another <div></div>.

My problem is that I do not know where the first <div></div> will be located in terms of absolute coordinates.
And I somehow need to find the location of the first <div></div> and then place another <div></div> on top of it.
The reason I do not know the coordinate of the first <div></div> is that, it's going to be placed below an article which will have various lentghs.

And I do not want to use frame or iframe... is there any possible ways?
0
Comment
Question by:nagunpyo
6 Comments
 
LVL 14

Expert Comment

by:kiddanger
ID: 13994007
If it has an id, you can retrieve the top left location.

The code:

<HTML>
<HEAD>
<TITLE></TITLE>
<script type="text/javascript">
function moveme() {
  var d1 = document.getElementById('div1');
  var d2 = document.getElementById('div2');
  d2.style.top = d1.style.top;
  d2.style.left = d1.style.left;
  }
</script>
</HEAD>
<BODY>

<div style="top: 50px; left: 150px; position: absolute" id="div1">div 1</div>
<div style="top: 150px; left: 50px; position: absolute" id="div2">div 2</div>
<span style="cursor: pointer" onclick="moveme()">click to move div 2 on top of div 1</span>

</BODY>
</HTML>

A working example:
http://kiddanger.com/lab/ee/whereismydiv.html
0
 

Author Comment

by:nagunpyo
ID: 13994227
my div 1 won't have top and left properties...
it'll be placed below an article without any locations given... :-(
0
 
LVL 14

Accepted Solution

by:
kiddanger earned 1800 total points
ID: 13994340
Ok, try the link again.  This will work.

<HTML>
<HEAD>
<TITLE></TITLE>
<script type="text/javascript">
function moveme() {
  var d1 = document.getElementById('div1');
  var d2 = document.getElementById('div2');
  d2.style.top = d1.offsetTop;
  d2.style.left = d1.offsetLeft;
  }
</script>
</HEAD>
<BODY>
<br />
<br />
<br />
<br />
<br />
<div id="div1">div 1</div>
<div style="top: 150px; left: 50px; position: absolute" id="div2">div 2</div>
<span style="cursor: pointer" onclick="moveme()">click to move div 2 on top of div 1</span>

</BODY>
</HTML>
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 53

Assisted Solution

by:COBOLdinosaur
COBOLdinosaur earned 200 total points
ID: 13994822
kiddanger you have the right code, but for standards compliance and better cross-browser support you need to include the unit of measure when you set the style:

d2.style.top = d1.offsetTop+'px';
d2.style.left = d1.offsetLeft+'px';

And for best practices in the CSS topic we normally include a doctype when we post code just to encourage the user to use them for better cross-browser support.

Cd&

0
 

Author Comment

by:nagunpyo
ID: 13996097
Thank you :-)
0
 
LVL 30

Expert Comment

by:GrandSchtroumpf
ID: 13996118
Don't need javascript for that.  Using javascript for layout is never a good solution.
When using absolute positioning, the coords you set are relative to the nearest (in the document hierarchy) container that is positionned.
So, just put your 2 divs in a container div that has "position: relative" and use "position:absolute; top:0; left:0" on the top div.  That's all.

Here is an example (the class attributes are not needed here because i use inline styles, i just included them to "label" the divs):

<div class="Long Article">
  This is the very long article
</div>

<div class="Container" style="position: relative; border: 2px solid red; padding: 5em;">
      <div class="BottomLayer">
            This is the bottom layer
      </div>
      <div class="TopLayer" style="position: absolute; top:0; left:0; border: 2px solid blue; padding: 1em;">
            This is the top layer
      </div>
</div>
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating a CSS block that only applies to printing By default, all of your CSS applies to every possible view of your page - whether on screen, printed, landscape, touch-screen, or whatever.  You can, however, add CSS that only applies under certai…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how to position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses
Course of the Month17 days, 21 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question