2

<html>
<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">

<style>
.red-tooltip.tooltip-inner {
background-color:red;
}
.red-tooltip + .tooltip-inner {
background-color:red;
}
.red-tooltip > .tooltip-inner {
background-color:red;
}
.red-tooltip ~ .tooltip-inner {
background-color:red;
}
</style>

</head>

<body>
<span class="red-tooltip" data-toggle="tooltip" data-placement="top" title="Hello world">Hover me</span>

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js" integrity="sha384-wHAiFfRlMFy6i5SRaxvfOCifBUQy1xHdJ/yoi7FRNXMRBu5WHdZYu1hA6ZOblgut" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js" integrity="sha384-B0UglyR+jN6CkvvICOB2joaf5I4l3gm9GU6Hc1og6Ls7i6U/mkkaduKaBhlAXv9k" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js" integrity="sha384-B0UglyR+jN6CkvvICOB2joaf5I4l3gm9GU6Hc1og6Ls7i6U/mkkaduKaBhlAXv9k" crossorigin="anonymous"></script>

<script>
$(function () {
  $('[data-toggle="tooltip"]').tooltip()
})
</script>
</body>
</html>

I'm having trouble styling specific bootstrap tooltips. I have learned that I can universally style all the tooltips using these:

.tooltip-inner {
// Styles
}
.comment-tooltip .tooltip.bs-tooltip-auto[x-placement^=top] .arrow::before, .tooltip.bs-tooltip-top .arrow::before {
// Styles
}

Etc. for all the tooltip arrows. But how can I style a specific tooltip (i.e. one element's tooltip). For example let's say this is my HTML:

<span class="red-tooltip" data-toggle="tooltip" data-placement="top" title="Hello world">
Tooltip
</span>

I tried all the CSS combinators (shown below) but none of them seem to let me style ONLY tooltips with the class red-tooltip

.red-tooltip.tooltip-inner {
background-color:#ff0000;
}
.red-tooltip ~ .tooltip-inner {
background-color:#ff0000;
}
.red-tooltip > .tooltip-inner {
background-color:#ff0000;
}
.red-tooltip + .tooltip-inner {
background-color:#ff0000;
}

Any insight would be much appreciated!

Ryan
  • 121
  • 3
  • Questions seeking code help must include the shortest code necessary to reproduce it **in the question itself** preferably in a [**Stack Snippet**](https://blog.stackoverflow.com/2014/09/introducing-runnable-javascript-css-and-html-code-snippets/). See [**How to create a Minimal, Complete, and Verifiable example**](http://stackoverflow.com/help/mcve) – Paulie_D Jan 18 '19 at 17:30
  • I added a snippet. – Ryan Jan 18 '19 at 17:36
  • Have you tried the snippet from here? It might work. https://stackoverflow.com/questions/17642447/change-bootstrap-tooltip-color – Iskandar Reza Jan 18 '19 at 19:14

1 Answers1

0

If you want only to apply a specific style to only one tooltip when hover it, I hope this will help you.

<html>

<head>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS"
    crossorigin="anonymous">

</head>

<body>
    <span id="myTooltip" class="red-tooltip" data-toggle="tooltip" data-placement="top" title="Hello world">Hover me</span>

    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
    crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js" integrity="sha384-wHAiFfRlMFy6i5SRaxvfOCifBUQy1xHdJ/yoi7FRNXMRBu5WHdZYu1hA6ZOblgut"
    crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js" integrity="sha384-B0UglyR+jN6CkvvICOB2joaf5I4l3gm9GU6Hc1og6Ls7i6U/mkkaduKaBhlAXv9k"
    crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js" integrity="sha384-B0UglyR+jN6CkvvICOB2joaf5I4l3gm9GU6Hc1og6Ls7i6U/mkkaduKaBhlAXv9k"
    crossorigin="anonymous"></script>
    <style>
        .red {
            background-color: red !important;
        }
    </style>

    <script>
        $(function () {
            $('[data-toggle="tooltip"]').tooltip();
        });

        // for store a random Id, we will use it to remove
        // the style we append to the document on tooltip hover
        var randomId;
         // get the tooltip to apply the style
        $('#myTooltip').hover((event) => {
            if (event.type == 'mouseenter' || event.type == 'mouseover') {
                // generate random Id and append the style to head
                randomId = 'tooltip' + Math.floor(Math.random() * 100) + 1;

                // append the style to the head
                $('head').append(
                    `<style data-remove="${randomId}">
                        .tooltip-inner{ 
                            background-color: red;
                        }
                        .bs-tooltip-auto[x-placement^=bottom] .arrow::before, 
                        .bs-tooltip-bottom .arrow::before {
                            border-bottom-color: red !important;
                        }
                    </style>`
                );
            }
            else if (event.type == 'mouseout' || event.type == 'mouseleave') {
                // we remove the style so doesn't effect other tooltip,
                // we wait to the tooltip fade
                setTimeout(() => {
                    $(`[data-remove="${randomId}"]`).remove();
                }, 100)
            }
        })
    </script>
</body>

</html>