【问题标题】:Re-color Tooltip in Bootstrap 4在 Bootstrap 4 中重新着色工具提示
【发布时间】:2020-10-07 20:27:27
【问题描述】:

我正在尝试在 Bootstrap 4 中重新皮肤/重新格式化工具提示,而原来的做法似乎不再有效。目前我正在这样做:

.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 是底部对齐工具提示顶部的箭头。

任何帮助将不胜感激

【问题讨论】:

    标签: css tooltip twitter-bootstrap-4 bootstrap-4


    【解决方案1】:

    截至Bootstrap 4.0

    CSS 用于 tooltip 重新着色由 .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;
    }
    

    【讨论】:

    • 请添加您建议的具体代码。该链接可能会更改并且与问题无关。因此建议在此处添加答案,以便答案始终对问题有意义
    • 已更新,感谢您的建议!由于它仍处于测试阶段,我会注意检查文档以确保工具提示在较新版本中更新。他们有你可以检查的静态版本。
    • 这个解决方案过于复杂,请看下面更简单的解决方案。
    【解决方案2】:

    在您的 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;
    }
    

    【讨论】:

      【解决方案3】:

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

      上述 BS4 解决方案有效,但它们都在全局范围内更改了工具提示样式。所有工具提示看起来都一样。为每个工具提示使用自定义样式会更甜蜜,例如就像警报框一样。我不知道为什么 BS 团队没有为现成的工具提示提供该功能。

      你好,数据容器

      使用 BS 工具提示提供的容器选项有一个相当简单的解决方案。使用此选项,您可以将工具提示元素附加到另一个特定元素。因此,您可以将工具提示包装到另一个元素中,并通过 data-container 属性将其附加到它,如下所示:

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

      现在您可以使用 wrapper 元素为其中的工具提示单独设置样式。例如,您想要一个“危险”风格的工具提示。这将是 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;
      }
      

      看起来像这样:

      同一页面上的另一个工具提示可能如下所示:

      你懂的……

      样式表

      由于我已经经历过,这里是我基于 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;
      }
      

      希望对您有所帮助。最好的问候,

      乔治

      【讨论】:

      • 请注意,如果您使用类指定容器并且在同一页面中有更多该类的元素,您可能会遇到问题。当我在每行都有不同的 tootlp 的表中使用它时,这发生在我身上,所以我最终为每个“容器”创建了一个 id,并将该 id 传递给每个工具提示。现在它工作正常。干杯! PD:对于单行格式很抱歉,我无法让它引入换行符,即使行尾有两个空格...:/
      【解决方案4】:

      如果您使用的是SASS,请检查tooltip.scss 文件:

      GIT

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

      【讨论】:

        【解决方案5】:

        Bootstrap 4.0.0 和 Popper.js

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

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

        只需将其添加到您的 CSS 中。

        【讨论】:

        • 最简单但有效的解决方案谢谢,我不明白为什么会选择过于复杂的解决方案。请注意,您不需要 2 个冒号,.arrow:before 就足够了。
        【解决方案6】:

        我正在使用引导程序 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;
          }
        

        【讨论】:

        • 我尝试了很多答案和不同的方法。这是唯一对我有用的。谢谢你。我正在使用 Bootsrap 4.0.0。
        【解决方案7】:

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

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

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

        【讨论】:

          【解决方案8】:

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

          $tooltip-opacity: $some_number; $tooltip-arrow-color: $some_color; $tooltip-bg: $some_color;

          【讨论】:

            【解决方案9】:

            透明使用

            .tooltip.show {
              opacity: .9; /* .9 = 90% background-color, 1 = 100% background-color */
            }
            

            【讨论】:

            • 如果我们减小字体大小会更合适 .tooltip { font-size: 12px; }
            【解决方案10】:

            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;}
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2020-12-03
              • 2021-07-20
              • 2016-04-30
              • 2017-10-01
              • 1970-01-01
              • 2019-07-04
              • 1970-01-01
              相关资源
              最近更新 更多