English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

AngularJS ng-style directive

AngularJS Reference Manual

AngularJS Example

Add styles using AngularJS, using CSS key=>value object format:

!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.staticfile.org/angular.js/1.4.6/angular.min.js"></script>
</head>
<body ng-app="myApp" ng-controller="myCtrl">
<h1 ng-style="myObj">Basic Tutorial Website</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>
</html>
Test and see ‹/›

Definition and Usage

ng-style The directive adds a style attribute to the HTML element.

ng-style The attribute value must be an object, and the object returned by the expression is also an object.

The object is composed of CSS properties and values, that is, key=>value pairs.

Syntax

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

All HTML elements support this attribute.

Parameter Value

ValueDescription
stringThe expression returns an object composed of CSS properties and values.

AngularJS Reference Manual