views:

4749

answers:

4

In every browser I've used, except ie8, an absolutely positioned element can be positioned according to the closest parent with relative positioning.

The below code shows two divs inside a table. The top div has position: relative, however, the nested, absolutely positioned element does not respect its boundaries (in ie8, it gets positioned at the bottom of the page instead of the bottom of the parent div).

Does anybody know a fix for this?

<style>
#top {
position: relative;
background-color: #ccc;
}
#position_me {
background-color: green;
position: absolute;
bottom: 0;
}
#bottom {
background-color: blue;
height: 100px;
}
</style>
<table>
  <tr>
    <td><div id="top"> Div with id="top"
        <div id="position_me"> Div with id="position me" </div>
      </div>
      <div id="bottom"> Div with id="bottom"
        <p>Lorem ipsum dolor sit amet.</p>
      </div></td>
  </tr>
</table>
+5  A: 

Declare a doctype. I added this to the top of my document, and it works:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;

Don't use that specific doctype unless you are going to code for XHTML 1.0 Strict standards.

Jonathan Sampson
No, you shouldn't use the XHTML strict doctype unless you code with XHTML strict!
MiffTheFox
No what? I didn't tell him otherwise. He didn't say which style he was working with, so I went with what I typically use.
Jonathan Sampson
I had a (lousy) doctype declaration on the "real" page (not my example). Lousy doctype = <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">I changed to this doctype and it works now: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
+4  A: 

That's becuase you're not using the document type. And IE working in the "quircks" mode.

Try this doctype:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
Sergei
+8  A: 

Add this:

#top {
//height: 100%;
}
#position_me {
//left: 0;
}

It forces IE8 to compute position correctly in quirks mode. There are many ways to get it:

//zoom: 1;
//writing-mode: tb-rl;

See http://haslayout.net/haslayout

utype
A: 

You can also use this

<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
goksel