Forum Moderators: not2easy

Message Too Old, No Replies

Behaviour of absolutely positioned SPAN

Different in IE and FF

         

adb64

10:46 am on Nov 13, 2007 (gmt 0)

10+ Year Member



My telecom provider sends me an email each month with an overview of the calls I've made.
When I view this mail in Thunderbird the text is hard to read as part of the text is displayed on top of each other. When I save the HTML and view it in FF it looks the same as in Thunderbird, which could be expected, when viewed in IE however it looks correct.
When I studied the HTML I saw they use absolutely positioned <SPAN> elements to build the page.

The use eg the following:

CSS:
#p123 {position: absolute; top: 100px; left: 50px;}

HTML
<span id="p123">13 November 2007</span>

For each element on the page the use a construction like this, so a lot of CSS elements.

In FF this is rendered as:

13
November
2007

In IE this is rendered as:

13 November 2007

For FF this means that the text partly overlaps with the line(s) below. Changing the font size doesn't help.

Does anybody here knows how an absolutely positioned SPAN should be rendered?

Thanks,
Arjan den Boer

birdbrain

1:46 pm on Nov 13, 2007 (gmt 0)



Hi there adb64,

I tested this code in IE7, Firefox 2 and Opera 9....


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

<style type="text/css">
#p123 {
position: absolute;
top: 100px;
left: 50px;
}
</style>

</head>
<body>

<div>
<span id="p123">13 November 2007</span>
</div>

</body>
</html>


...and I could detect no difference in presentation. ;)

If you say that Firefox presented it differently, then we would need to see the full code that produced the effect.:)

birdbrain

adb64

2:39 pm on Nov 13, 2007 (gmt 0)

10+ Year Member



Birdbrain,

Thanks, you're right. I will check tonight at home what is different in the HTML of the mail from my example.

Xapti

4:04 pm on Nov 13, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As far as I remember, absolutely positioned elements can be any width they want if the width is not specified. This typically means that you'll end up with two possible situations (or two other rarer ones, 0 width or 100% width) - that it would shrink to the longest word/inline element, or that it would fit all inline elements on the first line, possibly until it runs out of space.

adb64

8:18 pm on Nov 13, 2007 (gmt 0)

10+ Year Member



I found out what caused the problem, the SPAN is wrapped in another SPAN, see below:


<!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>
<title>Testpage</title>
<meta content="text/html; CHARSET=iso-8859-1" http-equiv="Content-type" />
<style type="text/css">
<!--
span {padding:0px;position:absolute;}
.FT0 {font-family:"sans-serif"; font-size:13px;}
.PS1 {left:417px;top:117px;}
--></style>
</head>
<body>
<span class="FT0">
<span class="PS1">18 November 2007</span>
</span>
</body>
</html>

Because of the SPAN with class FT0, although the class can be omitted, problem occurs anyway, the inner span will exhibit the behavior described above. If the outer SPAN is removed it will display as expected in FF, same as in IE.
Is there any explanation for this?

adb64

8:33 pm on Nov 13, 2007 (gmt 0)

10+ Year Member



Hmm, did some more testing.
When I replace all SPAN elements with DIV, the same problem occurs. When I change only one of the SPANs, indifferent which one, the problem does not occur.
Is this a bug in the renderer of FF?

Xapti

7:29 pm on Nov 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Perhaps yor problem isn't what I thought, but did you pay attention to what I told you?
You should try specifying a width in EM (tweak it so that it's as small as you need), so that it won't bump anything to a new line.
(I have not tested your problem, just looked at the description)

[edited by: Xapti at 7:30 pm (utc) on Nov. 14, 2007]

adb64

8:53 pm on Nov 14, 2007 (gmt 0)

10+ Year Member



Xapti, thanks. I can't do anything about the layout as it is in an email from my telecom provider which is hard to read. But I try to understand what is wrong (either with the markup of the mail or with the browser).
For as far as I know you can not set a width on an inline element as SPAN is, it will just take up the space what is needed and don't do newlines unless running out of space at the right edge of the parent element.
But the problem here is I think the absolute positioning of both SPAN elements which will take them out of the flow, remove the absolute positioning and all is fine. When using the web-developer add-on in FF and displaying style info (ctrl+shift+y) it won't display any info outside the inner span, only html and not html > body > span.FT0 as could be expected, so what has happened to the outer SPAN?
Is there someone in this forum with an in-depth knowledge of CSS or are we a gray area in which the specification leaves it up to the implementation on how to handle this?

Xapti

12:46 am on Nov 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The W3C specification is here:
[w3.org...]

So as far as I understand, if you don't have width (or right) set, the browser could put line breaks anywhere where it's possible - such as at (breakable) spaces - or only where specified (using <br>). The reason it shrinks only with another span wrapped around it, is because that containing span has zero-width. To accommodate a large object into a small one (or zero-width), it would need to make a line break at every possible spot. The reason IE doesn't do this may be some form of bug, or it may be a random "feature" (quirk).

The solution I mentioned could be implemented for your own machine (if you like using firefox) by using special style settings. A firefox extension like "Stylish" will do this.

[edited by: Xapti at 1:07 am (utc) on Nov. 16, 2007]

adb64

7:12 am on Nov 16, 2007 (gmt 0)

10+ Year Member



Hi Xapti,
Thanks for the information provided!

Arjan