Ticket #1599 (closed bug: fixed)
Internet Explorer doesn't like negative values for CSS width
| Reported by: | kkaefer | Owned by: | |
|---|---|---|---|
| Priority: | minor | Milestone: | 1.2.2 |
| Component: | core | Version: | 1.2 |
| Keywords: | Cc: | ||
| Blocking: | Blocked by: |
Description
The follwoing code causes IE (at least IE 7) to throw a script error:
$(function() { $('div').width(-1); });
(if .size() is > 0).
Obviously, it doesn't like negative values for width.
Change History
comment:2 Changed 6 years ago by davidserduke
The CSS2.1 spec states "Negative values for 'width' are illegal."
http://www.w3.org/TR/CSS21/visudet.html#propdef-width
I suggest this bug be closed.
comment:3 Changed 6 years ago by brandon
- need changed from Patch to Test Case
- Milestone changed from 1.2.1 to 1.2.2
What is the expected behavior? Should the width then be set to 0 instead of -1 or just ignored?
I would expect the result to be 0 but a quick run in firebug shows that firefox just ignores negative numbers for width and height.
I'm leaning towards fixing this so that the results while using jQuery are consistent cross-browser.
Index: jquery/src/core.js =================================================================== --- jquery/src/core.js (revision 3826) +++ jquery/src/core.js (working copy) @@ -1058,7 +1058,8 @@ return letter.toUpperCase(); }); - if ( value != undefined ) + // ignore negative numbers for width and height + if ( value != undefined && (name.match(/^width|height$/) && parseFloat(value) > 0) ) elem[ name ] = value; return elem[ name ];
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

This is a really minor issue - you shouldn't be setting negative widths to begin with - but I'm not a fan of throwing errors, so I'll look into it at some point.