Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Fix absolute DIVs not working as expected

Posted on 2015-01-08
3
Medium Priority
?
99 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
[X]
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
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
In this tutorial viewers will learn how to style a decorative dropcap for the first letter in a paragraph using CSS. In CSS, create a new paragraph class by typing "p.fancy": Then, to style only the first letter of the first sentence, include the ps…

722 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