Skip to content
Related Articles

Related Articles

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

View Discussion
Improve Article
Save Article
  • Last Updated : 06 Sep, 2022
View Discussion
Improve Article
Save Article

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:

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

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. 

HTML




<!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. 

HTML




<!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:

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!