-2

I have tried to keep the footer below some content with various methods, but it seems that the solutions don't behave as expected with AngularJS's dynamic content.

  • When the page is mostly blank, I want the footer to be at the bottom of the page

  • When the page expands (or just big in general), I want the footer to be pushed below the content (not sticky to the page itself)

    • The same logic should follow when I redirect the page

Here is a small demo that I made to illustrate my attempt. I tried using position: absolute; for my footer, but I don't know how to change that (or use alternatives) once the content expands.

Plunker

var app = angular.module('myApp', ['ui.router']);
app.config(function($stateProvider, $urlRouterProvider) {
  $stateProvider.
  state("main", {
    url: "/",
    templateUrl: "main.html",
    controller: "mainCtrl"
  }).
  state("small", {
    url: "/other/small",
    templateUrl: "other_small.html",
    controller: "smallCtrl"
  }).
  state("big", {
    url: "/other/big",
    templateUrl: "other_big.html",
    controller: "bigCtrl"
  })
  $urlRouterProvider.otherwise('/');
});

app.controller('mainCtrl', function($scope) {
  $scope.small_text = "aaa";
  $scope.big_text = new Array(100).fill("AAA");
});
app.controller('smallCtrl', function($scope) {
  $scope.small_text = "aaa";
});
app.controller('bigCtrl', function($scope) {
  $scope.big_text = new Array(100).fill("AAA");
});
body {
  height: 100%;
  margin: 0;
}

footer {
  background-color: #999999;
  color: #F1F1F1;
  text-align: center;
}

.footer {
  position: absolute;
  bottom: 0;
  height: 60px;
  width: 100%;
}

.main-view {
  background-color: #F1F1F1;
}
<!DOCTYPE html>
<html ng-app="myApp">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/1.0.20/angular-ui-router.min.js"></script>

<body>
  <div>

    <header>
      <h4>Responsive header</h4>
      <a ui-sref="main">Main page</a>
      <a ui-sref="small">Small page</a>
      <a ui-sref="big">Big page</a>
      <hr>
    </header>


    <div class="main-view" ui-view></div>

    <footer class="footer">
      <h4>
        <span>demo website</span> 2018 &copy;
      </h4>
    </footer>

  </div>
  
  <!-- Assume: separate files -->
  <script type="text/ng-template" id="main.html">
    {{::small_text}}
    <div>
      <button ng-click="show_big_text=!show_big_text">
      {{show_big_text ? 'Hide' : 'Show'}} big text
    </button>
      <div ng-show="show_big_text" ng-repeat="text in ::big_text track by $index">
        {{text}}
      </div>
    </div>
  </script>
  <script type="text/ng-template" id="other_small.html">
    {{::small_text}}
  </script>
  <script type="text/ng-template" id="other_big.html">
    <div ng-repeat="text in ::big_text track by $index">
      {{text}}
    </div>
  </script>

</body>
</html>
Aleksey Solovey
  • 4,153
  • 3
  • 15
  • 34

2 Answers2

-1

There is no need to use position: absolute. Just give a margin-top and place the footer at the end of your DOM. Then the footer will automatically start wherever your body content ends

var app = angular.module('myApp', ['ui.router']);
app.config(function($stateProvider, $urlRouterProvider) {
  $stateProvider.
  state("main", {
    url: "/",
    templateUrl: "main.html",
    controller: "mainCtrl"
  }).
  state("small", {
    url: "/other/small",
    templateUrl: "other_small.html",
    controller: "smallCtrl"
  }).
  state("big", {
    url: "/other/big",
    templateUrl: "other_big.html",
    controller: "bigCtrl"
  })
  $urlRouterProvider.otherwise('/');
});

app.controller('mainCtrl', function($scope) {
  $scope.small_text = "aaa";
  $scope.big_text = new Array(100).fill("AAA");
});
app.controller('smallCtrl', function($scope) {
  $scope.small_text = "aaa";
});
app.controller('bigCtrl', function($scope) {
  $scope.big_text = new Array(100).fill("AAA");
});
body {
  height: 100%;
  margin: 0;
}

footer {
  background-color: #999999;
  color: #F1F1F1;
  text-align: center;
}

.footer {
  margin-top: 20px;
  width: 100%;
}

.main-view {
  background-color: #F1F1F1;
}
<!DOCTYPE html>
<html ng-app="myApp">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/1.0.20/angular-ui-router.min.js"></script>

<body>
  <div>

    <header>
      <h4>Responsive header</h4>
      <a ui-sref="main">Main page</a>
      <a ui-sref="small">Small page</a>
      <a ui-sref="big">Big page</a>
      <hr>
    </header>


    <div class="main-view" ui-view></div>

    <footer class="footer">
      <h4>
        <span>demo website</span> 2018 &copy;
      </h4>
    </footer>

  </div>
  
  <!-- Assume: separate files -->
  <script type="text/ng-template" id="main.html">
    {{::small_text}}
    <div>
      <button ng-click="show_big_text=!show_big_text">
      {{show_big_text ? 'Hide' : 'Show'}} big text
    </button>
      <div ng-show="show_big_text" ng-repeat="text in ::big_text track by $index">
        {{text}}
      </div>
    </div>
  </script>
  <script type="text/ng-template" id="other_small.html">
    {{::small_text}}
  </script>
  <script type="text/ng-template" id="other_big.html">
    <div ng-repeat="text in ::big_text track by $index">
      {{text}}
    </div>
  </script>

</body>
</html>
rramakrishnaa
  • 817
  • 7
  • 19
  • you missed my first bullet point. My content is not always filling the entire page, so I want the footer to be at the bottom of the browser at those times. But when the page gets bigger, then yes, I want it where the content ends – Aleksey Solovey Sep 10 '18 at 12:42
-1

You can try something like this:

<div style="min-height: 100vh;position: relative;padding-bottom: 123px">
  ... // content
  <div style="position: absolute; bottom: 0; height: 123px">
  </div>
</div>
Petr Averyanov
  • 9,327
  • 3
  • 20
  • 38