Can't figure out difference between ffox, chrome

I’ve got the following code, and in Firefox, the span is showing up slightly above where I got it positioned in Chrome… just a couple pixels up. I can’t see why there would be a difference, though. I’m not using a full reset stylesheet, but I did double check with firebug what rules are applying to it and nothing appears to be different.
I will try throwing a reset on, but I’m also quite curious to know what might be causing the difference


<div id="bagfloat">
   <span id="bagitemnum">4</span>
</div>

#bagfloat {
background:url("../images/shoppingbagfloat.png") no-repeat scroll 0 0 transparent;
bottom:10px;
height:83px;
position:fixed;
right:2%;
width:83px;
z-index:98;
}

#bagitemnum {
color:#444444;
font-family:Helvetica,Arial,sans-serif;
font-size:18px;
left:64px;
position:absolute;
top:9px;
}

* {
margin:0;
padding:0;

The only thing I noticed was a slight difference in line-height since you had not specifically set it, it was using the default from each browser. At least with that code you posted that is, not sure about your actual page.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
 "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Test</title>

<style type="text/css">
body {
    margin:0;
    padding:0;
}
#bagfloat {
    background:orange;
    bottom:10px;
    height:83px;
    position:fixed;
    right:2%;
    width:83px;
    z-index:98;
}
#bagitemnum {
    color:#444;
    background:#FFF;
    [COLOR=Blue]font:18px/22px [/COLOR]Helvetica,Arial,sans-serif;
    left:64px;
    position:absolute;
    top:9px;
}
</style>

</head>
<body>

<div id="bagfloat">
   <span id="bagitemnum">4</span>
</div>

</body>
</html>

ah yes… that did it. thanks a lot!

I guess that not everything that is set ends up showing up in firebug… I had kind of thought all the properties would be, since I’ve noticed it showing rules from html.css. So how does it decide which to show i wonder, and is there a good way of detecting differences like that? oh wait… i guess that’s the computed tab. still not clear what does and doesn’t show up from html.css in the style tab though.

still not clear what does and doesn’t show up from html.css in the style tab though

I removed the 22px line-height I had set on it and ran it through firebug. I could not find line-height on any of the elements in html.css. It looks like the browser computes that on it’s own, looking at the FF computed styles it did show that it computed the line-height at 22px (just as I had set it) which was very close to 1.2

Using developer tools in chrome it had computed the line-height to normal which is typically 1.2 for all browsers. However if you take 18px and multiply it x 1.2 that gives you 21.6

That was the difference between FF and chrome when left to determine the line-height according to their computed styles. FF was larger by .4 if chrome was actually using 21.6

It’s about rounding up. Browsers can’t use fractions of a pixel.

So 18px*1.2 = 21.6px. Now, Ch will always round it down to 21px, while FF will do it right: 22px if it’s equal or greater than 21.5px.

I did a piece on how UAs behave with rounding up: here. Whenever rounding is involved you should expect different behaviour from different UAs.

@noonnope - Amongst programmers, ROUND is considered the ‘incorrect’ behavior because it’s a long logic operation, while trunc is a simple math operation. Difference between 80 clock cycles on a 8088 and 2 clock cycles… and even though today it’s only the difference between 4 clocks and 1, the preference for the simpler operation remains.

That said, there is NO proper behavior to say “do it right” – because the specification does not actually say how fractions of a pixel should be handled by the UA.

Though rounding off is just the tip of the iceberg when it comes to line-height issues. One of my core rules for making sure my code is predictable cross-browser is “You CANNOT trust the default or inherited line-height, so you MUST always redeclare line-height when you change font-size”.

The default line-height isn’t even defined in the specification and is COMPLETELY left up to the user agent. The MOST you will find is the SUGGESTION that user agents use 110% to 130%… which is why MOST browsers (but not all) default to 120%. For the most part you can assume 120% to be the default, but it often seems that Gecko based browsers randomly use 115% to 125% depending on font-face and font-size. This can REALLY drive you nuts looking at pages written in FF on some linux distro’s.

side note – This is more ‘proof’ in the specification of the original intent; not saying how things should appear and leaving that up to the User Agent. Whenever anything about appearance is in the HTML 2 and 4 specifications you will see words like “may” and “often” and not words like “should” or “must”.

Unlike every other browser maker under the hood gecko keeps track of ALL fractions regardless of how they are rounded during rendering. This means if you have a line-height that by the math works out to 10.4px, three elements of that size in a row will render at 10 (10.4), 11 (20.8) and then 10px (30.2) again. THAT can really drive you nuts and is part of why every other browser rounds down and throws away fractions; it’s also part of why gecko’s renderer is often slower than even IE 6 on certain things!

Then you have the issue of odd inheritance; there’s an example I use to show this:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html
	xmlns="http://www.w3.org/1999/xhtml"
	lang="en"
	xml:lang="en"
><head>

<meta
	http-equiv="Content-Type"
	content="text/html; charset=utf-8"
/>

<meta
	http-equiv="Content-Language"
	content="en"
/>

<style type="text/css">
body {
	padding:8px;
}

div {
	margin:0;
	padding:0;
}
#linkHeights {
	font-size:20px;
	line-height:200%;
}

#linkHeights div {
	font-size:14px;
	background:#CCC;
}

#linkHeights .test2 {
	line-height:200%;
	background:#EEE;
}
</style>

<title>
	Line-Height Oddity Demo
</title>

</head><body>

<div id="linkHeights">
	<div class="test1">
		Test
	</div>
	<div class="test2">
		Test
	</div>
</div>

</body></html>

You try that out and .test1 will end up 40px tall, while .test2 will end up 28px tall despite BOTH having 14px fonts declared on them and 200% line-height. This is because percentage line-heights are calculated and then inherited as PX. This one can REALLY make you tear your hair out.

Bottom line, ALWAYS redeclare your line-height when you change font-size; which is why I always just use the condensed font property and redeclare style and face too since it ends up about as many characters as declaring font-size and line-height separately.

That said, I’m wondering why you bother having two separate elements for that… AND why you have an id on the inner element.

<div id=“bagfloat”>4</div>

with this for CSS:


#bagfloat {
	position:fixed;
	bottom:10px;
	right:2%;
	z-index:98;
	width:83px;
	padding:9px 0 54px;
	text-indent:64px;
	font:normal 18px/20px arial,helvetica,sans-serif;
	color:#444;
	background:url("../images/shoppingbagfloat.png") 0 0 no-repeat;
}

Mind you I’m just guessing, but really there should be no need for that extra element since you can just skip declaring height and use line-height + padding to add up to the 83px, then use text-indent to push the text over that 64px you seem to want.

My test case, % to px, demonstrates, in fact, that you do have a proper way to do it: UAs have to make sure that a 100% = 33%+49%+18% fill for the parent is accomplished as it is when using px: 350px = 116px+172px+62px.

UAs are required to make it so, and that can be accomplished rounding up and/or down, not just down. If not, than using % is just a… fling?

That said, I’ve noticed the body CSS declaration on your previous examples and appreciate it :slight_smile:

They don’t “have to make sure” because the specification doesn’t say how rounding should be handled! At all! As such don’t expect major changes in that.

No, they are not required to do so, the specification doesn’t say that ANYWHERE. As a rule “real” programmers from languages like C avoid using calculations that result in floating point values in the first place because there are ALWAYS accuracy errors… because of that:

Not a fling, just a REALLY BAD IDEA for anything that requires accuracy. See what a retard FF can be about 50% in relationship to ‘center’… or how badly it can screw up the definition of “right:0” depending on browser width.

There’s a reason that when it comes to rounding even if their total does add up to the proper 100% I still say that overall their method is the more broken of the two; or at least a heck of a lot more of a pain in one’s tuchas. (but to put that in perspective, I’m the nutjob REALLY looking forward to being able to use box-sizing:border-box )

The example you linked to is a perfect case of where I would NEVER use % in the first place; on widths of elements stacking – I would always ALWAYS leave at least one of them dynamic so the rounding error does NOT rear it’s ugly head – as a rule I would generally make two fixed width and a dynamic because of that.

USUALLY on line-height I would use %/EM because the total height doesn’t matter as the parent should be dynamic width. In this case there’s a image interaction and a desired fixed height, so %/EM is completely impractical in the first place and should NOT be used. (even if that means FF’s “resize like a retard” ends up broken)

My demo about line-height above used 200% on purpose BTW – it means there are no rounding errors with the 20px and 14px font-size.

I believe specs don’t need to reinforce math rules :slight_smile:

Transformations have to be proportionate… as a whole. If those transformations are done right by at least two UAs, there is still hope.

Not a fling, just a REALLY BAD IDEA for anything that requires accuracy.

Fluid layouts aren’t usually about “pixel perfect”.