Tutorials References Menu

AngularJS ng-style Directive


Example

Add some style with AngularJS, using an object with CSS keys and values:

<body ng-app="myApp" ng-controller="myCtrl">

<h1 ng-style="myObj">Welcome</h1>

<script>
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
    $scope.myObj = {
        "color" : "white",
        "background-color" : "coral",
        "font-size" : "60px",
        "padding" : "50px"
    }
});
</script>
</body>
Try it Yourself »

Definition and Usage

The ng-style directive specifies the style attribute for the HTML element.

The value of the ng-style attribute must be an object, or an expression returning an object.

The object consists of CSS properties and values, in key value pairs.


Syntax

<element ng-style="expression"></element>

Supported by all HTML elements.


Parameter Values

Value Description
expression An expression which returns an object where the keys are CSS properties, and the values are CSS values.