Specification for dynamically changing CSS in Bootstrap tooltips

The default tooltip template in Bootstrap is a black box with white text. However, it is possible to modify the .tooltip css class to have a white background instead:

.tooltip{position:absolute;display:none;color:#333;text-align:left;font-size:0.9em;width:250px;}
.tooltip.in{opacity:0.9;z-index:1030;height:auto;display:block}
.tooltip.top{margin-top:-10px;text-align:center}
.tooltip.top{margin-top:-10px;text-align:center}
.tooltip.right{margin-left:10px}
.tooltip.bottom{margin-top:10px;text-align:center}
.tooltip.left{margin-left:-10px;text-align:right; position: absolute}
.tooltip-inner{display:inline-block;padding:10px;color:#666;background-color:white}
.tooltip-arrow{position:absolute;width:10px;height:0;}
.tooltip.top .tooltip-arrow{bottom:-5px;left:50%;margin-left:-5px;border-top-color:white;border-width:5px 5px 0}
.tooltip.right .tooltip-arrow{top:50%;left:-5px;margin-top:-5px;border-right-color:white;border-width:5px 5px 5px 0}
.tooltip.left .tooltip-arrow{top:50%;right:-5px;margin-top:-5px;border-left-color:white;border-width:5px 0 5px 5px}
.tooltip.bottom .tooltip-arrow{top:-5px;left:50%;margin-left:-5px;border-bottom-color:white;border-width:0 5px 5px}

If I want to change the tooltip template dynamically in an Angular directive:

angular.module("app", []).directive("tooltip", function(){
    return {
        restrict: "A",
        scope: {
            title: "@",
            template: "@"
        },
        link: function(scope, element){
            $(element).tooltip({
                title: scope.title,
                placement: "right",
            });            
        }
    }
})

For example, red-tooltip or blue-tooltip can be used:

<div ng-app="app">
    <a href="#" tooltip title="Title-1" template="red-tooltip">Title-1</a>
    <a href="#" tooltip title="Title-2" template="blue-tooltip">Title-2</a>
</div>

However, applying different overridden tooltip css classes may not work as expected.

Click here for a DEMO.

Answer №1

To update your link functionality, simply edit the code below:

    link: function(scope, element, attrs){
        $(element).tooltip({
            title: scope.title,
            placement: "right",
        }).addClass(attrs.template);
    }

Answer №2

When dealing with Tooltips, it's important to remember that styling involves two distinct elements - the arrow element and the box element.

The generated markup for a Tooltip often includes two divs - tooltip-arrow and tooltip-inner, as demonstrated here. To ensure consistency in color, both of these elements need to be styled accordingly.

Prior to applying styles, you must determine whether you want a red or blue Tooltip after rendering. There are 2 methods to achieve this:

  1. Add a CSS class to the element, similar to Ivan's example below

    link: function(scope, element, attrs){
        $(element).tooltip({
            title: scope.title,
            placement: "right",
        }).addClass(attrs.template);
    }
    
  2. Utilize the template attribute from your original markup and adjust your CSS selectors accordingly.

I personally prefer using class-selectors over attribute-selectors, which is why I have defined the styles for a red Tooltip as follows.

.red-tooltip + .tooltip.right .tooltip-arrow{ 
    border-right-color: red; 
}

.red-tooltip + .tooltip .tooltip-inner{ 
    background-color: red ; 
    color: white;
}

In the above code snippet, we use an adjacent selector to target the sibling of the .red-tooltip class within a .tooltip class containing both the .tooltip-arrow and .tooltip-inner divs. This ensures uniform coloration for both elements, resulting in a red-tooltip appearance.

For further clarification, refer to this sample Fiddle with the mentioned adjustments.

Note : Customizing Arrow elements in Tooltips can be challenging. Depending on the positioning (top, right, bottom, left), separate styles for border colors may be necessary. In your case, where right is used as the Tooltip placement, I specifically set border-right-color for the tooltip-arrow class. For comprehensive support across all placements, similar styles would need to be defined for each one.

.red-tooltip + .tooltip.left .tooltip-arrow{ 
    border-left-color: red; 
}

.red-tooltip + .tooltip.bottom .tooltip-arrow{ 
    border-bottom-color: red; 
}

Hopefully, this explanation proves helpful and informative :)

Answer №3

To resolve your issue, simply update your CSS code. Visit the JSFiddle link provided and copy the following class:

.tooltip-inner {color: #f00;}

This solution should effectively address your problem.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can I fetch and reference multiple local JSON files in Vue using Axios?

I am currently utilizing vue for prototyping some HTML components. Is there a method to make Vue detect two separate JSON files? vue.js var vm = new Vue({ el: '#douglas-laing', data: { products: [], contentPanels: [] }, created() ...

Is it possible to first match a named route and then a dynamic route in Express.js?

app.get('/post/create', create_post); app.get('/post/:slug', view_post); When trying to access /post/create, I expected the view_post function not to run. However, it still matches because "create" can be considered the value for :slug ...

What steps can be taken to ensure that AngularJS does not detach a form input's value from its model if it is invalid?

While working on a form implementation in AngularJS, I encountered a baffling behavior that has left me puzzled. It seems that whenever I set ng-minlength=5 as an input attribute, AngularJS disconnects the value until it meets the length requirement. Thi ...

Utilizing JavaScript Callbacks in HTML Image Tags

Currently I am working on a small application and looking to include a YouTube section. I have come across a method for obtaining the user's YouTube icon: This is included in the head section of my code: <script type="text/javascript" src="http:/ ...

Unleashing the power of XPath and wildcards in AJAX

Can anyone explain why the variable objProperties, which contains an xpath with a wildcard, is coming up empty in this scenario? function getXMLServerObject (httpType, cmd, isAsync) { var object = new Array(); $.ajax({ type: httpType, ...

Retrieving text content within a span element using CSS

Here is the Jsfiddle that I experimented with http://jsfiddle.net/LpH8B/2/ <span>*</span> <br> <span>*</span> <br> <span>hdhdf</span> span[text='*'] { color:red; } I am looking to target ...

Using CSS Grid to Create Three Rows of Equal Height

I am facing an issue with my vertical menu that stretches 100% height. The rows inside this menu need to have equal height and be stretched to fit the container. Additionally, I noticed that adding a button seems to drop the container slightly below the pa ...

Aligning navigation text in a grid layout

I am currently working on evenly distributing my 8 navigation links across the navigation bar. Below is the HTML code for my navigation: <nav> <ul class="nav"> <li><a href="index.html">Home</a></li> < ...

Ways to keep track of Angular Watchers within a web page

Is there a reliable method to accurately count the number of watchers on a page? I have come across various articles that provide different ways to do it, but each gives me conflicting counts, making it difficult to determine the correct approach. A Stac ...

Revamping this snippet - JavaScript with NodeJs

Currently working on a basic CRUD application, encountering an issue with obtaining the auto-incrementing value for the latest account in MongoDB. To provide more context, I've included the snippet below to achieve the following: 1) Conduct validati ...

Python Selenium for Element Detection

I am a beginner when it comes to using Selenium and I am currently seeking guidance on how to locate the element that is highlighted in this image: https://i.sstatic.net/uXf4u.png Despite my attempts, the following code resulted in an error message being ...

Creating HTML code for a mobile responsive design

I am facing an issue with my clinic webpage where the images appear differently on PC and mobile devices. I am new to HTML and CSS, so I tried using max-width: 100% but it didn't work. The code was originally created by someone else and I need help fi ...

Menu manipulation: Shifting menus left and right with a click

Take a look at this link. There are 12 menu items, but only 4 are visible due to space constraint. The rest are hidden. Update: Jsfiddle cleaned up and removed my fiddle. I have provided an alternative link above. There was a missing jQuery part in my que ...

What could be causing Ember/handlebars.js to prepend 'app/' to my template file names?

Whenever I try to load my Rails ember app, I encounter a frustrating issue where Ember is unable to locate the application and index templates. Ember.TEMPLATES['application] and Ember.TEMPLATES['index'] Oddly enough, when I check Ember.TEM ...

Mock asynchronous calls in a React component using Jest

I am new to using jest/enzyme and I am facing a challenge in mocking a call to an asynchronous function that returns a Promise. This call is within a react component's componentDidMount method. The purpose of the test is to verify that componentDidMo ...

The dynamic time picker in Vuetify is failing to populate time accurately. Time is not being populated as expected

My goal is to dynamically call the time picker by iterating through an array of objects. However, I'm encountering an issue where the time picker is selecting the time but not updating the value in the objects, unlike the date picker which works perfe ...

Experiencing a problem with Datatables where all columns are being grouped together in a single row

After creating a table with one row using colspan and adding my data to the next row, I encountered an issue with the datatables library. An error message appeared in the console: Uncaught TypeError: Cannot set property '_DT_CellIndex' of unde ...

Unable to get knockout sorting to function properly

I have been working on organizing my grid and here is the code that I have so far: ko.moveInGrid = { // Creating a view model class for grid population viewModel : function(config) { this.data = config.data; this.currentPageIndex = ...

Use jQuery to compare the input values whenever they are modified

I am trying to synchronize the input values of two inputs as they are typed in. The code I have seems to work sometimes, but not consistently. Here is the code snippet: $('#google-querynav').keypress(function() { var text = $(this).val(); ...

Issue with the first-child selector

Is this supposed to work or am I losing my mind? .project.work:first-child:before { content: 'Projects'; } .project.research:first-child:before { content: 'Research'; } <div class="project work"> <p>abcdef</p> ...