Solved

Fix absolute DIVs not working as expected

Posted on 2015-01-08
3
73 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 250 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 250 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Joomla Website Malfunction 9 50
Box Model and Styling 12 20
Css issue on scroll 1 18
SVG Logo 4 18
Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
This article discusses how to create an extensible mechanism for linked drop downs.
In this Micro Tutorial viewers will learn how to create a CSS image sprite (In a later tutorial, viewers will learn how to use CSS and HTML to create a navigation menu using this sprite) Open a new Photoshop document with a width of (Icon width)x(N…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now