How to style the host element of the component in AngularJS?

To style the host element of the component, use ng-style directive. CSS properties help in the styling of the component.

Approach: Styling of host components uses CSS properties, which can be altered, modified and updated whenever needed.

The CSS properties are listed as follows:



  • color: Used to set font color.
  • background-color: Used to set background color.
  • font-size: Used to set size of font.
  • padding: Used to set space around the text.

Syntax:

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

Example 1: This example uses ng-style directive. CSS properties of font color and background color are used here.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <h1><center>Geeks for Geeks</center></h1></head>
  
<script src=
  </script>
  
<body ng-app="gfg" ng-controller="gfgctrl">
  
    <h1 ng-style="ele">GFG</h1>
    <h2 ng-style="ele">Portal</h2>
  
    <script>
        var app = angular.module("gfg", []);
        app.controller("gfgctrl", function($scope) {
            $scope.ele = {
                "color": "white",
                "background-color": "green",
  
            }
        });
    </script>
  
</body>
  
</html>

chevron_right


Output:

Example 2: This example shows the styling of components based on font-size, font-color and background color.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <h1><center>Geeks for Geeks</center></h1></head>
  
<script src=
  </script>
  
<body ng-app="gfg" ng-controller="gfgctrl">
  
    <h1 ng-style="ele">GFG</h1>
    <h2 ng-style="ele1">Portal</h2>
  
    <script>
        var app = angular.module("gfg", []);
        app.controller("gfgctrl", function($scope) {
            $scope.ele = {
                "color": "white",
                "background-color": "green",
                "font-size": "120px",
            }
            $scope.ele1 = {
                "color": "white",
                "background-color": "green",
                "font-size": "20px",
            }
        });
    </script>
  
</body>
  
</html>

chevron_right


Output:




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.