Skip to content
Browse files
Only set height/width if it's a non-negative number (don't set it to 0).
  • Loading branch information
jeresig committed Sep 9, 2010
1 parent cb3a9c1 commit 8b7015987cbd24c79f328bcf9260a6596a785bf5
Showing with 5 additions and 1 deletion.
  1. +5 −1 src/css.js
@@ -123,7 +123,11 @@ jQuery.each(["height", "width"], function( i, name ) {

set: function( elem, value ) {
// ignore negative width and height values #1599
return Math.max( parseFloat(value), 0 ) + "px";
value = parseFloat(value);

if ( value >= 0 ) {
return value + "px";

3 comments on commit 8b70159


This comment has been minimized.

Copy link

@staabm staabm replied Sep 10, 2010

You also set it to 0....


This comment has been minimized.

Copy link
Member Author

@jeresig jeresig replied Sep 10, 2010

Actually, that's not the case. In the current version of jQuery we do:

    // ignore negative width and height values #1599
    if ( (name === "width" || name === "height") && parseFloat(value) < 0 ) {
        value = undefined;

Which effectively sets no value. We're matching that functionality (as of this commit).


This comment has been minimized.

Copy link

@GarrettS GarrettS replied Sep 10, 2010

The problem is not internet explorer. The problem itself is the attempt to assign a property value that is invalid or unparseable. The error is not limited to height or width (or other properties that accept length that is not negative (borderWidth, fontSize)).

This feature was discussed on comp.lang.javascript fairly recently in "Sencha Touch--Support 2 browsers in just 228K!" and also later in "isColor()?". For another example, when making transitional adjustments to style (animation), assigning "100" for fontWeight works because "100" is a valid value, but "101" is not a valid value, and so can be expected to thrown the error specified in DOM 2 Style.

Validating only two properties is limited but trying to validate every possible input value for every property would be impractical.

Please sign in to comment.