.tooltip-inner {
background-color: #00acd6 !important;
/*!important is not necessary if you place custom.css at the end of your css calls. For the purpose of this demo, it seems to be required in SO snippet*/
color: #fff;
}
.tooltip.top .tooltip-arrow {
border-top-color: #00acd6;
}
.tooltip.right .tooltip-arrow {
border-right-color: #00acd6;
}
.tooltip.bottom .tooltip-arrow {
border-bottom-color: #00acd6;
}
.tooltip.left .tooltip-arrow {
border-left-color: #00acd6;
}
// Assign custom CSS class after the tooltip template has been added to the DOM
$(document).on('inserted.bs.tooltip', function(e) {
var tooltip = $(e.target).data('bs.tooltip');
tooltip.$tip.addClass($(e.target).data('tooltip-custom-class'));
});
<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>
//define a color
$m-color-blue: #004c97;
//create a mixin with parameters and default value
@mixin m-tooltip-color-direction($name:'.mtootltip', $color:$m-color-blue) {
//reference your custom class name
#{$name} {
.tooltip-inner {
background-color: $color;
}
//create a list of possible directions
$directions: top bottom left right;
//loop through the list
@each $direction in $directions {
&.bs-tooltip-#{$direction} .arrow:before,
.bs-tooltip-auto[x-placement^="$direction"] .arrow:before {
border-#{$direction}-color: $color !important;
}
}
}
}
Set your BS4 Tooltip in your layout (adjust direction)
Finally, add a BS4 tooltip to your layout, and give it a custom class for flexibility. This BS4 resource talks about using template as an option. We'll be using that to add our custom class.
The following example adds a tooltip on an info icon badge:
当使用 BootStrap4时,工具提示会附加到 body 标记的底部。如果您的工具提示是另一个标记的子标记,则无法使用 css 来定位该工具提示。我发现唯一可行的方法是使用 insert ted.bs.tooltip 事件并向内部 div 和箭头添加一个类。然后你可以在 css 中定位这个类。
// initialize tooltips
$('[data-toggle="tooltip"]').tooltip();
// Add the classes to the toolip when it is created
$('[data-toggle="tooltip"]').on('inserted.bs.tooltip',function () {
var thisClass = $(this).attr("class");
$('.tooltip-inner').addClass(thisClass);
$('.arrow').addClass(thisClass + "-arrow");
});
/* style the tooltip box and arrow based on your class*/
.bs-tooltip-left .redTip {
background-color: red !important
}
.bs-tooltip-left .redTip-arrow::before {
border-left-color: red !important
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<p>Show a red tooltip <a href="#" class="redTip" data-toggle="tooltip" data-placement="left" title="You Did It!">Hover over me</a></p>
.tooltip-inner {
background-color: red !important; /* Set box color to red */
color: black !important; /* Set text color to black */
}
.tooltip.bs-tooltip-top .tooltip-arrow::before {
border-top-color: red; /* Set arrow color to red */
}
.tooltip.bs-tooltip-bottom .tooltip-arrow::before {
border-bottom-color: red; /* Set arrow color to red */
}
.tooltip.bs-tooltip-start .tooltip-arrow::before {
border-left-color: red; /* Set arrow color to red */
}
.tooltip.bs-tooltip-end .tooltip-arrow::before {
border-right-color: red; /* Set arrow color to red */
}