简体   繁体   中英

Bootstrap 4 change tooltip arrow color

I want to change the tooltip arrow color to red. I've googled about for an hour and the snippets that I found did not work.

My last attempt was:

.tooltip-arrow {
  border-right-color: red;
  border-left-color: red;
  border-bottom-color: red;
  border-top-color: red;
}

The tooltip is positioned right, poiting left.

In bootstrap 4.1 you can use:

.bs-tooltip-auto[x-placement^=bottom] .arrow::before, .bs-tooltip-bottom .arrow::before {
    border-bottom-color: red !important;
}
.bs-tooltip-auto[x-placement^=top] .arrow::before, .bs-tooltip-top .arrow::before {
    border-top-color: red !important;
}
.bs-tooltip-auto[x-placement^=left] .arrow::before, .bs-tooltip-left .arrow::before {
    border-left-color: red !important;
}
.bs-tooltip-auto[x-placement^=right] .arrow::before, .bs-tooltip-right .arrow::before {
    border-right-color: red !important;
}

The selector you are looking for is .tooltip.bs-tether-element-attached-left .tooltip-inner::before :

.tooltip.bs-tether-element-attached-left .tooltip-inner::before {
    border-right-color: red;
}

If you want every tooltip arrow red - jsfiddle :

.tooltip.bs-tether-element-attached-bottom .tooltip-inner::before {
    border-top-color: red;
}

.tooltip.bs-tether-element-attached-top .tooltip-inner::before {
    border-bottom-color: red;
}

.tooltip.bs-tether-element-attached-left .tooltip-inner::before {
    border-right-color: red;
}

.tooltip.bs-tether-element-attached-right .tooltip-inner::before {
    border-left-color: red;
}

For Bootstrap 4.3.1

Add to HTML File

<button type="button" class="btn btn-secondary" data-toggle="tooltip" data-placement="top" title="Tooltip on top">
  Tooltip on top
</button>

Add to CSS File

.tooltip-inner {
    background-color: red;
    color: #444;
    width: auto;
    max-width: 150px;
    font-size: 100%;
    white-space: nowrap;
}

.bs-tooltip-auto[x-placement^=top] .arrow::before, .bs-tooltip-top .arrow::before {
    border-top-color: red;
}
.bs-tooltip-auto[x-placement^=right] .arrow::before, .bs-tooltip-right .arrow::before {
    border-right-color: red;
}
.bs-tooltip-auto[x-placement^=bottom] .arrow::before, .bs-tooltip-bottom .arrow::before {
    border-bottom-color: red;
}
.bs-tooltip-auto[x-placement^=left] .arrow::before, .bs-tooltip-left .arrow::before {
    border-left-color: red;
}

Add to Javascript file

$(document).ready(function () {
    $('.btn').tooltip();
});

I am using bootstrap 4.0.0-beta.2. and this code worked for me.

.tooltip.bs-tooltip-bottom .tooltip-inner {
    background:#444 !important;
}

.tooltip .arrow:before {
 border-bottom-color:#444 !important;
 border-top-color:#444 !important;
 }

Bootstrap 4.0.0 & Popper.js

Left arrow tooltip:

.tooltip .tooltip-inner {
  background-color: green;
}

.tooltip .arrow::before {
  border-left-color: green;
}

Just add in to your CSS.

I usually override bootstrap 4.3 tooltip styles like this, which should be straight-forward:

/*Tooltip*/
.tooltip {
  font-family: "Roboto", "Tahoma", "Helvetica", sans-serif;
}

.tooltip > .arrow::before {
  border-bottom-color: #757E94;
}

.tooltip-inner {
  background-color: #757E94;
  color: white;
  font-style: italic;
}

jQuery plugin for variable color of tooltip arrow:

(function($) {
    $.fn.colortips = function(){
        return this.each(function() {
          var tt = $(this);
          tt.on('focus mouseenter', function(){
            var bc = tt.css('background-color');
            var tc = (tt.data('color')===undefined || tt.data('color')==='') ? '' : tt.data('color');
            var pt = tt.data('placement');
            $('.bs-tooltip-bottom.show .tooltip-inner').css({
              'background-color': bc,
              'color': tc
            });
            $('<style id="colortips-style">.bs-tooltip-bottom.show .arrow::before{border-'+pt+'-color: '+bc+';}</style>')
              .appendTo('head');
          }).on('focusout mouseleave', function(){
            $('.bs-tooltip-bottom.show .tooltip-inner').css({
              'background-color': '',
              'color': ''
            });
            $('#colortips-style').remove();
          });
        });
    }
}(jQuery));

See example .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM