You are here: Reference > JavaScript > client-side > style handling > methods > setFloatValue (CSSPrimitiveValue)

setFloatValue method (CSSPrimitiveValue)

Browser support:
Specifies a float value for the CSSPrimitiveValue object with the specified unit type.
If the style property that belongs to the CSSPrimitiveValue object cannot accept the new value, the setFloatValue method raises an exception.
A CSSPrimitiveValue object can only be retrieved with the getPropertyCSSValue method.
Two objects exist in JavaScript that support the getPropertyCSSValue method, the style and CSSStyleDeclaration objects.
Therefore, the setFloatValue method can only be used on the style object and only in Google Chrome and Safari.


object.setFloatValue (unitType, value);
You can find the related objects in the Supported by objects section below.


Required. Integer that specifies the unit type. Predefined constants are available for the possible values of this parameter, in the scope of the CSSPrimitiveValue object. For further details, see the page for the CSSPrimitiveValue object.
One of the following values (the value of a predefined constant appears in parentheses after the constant):
The specified value is a simple number.
The value is specified in percentage.
CSS_PX (5)
The value is specified in pixels.
CSS_CM (6)
The value is specified in centimeters.
CSS_MM (7)
The value is specified in millimeters.
CSS_IN (8)
The value is specified in inches.
CSS_PT (9)
The value is specified in points.
CSS_PC (10)
The value is specified in picas.
Required. Floating-point number that specifies the value.

Return value:

This method has no return value.

Example HTML code 1:

This example illustrates the use of the setFloatValue method:
    <script type="text/javascript">
        function SetWidth () {
            var div = document.getElementById ("myDiv");
            try {
                var cssValue = ("width");
                if (cssValue) {
                    cssValue.setFloatValue (CSSPrimitiveValue.CSS_PX, 300);
                    alert ("The width of the element is changed.");
                else {
                    alert ("Cannot access the value of the width property!");
            catch (e) {
                alert ("Your browser does not support the getPropertyCSSValue method!");
    <div id="myDiv" style="width:200px; background-color:#a0e0e0;">A div element</div>
    <br /><br />
    <button onclick="SetWidth ()">Set the width of the div to '300px'!</button>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content