Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Fix absolute DIVs not working as expected

Posted on 2015-01-08
3
Medium Priority
?
104 Views
Last Modified: 2015-01-09
I must not understand well how relative/absolute DIVs work. I have a page of the form:

<DIV id="d1" style="position: relative">
  <div style="position: absolute; top: 100px; left: 100px">
     some text
  </div>
</DIV>

<DIV id="d2" style="position: relative">
  <div style="position: absolute; top: 100px; left: 100px">
     more text
  </div>
</DIV>

I expected d2 to be beneath d1 and for "more text" to be in d2 beneath "some text" each at the 100,100 offsets. However what I get is "more text" printing on top of "some text". How do I fix this?

Here is an example:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body style="margin: 0; padding: 0; border: 0; font-family: Arial; font-size: 10pt">

<DIV style="position: relative; width: 8.5in;">

  <Div style="position: absolute; top: 0.84in; left: 0.4in;">
    Copy B, copy 1
  </Div>

  <Div style="position: absolute; top: 0.84in; left: 4.5in;">
    COPY B, copy 2
  </Div>

  <Div style="position: absolute; top: 6.35in; left: 0.4in;">
    COPY C, copy 1
  </Div>

  <Div style="position: absolute; top: 6.35in; left: 4.5in;">
    COPY c, copy 2
  </Div>

</DIV>

<div style="page-break-before:always"></div>

<Div style="position: relative; width: 8.5in;">

  <Div style="position: absolute; top: 0.84in; left: 0.4in;">
    p2 COPY B, copy 1
  </Div>

  <Div style="position: absolute; top: 0.84in; left: 4.5in;">
    p2 COPY B, copy 2
  </Div>

  <Div style="position: absolute; top: 6.35in; left: 0.4in;">
    p2 COPY C, copy 1
  </Div>

  <Div style="position: absolute; top: 6.35in; left: 4.5in;">
    p2 COPY c, copy 2
  </Div>

</DIV>
</body>
</html>

Open in new window

0
Comment
Question by:jmarkfoley
3 Comments
 
LVL 25

Assisted Solution

by:Kyle Hamilton
Kyle Hamilton earned 1000 total points
ID: 40537825
an absolutely positioned element doesn't take up any space within the flow of the document. so the relatively positioned container will have a height of 0. the container below it (id2) also has a height of 0. so they will appear to be on top of each other. since each of the absolute elements has an offset of 100, their content will also appear on top of each other 100 px below the top of the containers (which are on top of each other)
0
 
LVL 53

Accepted Solution

by:
COBOLdinosaur earned 1000 total points
ID: 40538283
Kyle is correct. give the divs a height attribute; though for some older browsers you might also need to put in a non-breaking space &nbsp; to insure the have form.

Also do us all a favor and use lower case for all tags.  Technically, that is how they are defined in the specification and some day browsers might treat DIV or Div as invalid tags.

Cd&
0
 
LVL 1

Author Closing Comment

by:jmarkfoley
ID: 40540518
Setting my <DIV> height to 11in did the trick. Thanks. I'm splitting the points because Kyle described the problem, but COBOLdinosaur gave the actual solution.

btw - if they ever decide to make tags case sensitive I've got a whole lot bigger problems than just div tags!!!
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

916 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