在Bootstrap 4中重新设置工具提示的颜色

38

我试图重新设计/格式化Bootstrap 4中的工具提示(tooltip),但原来的方法似乎不再起作用。目前我正在这样做:

.tooltip-inner {
    background: #7abcff; 
    background: -webkit-linear-gradient(top, #7abcff 0%,#60abf8 44%,#4096ee 100%); 
    background:    -moz-linear-gradient(top, #7abcff 0%,#60abf8 44%,#4096ee 100%); 
    background:   linear-gradient(to bottom, #7abcff 0%,#60abf8 44%,#4096ee 100%);
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#7abcff', endColorstr='#4096ee',GradientType=0 ); 
    color: #fff;
    font-family: 'Roboto', Arial, sans-serif;
}
.tooltip.top .tooltip-arrow {
    border-top-color: #7abcff;
}

.tooltip-inner 的效果还不错,但是 .tooltip.top .tooltip-arrow 不行;它依旧保持为黑色。我猜想 .tooltip.top 是指底部对齐的工具提示上方的箭头。

非常感谢任何帮助。

10个回答

62

截至Bootstrap 4.0版本

提示框(tooltip)CSS重新着色由.tooltip-inner类处理,正如你所注意到的:

.tooltip-inner {
    max-width: 200px;
    padding: 3px 8px;
    color: #fff;
    text-align: center;
    background-color: #000;
    border-radius: .25rem;
}

顶部箭头的CSS样式:

.tooltip.bs-tooltip-auto[x-placement^=top] .arrow::before, .tooltip.bs-tooltip-top .arrow::before {
    margin-left: -3px;
    content: "";
    border-width: 5px 5px 0;
    border-top-color: #000;
}

向右箭头的 CSS:

.tooltip.bs-tooltip-auto[x-placement^=right] .arrow::before, .tooltip.bs-tooltip-right .arrow::before {
    margin-top: -3px;
    content: "";
    border-width: 5px 5px 5px 0;
    border-right-color: #000;
}

Css 底部箭头:

.tooltip.bs-tooltip-auto[x-placement^=bottom] .arrow::before, .tooltip.bs-tooltip-bottom .arrow::before {
    margin-left: -3px;
    content: "";
    border-width: 0 5px 5px;
    border-bottom-color: #000;
}

左箭头的CSS:

.tooltip.bs-tooltip-auto[x-placement^=left] .arrow::before, .tooltip.bs-tooltip-left .arrow::before {
    right: 0;
    margin-top: -3px;
    content: "";
    border-width: 5px 0 5px 5px;
    border-left-color: #000;
}

请添加您建议的特定代码。链接可能会更改并与问题无关。因此,最好在此处添加答案,以便答案始终对问题有意义。 - Aniruddha Das
更新了,感谢建议!由于它仍处于Beta版本,我建议查看文档以确保工具提示在较新版本中得到更新。它们有静态版本可供检查。 - madison
1
此解决方案过于复杂,请参见下面更简单的解决方案。 - iMil

25

在你的CSS文件中简单使用这段代码。

.tooltip-inner {
background-color: #00cc00;
}
.tooltip.bs-tooltip-right .arrow:before {
    border-right-color: #00cc00 !important;
}
.tooltip.bs-tooltip-left .arrow:before {
    border-left-color: #00cc00 !important;
}
.tooltip.bs-tooltip-bottom .arrow:before {
    border-bottom-color: #00cc00 !important;
}
.tooltip.bs-tooltip-top .arrow:before {
    border-top-color: #00cc00 !important;
}

这也可以工作,但对于两种方法,箭头都没有采用新的颜色。 - ReeseB

18

为每个工具提示使用自定义样式

以上的BS4解决方案都有效,但它们会全局地改变工具提示的样式。所有工具提示看起来都一样。为每个工具提示使用自定义样式,就像警告框一样,会更好。我不知道BS团队为什么没有提供这个功能。

你好,data-container

有一个非常简单的解决方案,利用BS工具提示提供的容器选项。您可以将工具提示包装到另一个元素中,并通过data-container属性将其附加到该元素中,例如:

<span class="wrapper"><a href="#" data-toggle="tooltip" data-container=".wrapper" title="Some tooltip text!">Hover over me</a></span>
现在您可以使用包装元素单独为其中的工具提示样式设置样式。例如,您想要一个“危险”样式的工具提示。这将是HTML代码:

现在您可以使用包装元素单独为其中的工具提示样式设置样式。例如,您想要一个“危险”样式的工具提示。这将是HTML代码:

<span class="tooltip-danger"><a href="#" data-toggle="tooltip" data-container=".tooltip-danger" title="Some tooltip text!">Hover over me</a></span>

这将是样式表:

.tooltip-danger .tooltip-inner {
   color: #721c24;
   background-color: #f8d7da;
   border: 1px solid #721c24;
}
.tooltip-danger .tooltip.bs-tooltip-top .arrow:before {
   border-top-color: #721c24;
}
.tooltip-danger .tooltip.bs-tooltip-right .arrow:before {
   border-right-color: #721c24;
}
.tooltip-danger .tooltip.bs-tooltip-bottom .arrow:before {
   border-bottom-color: #721c24;
}
.tooltip-danger .tooltip.bs-tooltip-left .arrow:before {
   border-left-color: #721c24;
}

那将看起来像这样:

“danger”风格的工具提示

同一页上的另一个工具提示可能是这样的:

“success”风格的工具提示

你明白了吧...

样式表

既然我已经完成了,这里是基于 BS4 警告样式的我的工具提示样式:

.tooltip-danger .tooltip-inner {
   color: #721c24;
   background-color: #f8d7da;
   border: 1px solid #721c24;
}
.tooltip-danger .tooltip.bs-tooltip-top .arrow:before {
   border-top-color: #721c24;
}
.tooltip-danger .tooltip.bs-tooltip-right .arrow:before {
   border-right-color: #721c24;
}
.tooltip-danger .tooltip.bs-tooltip-bottom .arrow:before {
   border-bottom-color: #721c24;
}
.tooltip-danger .tooltip.bs-tooltip-left .arrow:before {
   border-left-color: #721c24;
}

.tooltip-dark .tooltip-inner {
   color: #1b1e21;
   background-color: #d6d8d9;
   border: 1px solid #1b1e21;
}
.tooltip-dark .tooltip.bs-tooltip-top .arrow:before {
   border-top-color: #1b1e21;
}
.tooltip-dark .tooltip.bs-tooltip-right .arrow:before {
   border-right-color: #1b1e21;
}
.tooltip-dark .tooltip.bs-tooltip-bottom .arrow:before {
   border-bottom-color: #1b1e21;
}
.tooltip-dark .tooltip.bs-tooltip-left .arrow:before {
   border-left-color: #1b1e21;
}

.tooltip-info .tooltip-inner {
   color: #0c5460;
   background-color: #d1ecf1;
   border: 1px solid #0c5460;
}
.tooltip-info .tooltip.bs-tooltip-top .arrow:before {
   border-top-color: #0c5460;
}
.tooltip-info .tooltip.bs-tooltip-right .arrow:before {
   border-right-color: #0c5460;
}
.tooltip-info .tooltip.bs-tooltip-bottom .arrow:before {
   border-bottom-color: #0c5460;
}
.tooltip-info .tooltip.bs-tooltip-left .arrow:before {
   border-left-color: #0c5460;
}

.tooltip-light .tooltip-inner {
   color: #818182;
   background-color: #fefefe;
   border: 1px solid #818182;
}
.tooltip-light .tooltip.bs-tooltip-top .arrow:before {
   border-top-color: #818182;
}
.tooltip-light .tooltip.bs-tooltip-right .arrow:before {
   border-right-color: #818182;
}
.tooltip-light .tooltip.bs-tooltip-bottom .arrow:before {
   border-bottom-color: #818182;
}
.tooltip-light .tooltip.bs-tooltip-left .arrow:before {
   border-left-color: #818182;
}

.tooltip-primary .tooltip-inner {
   color: #004085;
   background-color: #cce5ff;
   border: 1px solid #004085;
}
.tooltip-primary .tooltip.bs-tooltip-top .arrow:before {
   border-top-color: #004085;
}
.tooltip-primary .tooltip.bs-tooltip-right .arrow:before {
   border-right-color: #004085;
}
.tooltip-primary .tooltip.bs-tooltip-bottom .arrow:before {
   border-bottom-color: #004085;
}
.tooltip-primary .tooltip.bs-tooltip-left .arrow:before {
   border-left-color: #004085;
}

.tooltip-secondary .tooltip-inner {
   color: #383d41;
   background-color: #e2e3e5;
   border: 1px solid #383d41;
}
.tooltip-secondary .tooltip.bs-tooltip-top .arrow:before {
   border-top-color: #383d41;
}
.tooltip-secondary .tooltip.bs-tooltip-right .arrow:before {
   border-right-color: #383d41;
}
.tooltip-secondary .tooltip.bs-tooltip-bottom .arrow:before {
   border-bottom-color: #383d41;
}
.tooltip-secondary .tooltip.bs-tooltip-left .arrow:before {
   border-left-color: #383d41;
}

.tooltip-success .tooltip-inner {
   color: #155724;
   background-color: #d4edda;
   border: 1px solid #155724;
}
.tooltip-success .tooltip.bs-tooltip-top .arrow:before {
   border-top-color: #155724;
}
.tooltip-success .tooltip.bs-tooltip-right .arrow:before {
   border-right-color: #155724;
}
.tooltip-success .tooltip.bs-tooltip-bottom .arrow:before {
   border-bottom-color: #155724;
}
.tooltip-success .tooltip.bs-tooltip-left .arrow:before {
   border-left-color: #155724;
}

.tooltip-warning .tooltip-inner {
   color: #856404;
   background-color: #fff3cd;
   border: 1px solid #856404;
}
.tooltip-warning .tooltip.bs-tooltip-top .arrow:before {
   border-top-color: #856404;
}
.tooltip-warning .tooltip.bs-tooltip-right .arrow:before {
   border-right-color: #856404;
}
.tooltip-warning .tooltip.bs-tooltip-bottom .arrow:before {
   border-bottom-color: #856404;
}
.tooltip-warning .tooltip.bs-tooltip-top .arrow:before {
   border-left-color: #856404;
}
希望这能帮到您。最好的问候,George。

请注意,如果您使用类指定容器,并且在同一页中有更多的该类元素,则可能会遇到问题。当我在表格中使用不同行上的不同工具提示时,就发生了这种情况,所以我最终为每个“容器”创建了一个ID,并将该ID传递给每个工具提示。现在它运行良好。祝好!附注:抱歉关于单行格式,我无法引入换行符,即使在行末加上两个空格... :/ - GreenEyed

12

如果您正在使用SASS,请查看tooltip.scss文件:

GIT

要更改颜色,只需覆盖这些变量的值:$tooltip-arrow-color$tooltip-bg


11

Bootstrap 4.0.0和Popper.js

这是左侧绿色工具提示的最小代码:

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

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

将它添加到您的CSS中即可。


最简单但有效的解决方案,谢谢。我不明白为什么过于复杂的解决方案会成为首选。请注意,您不需要使用两个冒号,.arrow:before就足够了。 - iMil

6
我正在使用Bootstrap 4.0.0-beta.2版本,这段代码对我有效。
.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。 - Luís Henriques

5

Bootstrap 43有不同的类,您需要使用:

.tooltip.tooltip-top .tooltip-arrow,
.tooltip.bs-tether-element-attached-bottom .tooltip-arrow {
  border-top-color: #7abcff;
}

这些选择器表示顶部对齐的箭头 工具提示。

2

一个超级简单的更改方法是覆盖默认变量:

$tooltip-opacity: $一些数字; $tooltip-arrow-color: $一些颜色; $tooltip-bg: $一些颜色;


2
为了透明使用
.tooltip.show {
  opacity: .9; /* .9 = 90% background-color, 1 = 100% background-color */
}

如果我们减小字体大小,那么.tooltip { font-size: 12px; }会更合适。 - Raja

1

Bootstrap v4.0.0-beta

data-toggle="tooltip" data-placement="right"

-这对我很有效。

.tooltip-inner{ color:#000; font-weight:400;  background-color:#94C120;}
.tooltip.bs-tooltip-auto[x-placement^=right] .arrow::before, .tooltip.bs-tooltip-right .arrow::before { border-right-color: #94C120;}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接