How do you change the width and height of Twitter Bootstrap's tooltips?

后端 未结 21 1336
情歌与酒
情歌与酒 2020-12-12 13:26

I created a tooltip using Twitter Bootstrap.

The tooltip is displaying with three lines. However, I would like to display the tooltip with only one line.

How

相关标签:
21条回答
  • 2020-12-12 13:43

    Another solution; create a new class (e.g. tooltip-wide) in CSS:

    .tooltip-wide + .tooltip > .tooltip-inner {
         max-width: 100%;
    }
    

    Use this class on elements where you want wide tooltips:

    <div class="tooltip-wide" data-toggle="tooltip" title="I am a long tooltip">Long tooltip</div>
    

    Bootstrap tooltip generates markup below the element containing the tooltip, so the HTML actually looks something like this after the markup is generated:

    <div class="tooltip-wide" data-toggle="tooltip" title="I am a long tooltip">Long tooltip</div>
    <div class="tooltip" role="tooltip">
        <div class="tooltip-arrow"></div>
        <div class="tooltip-inner">I am a long tooltip</div>
    </div>
    

    The CSS uses this to access the adjacent element (+) to .tooltip-wide, and from there navigates to .tooltip-inner, before applying the max-width attribute. This will only affect elements using the .tooltip-wide class, all other tooltips will remain unaltered.

    0 讨论(0)
  • 2020-12-12 13:44

    On BS3

    .tooltip-inner {
        min-width: 150px; /* the minimum width */
    }
    
    0 讨论(0)
  • 2020-12-12 13:44

    To fix the width problem, use the following code instead.

    $('input[rel="txtTooltip"]').tooltip({
        container: 'body'
    });
    

    example: http://eureka.ykyuen.info/2014/10/08/bootstrap-3-tooltip-width/

    0 讨论(0)
  • 2020-12-12 13:44

    I had the same problem, however all popular answers - to change .tooltip-inner{width} for my task failed to do the job right. As for other (i.e. shorter) tooltips fixed width was too big. I was lazy to write separate html templates/classes for zilions of tooltips, so I just replaced all spaces between words with &nbsp; in each text line.

    0 讨论(0)
  • 2020-12-12 13:46

    I realize this is a very old question, but if I landed here, so will others. So I figured I weigh in.

    If you want the tooltip to be responsive to one line only regardless of how much content you add to it, the width has to be flexible. However, Bootstrap does initiate the tooltip to a width, so you have to at least declare what that width will be and make it flexible from that size on up. This is what I recommend:

    .tooltip-inner {
        min-width: 100px;
        max-width: 100%; 
    }
    

    The min-width declares a starting size. As opposed to the max-width, as some other would suggest, which it declares a stopping width. According to your question, you shouldn't declare a final width or your tooltip content will eventually wrap at that point. Instead, you use an infinite width or flexible width. max-width: 100%; will ensure that once the tooltip has initiated at 100px wide, it will grow and adjust to your content regardless of the amount of content you have in it.

    KEEP IN MIND Tooltips are not intended to carry a lot of content. It could look funky if you had a long string across the entire screen. And it will definitely will have an impact in your responsive views, specially smartphone (320px width).

    I would recommend two solutions to perfect this:

    1. Keep your tooltip content to a minimum so as to not exceed 320px wide. And even if you do this you must remember if you have the tooltip placed at the right of the screen and with data-placement:right, your tooltip content will not be visible in smartphones (hence why bootstrap initially designed them to be responsive to its content and allow it to wrap)
    2. If you are hellbent on using this one line tooltip concept, then cover your six by using a @media query to reset your tooltip to fit the smartphone view. Like this:

    My demo HERE demonstrates the flexibility and responsiveness on the tooltips according to content size and device display size as well

    @media (max-width: 320px) {
        .tooltip-inner {
             min-width: initial;
             width: 320px;
        }
    }
    
    0 讨论(0)
  • 2020-12-12 13:47

    Set the class to the main tooltip div and for the inner tooltip

    div.tooltip {
        width: 270px;
    }
    
    div.tooltip-inner {
        max-width: 280px;
    }
    
    0 讨论(0)
提交回复
热议问题