InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

AngularJS

  • Hello world
  • ng-init
  • ng-model
  • Controller to increment value
  • Controller - basic clock
  • Controller - ajax
  • date filter
  • include template (url)
  • include template (inline)
  • tabs using ng-switch
  • conditional style using ng-style
  • animation with ng-class
  • ng-if vs ng-hide/ng-show

AngularJS Directives

  • ng-bind
  • ng-bind-html
  • ng-bind-template
  • ng-blur
  • ng-change
  • ng-checked
  • ng-class
  • ng-class-even and ng-class-odd
  • ng-click
  • ng-cloak
  • ng-dblclick
  • ng-disabled
  • ng-focus
  • ng-hide
  • ng-href
  • ng-if
  • ng-list
 
  • Home
  • > Tutorials
  • > Javascript
  • > AngularJS

AngularJS – dynamic css using ng-style

on Apr 12, 2016

Angularjs ng-style directive can be used to define inline style attribute which can define dynamic style values using variables.

Usage:

<ANY
  ng-style="expression">
...
</ANY>
// e.g. ng-style="{display: cbValue == true? 'static':'none'}"

Note that expression evals to an object whose keys are CSS style names and values are corresponding values for those CSS keys.

Example – Show/hide a div using ng-style and checkbox state

This example will use checkbox state to hide/show a div element using style display.

<div ng-app="myApp" ng-controller='MyCtrl'>
  <input type="checkbox" ng-model="cbValue">Show/Hide<br><br>
  <div ng-style="{display: cbValue == true? 'static':'none'}">
    Hello world
  </div>
</div>

<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular.min.js"></script>
<script>
var module = angular.module('myApp', []);
module.controller('MyCtrl', function($scope) {
  $scope.cbValue = true;
});
</script>
refresh done
try it online

Example – Show/hide a div using ng-style and checkbox ng-change handler

Same example with a model variable in ng-style and changing the variable in ng-change handler.

<div ng-app="myApp" ng-controller='MyCtrl'>
  <input type="checkbox" ng-model="cbValue" ng-change="styleVal= cbValue ? {display:'static'} : {display:'none'}">Show/Hide<br><br>
  <div ng-style="styleVal">
    Hello world
  </div>
</div>

<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular.min.js"></script>
<script>
var module = angular.module('myApp', []);
module.controller('MyCtrl', function($scope) {
  $scope.cbValue = true;
});
</script>
refresh done
try it online

Suggested posts:

  1. node – how to find version of installed package
  2. CSS animation shorthand property
  3. CSS general sibling selector
  4. CSS :enabled :disabled – style of enable and disabled elements
  5. AngularJS ng-focus example
  6. AngularJS ng-class-even and ng-class-odd example
  7. HTML input checkbox
  8. AngularJS simple controller tutorial to increment a value
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged AngularJS, Javascript, Tutorials
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress