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

后端 未结 21 1338
情歌与酒
情歌与酒 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:48

    You may edit the .tooltip-inner css class in bootstrap.css. The default max-width is 200px. You may change the max-width to your desired size.

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

    I needed to adjust the width of some tooltips. But not an overall setting. So I ended up doing this:

    CSS

    .large-tooltip .tooltip-inner {
        max-width: 300px;
    }
    @media screen and (max-width: 300px) {
      .large-tooltip .tooltip-inner {
        max-width: 100%;
      }
    }
    

    JQuery

    $('[data-toggle="tooltip"]')
        .tooltip()
        .on('shown.bs.tooltip', function(e) {
            /**
             * If the item that triggered the event has class 'large-tooltip'
             * then also add it to the currently open tooltip
             */
            if ($(this).hasClass('large-tooltip')) {
                $('body').find('.tooltip[role="tooltip"].show').addClass('large-tooltip');
            }
        });
    

    Html

    <img src="/theimage.jpg" class="large-tooltip" data-toggle="tooltip" data-html="true" title="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." />
    
    0 讨论(0)
  • 2020-12-12 13:56

    Just override the default max-width to whatever fits your needs.

    .tooltip-inner {
      max-width: 350px;
    }
    

    When max-width doesn't work (option 1)

    If max-width does not work, you could use a set width instead. This is fine, but your tooltips will no longer resize to fit the text. If you don't like that, skip to option 2.

    .tooltip-inner {
      width: 350px;
    }
    

    Correctly dealing with small containers (option 2)

    Since Bootstrap appends the tooltip as a sibling of the target, it is constrained by the containing element. If the containing element is smaller than your max-width, then max-width won't work.

    So, when max-width doesn't work, you just need to change the container:

    <div class="some-small-element">
      <a data-container="body" href="#" title="Your boxed-in tooltip text">
    </div>
    

    Pro Tip: the container can be any selector, which is nice when you only want to override styles on a few tooltips.

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

    Please refer the below post. cmcculloh's answer worked for me. https://stackoverflow.com/posts/31683500/edit

    As hinted at in the documentation, the easiest way to ensure that your tooltip does not wrap at all is to use

    .tooltip-inner {
        max-width: none;
        white-space: nowrap;
    }
    

    With this, you don't have to worry about dimension values or anything like that. Main problem being if you have a super long line of text it will just go off of the screen (as you can see in the JSBin Example).

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

    BS3:

    .tooltip-inner { width:400px; max-width: 400px; }
    
    0 讨论(0)
  • 2020-12-12 14:01

    Mine where all variable lengths and a set max width would not work for me so setting my css to 100% worked like a charm.

    .tooltip-inner {
        max-width: 100%;
    }
    
    0 讨论(0)
提交回复
热议问题