Open In App

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

Last Updated : 06 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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:

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads