Forum Moderators: not2easy

Message Too Old, No Replies

Floated div with negative margin and IE

Goes behind containing div's border

         

gasell

2:41 pm on Jul 10, 2007 (gmt 0)

10+ Year Member



I'm having with trouble with the following:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head profile="http://gmpg.org/xfn/11">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Title</title>

<style type="text/css" media="screen">
body{
text-align:center;
font-size:0.8em;
}
#holder{
margin:auto;
width:600px;
border-width:0 10em 0 1em;
border-style:solid;
border-color: #de8936;
}
#content{
float:left;
width:570px;
margin:0;
padding:1em 15px;
}
#sidebar{
float:left;
margin:0 -20em 0 0;
width:9em;
padding:0.5em;
background:#44607d;
}
</style>
</head>

<body>
<div id="holder">
<div id="content">Some text</div>
<div id="sidebar">Some text</div>
</div>
</body>
</html>

With IE version under 7(according to browsershots.org and tested with full source, hopefully I didn't remove anything relevant) sidebar is behind holder border, but I want it on top of the border.
Also as a more minor issue, in Opera and IE7, it won't stay on border precisely being either slightly wider or narrower.

coyoteRick

4:18 pm on Jul 10, 2007 (gmt 0)

10+ Year Member



Is there a particular reason you favor EM units? I usually reserve the relative EM unit for font sizes and not the block elements within my layout. The abundant usage of em units is probably causing a lot of your measurement & positioning problems for you.

Also, when defining a width on an element, it is typically not good practice to also define a padding to that element. It's best to define padding on the child elements instead. Otherwise, the padding & width that you define end up adding up together and increasing the overall width of the element in some browsers. This is badly affecting your content division causing a horizontal and vertical scrollbar to appear. It displays like this in both FF and IE7 for me. Border widths also add on to the ultimate size of a fixed width element with some browsers.

I would encourage you to try replacing some of you relative EM measurements with PX units... EM measurements are relative to the elements font size whereas PX units are relative to the screen resolution.

gasell

6:09 pm on Jul 10, 2007 (gmt 0)

10+ Year Member



I use ctrl++ quite a lot myself whenever text seems too little for me and it usually breaks a layout since text won't fit in its container anymore. Therefore I like measuring with em, to give users such a freedom too. However I need to use px for some things that depend on header image (actual layout has one). This is also the reason why left-right padding is given with pixels instead. Otherwise it would increase the width of content and that would make such a layout impossible. But sidebar containing links should grow with text. Also notice that even though I could make whole right border into a div and fit sidebar in it, I had the layout before I decided to add sidebar, plus I would have to deal with make-divs-equal-height issue.

Problems I've seen so far is that sidebar is in the right place but behind holder or that 10em border and 10em sidebar aren't of equal width. One IE 5.x version showed it under and shifted but I wouldn't consider that a big issue if it were the only one. I haven't tried z-index yet, because I've always thought it's for absolute positioning.

When it comes to padding and margins adding width to an element then my experience so far is that avoiding quirks mode let's you rely on room-taken=width+padding+margin+border. At least in newer browsers.

Do I get it right that you're seeing a horisontal/vertical scrollbar? I sure didn't.

At the moment I wouldn't like to give up em widths and would like to know why 10!=10 and if there's a way to make IE6 (at least) to display like IE7 does, even if it needed css applied only to IE6 alone. And in the end showing sidebar on top is far more important than 10!=10 because the ladder doesn't make my site less usable and is more my perfectionism issue.

gasell

10:33 am on Jul 11, 2007 (gmt 0)

10+ Year Member



It was still probably not behind border but not displayed at all. IE is a bit buggy when it comes to negative margins as I dicovered.

Added position:relative; to floated sidebar and now it displays in all browsers. IE5.x makes content narrower and therefore sidebar is shifted but it is usable. In IE other versions sidebar is about 1px narrower than border and in Opera as much wider.

I'd be great if someone could come up with further improvements as I've run out of ideas and can't access IE5 othr than using browsershots.