By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,495 Members | 1,320 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,495 IT Pros & Developers. It's quick & easy.

<img width="100px" ...> valid (X)HTML?

P: n/a
Hi everyone,

I've got a question about what makes the "img" element's width/height
attributes valid HTML or XHTML.

First of all, this is a rather theoretical question, but digging through
the W3C HTML 4.01 standard and this group's archive didn't give me a
satisfactory answer, so here we go:

Is <img src="img.png" alt="" width="100px" height="100px"really valid?

That kind of width/height specification is certainly accepted by the W3C
validator (no matter which version of HTML or XHTML you use), but to my
surprise, I found out that the validator accepts virtually everything
for the width/height attributes, even negative and non-numeric values,
for example:

<img src="img.png" alt="" width="-100" height="fifty">

According to [1], width/height are of type "length", which is either a
percentage ("50%" is explicitly stated as an example) or "%Pixel" [2].
There it says that a pixel value is an "integer representing length in
pixels".

So if the standard requires an integer value for width/height, why does
the validator accept non-integer values? And why doesn't the standard
require unsigned integers?
[1] http://www.w3.org/TR/html401/struct/....html#h-13.7.1
[2] http://www.w3.org/TR/html401/sgml/dtd.html#Pixels
--
Christian Hackl
Dec 17 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Christian Hackl wrote:
Hi everyone,

I've got a question about what makes the "img" element's width/height
attributes valid HTML or XHTML.

First of all, this is a rather theoretical question, but digging through
the W3C HTML 4.01 standard and this group's archive didn't give me a
satisfactory answer, so here we go:

Is <img src="img.png" alt="" width="100px" height="100px"really valid?

That kind of width/height specification is certainly accepted by the W3C
validator (no matter which version of HTML or XHTML you use), but to my
surprise, I found out that the validator accepts virtually everything
for the width/height attributes, even negative and non-numeric values,
for example:

<img src="img.png" alt="" width="-100" height="fifty">

According to [1], width/height are of type "length", which is either a
percentage ("50%" is explicitly stated as an example) or "%Pixel" [2].
There it says that a pixel value is an "integer representing length in
pixels".

So if the standard requires an integer value for width/height, why does
the validator accept non-integer values? And why doesn't the standard
require unsigned integers?
I'm sure others will explain this in more detail, but the summary is
that validity is only a subset of compliance.
Dec 18 '07 #2

P: n/a
..oO(Christian Hackl)
>I've got a question about what makes the "img" element's width/height
attributes valid HTML or XHTML.

First of all, this is a rather theoretical question, but digging through
the W3C HTML 4.01 standard and this group's archive didn't give me a
satisfactory answer, so here we go:

Is <img src="img.png" alt="" width="100px" height="100px"really valid?
No, but ...
>That kind of width/height specification is certainly accepted by the W3C
validator (no matter which version of HTML or XHTML you use), but to my
surprise, I found out that the validator accepts virtually everything
for the width/height attributes, even negative and non-numeric values,
for example:

<img src="img.png" alt="" width="-100" height="fifty">
.... an SGML validator like the W3 validator cannot check attribute
values. A schema validator can, which is what you should use to validate
XML/XHTML.

Micha
Dec 18 '07 #3

P: n/a
Michael Fesser wrote:
.oO(Christian Hackl)
>>
Is <img src="img.png" alt="" width="100px" height="100px"really valid?

... an SGML validator like the W3 validator cannot check attribute
values.
Thanks! That actually makes sense. I've just found out that there's even
a chapter in the HTML 4.01 standard that explains this problem:

| Specifically, an SGML parser ensures that the syntax, the structure,
| the list of elements, and their attributes are valid. But for
| instance, it cannot catch errors such as setting the width attribute
| of an IMG element to an invalid value (i.e., "foo" or "12.5").
| Although the specification restricts the value for this attribute to
| an "integer representing a length in pixels," the DTD only defines it
| to be CDATA, which actually allows any value. Only a specialized
| program could capture the complete specification of HTML 4.

http://www.w3.org/TR/html4/sgml/intro.html
A schema validator can, which is what you should use to validate
XML/XHTML.
Do you recommend any specific schema validator?

Googling for "schema validator xhtml" revealed this tool:
http://schneegans.de/sv/

It catches the width/height errors. Looks pretty good to me.
--
Christian Hackl
Dec 18 '07 #4

P: n/a
Scripsit Michael Fesser:
>Is <img src="img.png" alt="" width="100px" height="100px"really
valid?

No, but ...
Yes it is. It is valid but incorrect (by the specifications). In
practical terms, browsers tend to ignore the "px" part, but that's a
different issue.
><img src="img.png" alt="" width="-100" height="fifty">

... an SGML validator like the W3 validator cannot check attribute
values.
Yes it can and it does (and the W3C validator is actually a mixed SGML
and XML validator, with all the usual consequences of a compromise). In
this case, it checks against the attribute declaration, which is CDATA,
which means pretty much anything, including all the cases mentioned
here.
A schema validator can, which is what you should use to
validate XML/XHTML.
A schema validator, using suitable schema, can check much more than a
DTD validator, for sure. However, this is about XML (including XHTML),
and we can see from the lack of the magic "/" that the OP is using
classic, nominally SGML based HTML.

Moreover, there is no normative schema for XHML 1.0. The schemas are
just unofficial attempts at capturing the syntax requirements (partly
formal, partly prose) in the XHTML 1.0 specification.

--
Jukka K. Korpela ("Yucca")
http://www.cs.tut.fi/~jkorpela/

Dec 18 '07 #5

P: n/a
Jukka K. Korpela schrieb:
A schema validator, using suitable schema, can check much more than a
DTD validator, for sure. However, this is about XML (including XHTML),
and we can see from the lack of the magic "/" that the OP is using
classic, nominally SGML based HTML.
No no, that's a misunderstanding. I thought the "img" issue was the
same, no matter whether HTML or XHTML was used. That's why I wrote
"(X)HTML". I just didn't want to make my posting unnecessarily long by
writing

<img src="img.png" alt="" width="100px" height="100px"or <img
src="img.png" alt="" width="100px" height="100px" />

The question, in fact, arised in the context of XHTML :)
Moreover, there is no normative schema for XHML 1.0. The schemas are
just unofficial attempts at capturing the syntax requirements (partly
formal, partly prose) in the XHTML 1.0 specification.
What about XHTML 1.1?

Are you telling me that there is no official (W3C) definition of what is
conforming XHTML 1.0?

In any case, I gained a lot of new insights from this thread, thank you all.
--
Christian Hackl
Dec 18 '07 #6

P: n/a
Jukka K. Korpela wrote:
The situation is the same. On the other hand, XHTML 1.1 is an exercise
in futility. Even if you fake it to be text/html (there's an eternal
debate over the issue whether this is even "legal")
The W3C seems to endorse that practice, but common sense says as sending
something that's not HTML as text/html is a lie, leading to
interoperability problems.

This make me think of those baby games, where you've to put shapes in the
right holes.
http://store.babycenter.com/images/e...ture_image.jpg

Serving XHTML as text/html is like finding the cube (XHTML) "much
prettier" than the cylinder (HTML), but as there's no hole (in IE) for the
cube, striking with a hammer on the cube to make it fit in the cylinder
hole (text/html).
After having been crushed by a hammer, the cube isn't "pretty" anymore.

--
If you've a question that doesn't belong to Usenet, contact me at
<tabkanDELETETHISnaz at yahoDELETETHATo.fr>
Dec 18 '07 #7

P: n/a
Jukka K. Korpela scrisse:
Scripsit Christian Hackl:
>(which allows you, among other
things, to formally define that "width" and "height" must be integer
values).

Well, such a requirement _can_ be expressed in a DTD, assuming it means
restricting them to unsigned integers and you use an SGML-based DTD. [...]
On the other hand, even in HTML 4.01 DTDs, the WIDTH and HEIGHT attributes
are essentially unrestricted, since SGML DTDs cannot express the requirement
"must be an unsigned integer or an unsigned integer followed by the '%'
sign".
Oh, so the only reason the DTD does not restrict width/height to integer
values is that pesky possibility of specifiying percentages?
Interesting! It's always amazing for me to see how little I really know
about all that stuff.

Still, though, I wonder why the W3C would not just declare those
informative schemas authoritative and incorporate them into the official
validation service, but then again, what do I know about the wondrous
ways of the W3C? :)
--
Christian Hackl
Dec 18 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.