Forum Moderators: not2easy

Message Too Old, No Replies

inline vs. block -- relative vs. fixed padding

td div inline block relative fixed padding

         

Dabrowski

6:12 pm on Apr 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've discovered a strange inconsistency with padding/margins.

I don't know if it's a block vs. inline issue, I was hoping someone can clarify this. I believe a TD is an inline element?

The issue is, when specifying padding on a TD as 10px, it is added onto the width, when specifying as 1em, it is included in the width. Doesn't happen with DIVs I know these are blocks.

Test page to demonstrate:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>

<head>
<style>

BODY{font-size: 62.5%;}/* 1em should be 10px! */

.one{padding: 10px;width: 500px;height: 100px;border: 1px solid red;}
.two{padding: 1em;width: 500px;height: 100px;border: 1px solid blue;}

</style>
</head>

<body>

<table><tr><td class='one'>TD Width 500px, height 100px, padding 10px</td></tr></table>
<table><tr><td class='two'>TD Width 500px, height 100px, padding 1em</td></tr></table>
<br><br>
<div class='one'>DIV Width 500px, height 100px, padding 10px</div>
<div class='two'>DIV Width 500px, height 100px, padding 1em</div>

</body>

</html>

This is not a problem, just a query and hopefully a learning experience.

Fotiman

6:31 pm on Apr 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Interesting. You should note, Firefox does not exhibit this behavior (IE6 does... don't know about IE7).

Fotiman

6:36 pm on Apr 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Also note that the difference being shown is not equal to 1em. That is, try changing the em value to, say, 8em and you'll see that the width of the td does not change.

Dabrowski

7:06 pm on Apr 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here are exact values, including the 1px border:

IE7 in PX: 522x122 (padding and border added)
IE7 in EM: 502x102 (only border added)

FF in PX: 522x100
FF in EM: 522x100

It seems FF adds padding and border to width only in both cases, and IE adds includes the EMs in both width and height.

Anyone try this in IE6?

Fotiman

7:27 pm on Apr 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I've tried it in IE6... like I said, the difference it displays is not necessarily directly related to the 1em. That is, setting a padding of 2, 3, or 4 em does not change the width of the td any.

SuzyUK

7:42 pm on Apr 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



interesting.. is it just another case of IE being unable or unwilling to resolve previous calculations?

Fotiman.. yes what you're seeing in IE6 is what Dabrowski is saying.. the difference as I understand isn't related to 1em.. it's the fact that IE doesn't add (any) em padding to the width of the table cell i.e. not adhering to the box model.

I tried using 1em borders too IE adds them OK, but try percentage padding.. both IE and FF have fun with it ;)

SuzyUK

7:54 pm on Apr 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I believe a TD is an inline element?

No it's a "flow element" definition of TD [w3.org] - which means it can contain both block and inline elements, technically it's display setting should be

display: table-cell;
but IE doesn't support that and it gives a td -
display: block;
(via Dom Inspector)

which makes it even stranger that it doesn't treat it as a block? - changing the display of the TD to inline-block makes no difference either..

Dabrowski

9:14 pm on Apr 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Whoa!

I'm glad it's not just me.

Suzy I tried with 2% (10px based on 500px), FF and IE are almost the same.

IE on TD: 502x102, same as with EM.
IE on DIV: 542x142, for some reason has doubled the padding, but text is in correct place. So somehow it's added 2% to left side, which is fine, but 6% to right side?

FF on TD: 502x100, it's now also forgotten about the borders top and bottom.
FF on DIV: 541x141, again doubled padding like IE, but also lost 1px vertical and horiz. Work that one out?

Fotiman, yes the fact that it hasn't widened the TD is the point, but the text is NOT right on the left so padding is there.

I'm annoyed because I found the problem whilst writing a JS, and on my test page it DOES affect FF too, but I can't get it to do it on FF now for you guys to look at.

They both get DIVs right which I suppose is good, but the fact that they mess up tables differently makes it even harder for us to use them. Damn W3C!

Dabrowski

11:10 pm on Apr 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Fiddled about with %ages some more, if you wrap the content in a DIV with padding: 0 and a border to see where the edges are, you can see FF places the padding 20px each side, the padding is exactly doubled all the way around.

IE has top/bottom padding 20px, doubled, but left/right has 10px, then the inner border, then another 10px! As if it's inherited it. It obviously hasn't as the text remains in the same place when you remove the inner DIV!

No wonder people don't use fluid/elastic layouts!

Dabrowski

11:42 pm on Apr 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have fixed this with my TR height fix JS.