Forum Moderators: not2easy

Message Too Old, No Replies

Centered div issue

         

rchinoy

1:55 am on May 17, 2007 (gmt 0)

10+ Year Member



Hi everyone,

I've been trying to use a centered div with a border as a container for a form laid out using a table, but have run into a problem in Firefox (oddly, IE behaves in the way I want). Naturally, I've set the width of the container div so that I can use left and right margins set to auto so the div will be centered. However, I would like the div to expand its width automatically, so it will contain the entire table. IE does automatically expand the div to contain the table, but Firefox does not, stubbornly honoring the width setting.

I realize that Firefox is probably behaving in a very standards-compliant way, and that IE is breaking the rules as it often does, but it just so happens I want the IE behavior this time, and need to find some way to get Firefox to behave the same way.

Below is a contrived example of the problem. Just look at it in IE, and then in Firefox. Anyone know of a way to get Firefox to expand the div like IE does?

--------------------------------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Div Problem</title>
<style>
#FormBrdr {
width: 100px;
margin-left:auto;
margin-right:auto;
padding: 5px;
border: 1px solid #003366;
}
</style>
</head>
<body>
<div id="FormBrdr">
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td nowrap>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. In fermentum metus.</td>
</tr>
</table>
</div>
</body>
</html>
--------------------------------------------------------

Thanks,
Russ

Robin_reala

6:46 am on May 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld Russ!

So you want your <div> to act more like a table? That's easy enough, just add

display:table
to its CSS. Your problem though is going to be IE7 which I believe will act like Firefox but not recognise the
display:table
workaround.

The way of doing it which IE will recognise is to set

text-align:center
on the <body>, then set
display:inline-block
on #FormBrdr and remove the margins and width. Inline-block elements shrinkwrap their contents in the absence of a width, which is what we want here. There's a downside to this method though - inline-block doesn't work in Firefox versions less than 3.

The third option is just to dump the <div> and style the <table> directly. I can't see any obvious reason why this wouldn't be possible with your code...

[edited by: Robin_reala at 6:46 am (utc) on May 17, 2007]

rchinoy

11:57 am on May 17, 2007 (gmt 0)

10+ Year Member



Yeah, suspecting this wasn't solvable, I have already dumped the div in the real code. It just seems like using a div this way shouldn't be so difficult, so I had to ask. Thanks for such a complete answer.

Robin_reala

1:31 pm on May 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<div>s are easy, they just work in different ways to that which you'd expect if you're coming from a tables-layout based mentality. But there's usually workarounds regardless.

rchinoy

2:32 pm on May 17, 2007 (gmt 0)

10+ Year Member



Don't get me wrong, I use divs all the time and as much as possible, but something this simple shouldn't send me running back to a table.