Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Centered float lefts work in Opera but not IE

Status
Not open for further replies.

theblackgold

Technical User
Dec 3, 2009
4
US
I have several centered, equal-sized float left div's with text inside of them, with a fixed width sidebar on the right. My intent is that, if the screen is wide enough, they will all sit side-by-side and centered on the space not taken by the sidebar. When the screen is too narrow, they start to stack on each other in sort of a grid.

I've gotten it to work perfectly in Opera but it fails in IE, where although everything stacks properly with screen size, it's just always floated to the left of the screen, so I'm assuming my centering logic is failing for IE, but I can't figure out why. Here's the relavent code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "<html xmlns="<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="../_css/main.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="wrapper">
<div id="sidebar">
</div>
<div id="main">
<div id="list">
<span class="item">
</span>
<span class="item">
</span>
<span class="item">
</span>
<span class="item">
</span>
<span class="item">
</span>
</div>
</div>
</div>
</body>
</html>



And the CSS:

@charset "utf-8";

body {
padding: 0;
background: #000;
margin: 0;
font-family: "Lucida Console", Monaco, monospace;
font-size: 14px;
line-height: 16px;
color: #CCC;
}
#wrapper {
max-width: 1920px;
min-width: 985px;
padding-right: 10px;
padding-left: 10px;

}
#sidebar {
width: 200px;
float: right;
}
#main {
margin-right: 200px;
}
#main #list {
margin-top: 5px;
position: relative;
float: left;
left: 50%;
}
#main #list #item {
width: 315px;
margin-bottom: 10px;
padding: 5px;
height: 235px;
margin-right: 10px;
border: 1px solid #0F3;
background: #333;
float: left;
position: relative;
left: -50%;
}


It took a bit of research to figure out using relative positioning with plus and minus 50% left values could center something, but I can't figure out why it doesnt work in IE when from what I read it should? I guess I'm missing something simple?
 
IE is not standards compliant and very buggy. For example, if you make an area that's 100 pixels wide then inside it you make two columns of 50 pixels each. IE figures the two columns add up to be 104 pixels wide and therefore doesn't fit properly inside the 100 pixel wide area and totally messes up the layout. It doesn't matter if you use pixels, percents or any other measuring system, IE can't add and different versions of IE will add things up to different amounts.

This is only one many many IE bugs that cause web developers to pull out their hair, but it doesn't mean you can't get a proper layout in IE. It just takes a lot of patience and experience with each version of IE to figure out what can and can't be done and how to get around all of IE's bad behavours.

Hmmm... i see your style sheet uses a negative number to position a background. Negative numbers are another area where IE has major problems.

IE 8 has behaves much better but it too isn't nearly as good as any of the other major browsers on the market and it'll be a long time before we can stop developing for older versions of IE.
 
Yea, I'm not too experienced with older browsers and their quirks so I just do what I can when I can to get it to work as best as possible. Other parts of the website design use negative numbers and it seems to work for now, and I kinda solved my problem here without using negative numbers. Using text-align center on the containing div and then display inline-block for the elements that move around. It seems to work in all the modern browsers but I think I remember older IE versions not liking inline-block.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top