Edit this page

Style Binding

The style binding sets the style (CSS) attributes of the target DOM element.

The example below demonstrates how to use the style binding.

Example
<span data-bind="style: {color: priceColor, fontWeight: priceFontWeight},
             text: price"></span>

<script>
var viewModel = kendo.observable({
    price: 42,
    priceColor: function() {
        var price = this.get("price");

        if (price <= 42) {
            return "#00ff00";
        } else {
            return "#ff0000";
        }
    },
    priceFontWeight: function() {
        var price = this.get("price");

        if (price <= 42) {
            return "bold";
        } else {
            return ""; //will reset the font weight to its default value
        }
    }
});

kendo.bind($("span"), viewModel);
</script>

This example results in the output below.

Example
<span style="color: #00ff00; font-weight: bold">42</span>

Common Scenarios

Use Style Attributes Containing Dashes

If the style attribute contains a dash, such as font-weight, font-size, background-color etc) you should omit the dash and capitalize the next letter (fontWeight, fontSize, backgroundColor).

Reset Style Attributes to Their Original Value

To reset the value of a style attribute set it to empty string: "".

See Also

Other articles on the Kendo UI MVVM component and bindings:

Is this article helpful? Yes / No
Thank you for your feedback!

Give article feedback

Tell us how we can improve this article

close
Dummy