Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap UI: How to change background color of popover

I'm trying to change the background color of a Bootstrap UI popover by creating custom popover classes to override the existing ones (e.g. popover1, popover2, etc. instead of popover). I know that this works for vanilla Bootstrap popovers (here is the fiddle, but it doesn't seem to work for the Bootstrap UI popovers).

When I apply the same method to the Bootstrap UI popover, it just shows a tiny, blank popover. All I have done so far is change

<a class="btn btn-primary popover-container" id="popover" data-toggle="popover" data-placement="right" data-container="body" rel="log-popover">Log level</a>

to

<a class="btn btn-primary popover-container" popover-placement="right" popover-template="'partials/loglevel-template.html'" popover-trigger="click">Log level</a>

loglevel-template.html

<div class="popover1">
    <div class="arrow"></div>
    <div class="popover-content">
       <p>some content</p>
    </div>
</div>

When I remove the popover1 class it works, so there's no functional issues on just getting the popover to display.

I like using the Bootstrap UI popovers more because you don't have to use any of that hard-coding template tomfoolery in jQuery (in fact you don't have to write any jQuery at all). I just can't figure out how to change the background color of the Bootstrap UI popovers. Before I head down the rabbit-hole I wanted to know if anyone else has achieved this, or if there is an easy fix (perhaps Bootstrap UI popovers use a different set of classes than the vanilla popovers). If it's a matter of overriding some CSS classes, that would be the dream.

like image 229
UltraSonja Avatar asked Aug 14 '15 17:08

UltraSonja


1 Answers

This is unfortunately not documented in the UI Bootstrap documentation, and I (also unfortunately) took several hours to find this extremely simple solution, but hopefully this will save some other folks some time. You can add a popover-class attribute to the element on which you place the uib-popover directive, then style the popover accordingly. See snippet below for details:

angular.module('ui.bootstrap.demo', ['ngAnimate', 'ui.bootstrap']);
angular.module('ui.bootstrap.demo').controller('PopoverDemoCtrl', function ($scope, $sce) {
  
  $scope.dynamicPopover = {
    content: 'Hello, World!',
    templateUrl: 'myPopoverTemplate.html',
    title: 'Title'
  };

});
.trigger-popover-button {
  margin: 25% 0 0 10%;
}

.custom-dynamic-popover-class {
  color: red;
}

.custom-dynamic-popover-class > .popover-inner > .popover-title {
  background: yellow;
}

.custom-dynamic-popover-class > .popover-inner > .popover-content {
  background: blue;
}
<!doctype html>
<html ng-app="ui.bootstrap.demo">
  <head>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular-animate.js"></script>
    <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-1.3.3.js"></script>
    <script src="example.js"></script>
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
  </head>
  <body>

    <div ng-controller="PopoverDemoCtrl">
    
      <button uib-popover-template="dynamicPopover.templateUrl" 
              popover-title="{{dynamicPopover.title}}" 
              popover-class="custom-dynamic-popover-class"
              type="button" 
              class="btn btn-default trigger-popover-button">
        Popover With Template
      </button>

      <script type="text/ng-template" id="myPopoverTemplate.html">
        <div>{{dynamicPopover.content}}</div>
        <div class="form-group">
          <label>Popup Title:</label>
          <input type="text" 
                 ng-model="dynamicPopover.title" 
                 class="form-control">
        </div>
      </script>
   
    </div>
  </body>
</html>
like image 79
Bennett Adams Avatar answered Oct 19 '22 04:10

Bennett Adams