Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Fix absolute DIVs not working as expected

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
styling bootstrap grid system with CSS 2 38
Show Ruler in Outlook 2010 STILL NOT SHOWING! 4 23
AJAX Wordpress Not Reading Variable 2 27
Search Item in Table 2 19
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

860 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