Open In App

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

In this article, we will see how to style the host element of the component in AngularJS, along with knowing the basic implementation through the examples. This task can be accomplished by implementing the ng-style directive with the element. 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:



Syntax:

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

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






<!DOCTYPE html>
<html>
  
<head>
    <script src=
    </script>
</head>
  
<body ng-app="gfg" 
      ng-controller="gfgctrl">
    <h1 style="text-align: center;
               color:green">
        GeeksforGeeks
    </h1>
    <h2 ng-style="ele">GFG</h2>
    <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>

Output: 

 

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




<!DOCTYPE html>
<html>
  
<head>
    <script src=
    </script>
</head>
  
<body ng-app="gfg" ng-controller="gfgctrl">
    <h1 style="text-align: center;
               color:green">
        GeeksforGeeks
    </h1>
    <h2 ng-style="ele">GFG</h2>
    <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>

Output:

 


Article Tags :