Oleg replies that https://stackoverflow.com/a/2326165/ is the best of them. This allows you to apply tooltip styles to dynamically created elements. However, it does not work for Bootstrap 4. It should be slightly modified:
Bootstrap 3:
$(document).on('inserted.bs.tooltip', function(e) { var tooltip = $(e.target).data('bs.tooltip'); tooltip.$tip.addClass($(e.target).data('tooltip-custom-class')); });
Example: https://www.bootply.com/UORR04ncTP
Bootstrap 4:
$(document).on('inserted.bs.tooltip', function(e) { var tooltip = $(e.target).data('bs.tooltip'); $(tooltip.tip).addClass($(e.target).data('tooltip-custom-class')); });
Example: https://jsfiddle.net/nsmcan/naq530hx
Complete Solution (Bootstrap 3)
JS
$(document).on('inserted.bs.tooltip', function(e) { var tooltip = $(e.target).data('bs.tooltip'); tooltip.$tip.addClass($(e.target).data('tooltip-custom-classes')); }); $('body').tooltip({ selector: "[title]", html: true });
HTML
<h1>Test tooltip styling</h1> <div><span title="Long-long-long tooltip doesn't fit the single line">Hover over me 1</span></div> <div><span data-tooltip-custom-classes="tooltip-left" data-container="body" title="Example (left aligned):<br>Tooltip doesn't fit the single line, and is not a sibling">Hover over me 2</span></div> <div><span class="text-danger" data-tooltip-custom-classes="tooltip-large tooltip-left tooltip-danger" title="Example (left aligned):<br>This long text we want to have in the single line">Hover over me 3</span></div>
CSS
.tooltip.tooltip-large > .tooltip-inner { max-width: 300px; } .tooltip.tooltip-left > .tooltip-inner { text-align: left; } .tooltip.top.tooltip-danger > .tooltip-arrow { border-top-color: #d9534f; } .tooltip.top-left.tooltip-danger > .tooltip-arrow { border-top-color: #d9534f; } .tooltip.top-right.tooltip-danger > .tooltip-arrow { border-top-color:#d9534f; } .tooltip.right.tooltip-danger > .tooltip-arrow { border-right-color: #d9534f; } .tooltip.left.tooltip-danger > .tooltip-arrow { border-left-color: #d9534f; } .tooltip.bottom.tooltip-danger > .tooltip-arrow { border-bottom-color: #d9534f; } .tooltip.bottom-left.tooltip-danger > .tooltip-arrow { border-bottom-color: #d9534f; } .tooltip.bottom-right.tooltip-danger > .tooltip-arrow { border-bottom-color: #d9534f; } .tooltip.tooltip-danger > .tooltip-inner { background-color: #d9534f; }
Sergey Nudnov Jun 10 '18 at 21:35 2018-06-10 21:35
source share