Solved

Fix absolute DIVs not working as expected

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
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.
In this tutorial viewers will learn how to customize the background color and font color of highlighted text using the ::selection element in CSS Begin by defining the selected text as an element in CSS by typing "::selection": Style the ::selection…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

786 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