How to create a tooltip?

喜你入骨 提交于 2019-12-17 10:09:11

问题


I want to make a custom CSS class for tooltip which will wrap a string of length more than 25-30.Usually such a long text doesn't fit into the tootltip text area.

And is there anyway to do this using Tooltip (ui.bootstrap.tooltip) ? Like using custom CSS class to get the desired output.

This is the simple css tooltip - Plunker DEMO

Here is the code Snippet for the same :

.tooltip {
    position: relative;
    display: inline-block;
    border-bottom: 1px dotted black;
}

.tooltip .tooltiptext {
   
    max-width:100px;
    padding:15px;
    min-height:30px;
    background:#fff;
    visibility: hidden;
    border: 1px solid black;
    color: #000;
    text-align: center;
    border-radius: 6px;
    /* Position the tooltip */
    position: absolute;
    z-index: 1;
}

.tooltip:hover .tooltiptext {
    visibility: visible;
}
<body style="text-align:center;">

<p>Move the mouse over the text below:</p>

<div class="tooltip">Hover over me 1
  <span class="tooltiptext">Tooltip text</span>
</div>
<div class="tooltip">Hover over me 2
  <span class="tooltiptext">Array [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,,1,11,1,1,1,11,1,,1,1,1,1,1,1,1,1,1,1,1,1,1,1]</span>
</div>
</body>

回答1:


The CSS Solution

There is a very simple solution to the problem at hand. What I essentially added is the following CSS code

word-wrap:break-word;

to the class that surrounds your tooltip text.

Here is a working demo

Also, this is a good read

What if I am using Angular UI?

For styling a tooltip from angular ui, I added tooltip-class="tooltip" to the tooltip code.

Here is a working demo with angular

This is a modified plunkr obtained from the Angular UI documentation




回答2:


Here is complete code of custom tooltip in bootstrap

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <script>
   $(document).ready(function(){
    $("[data-toggle=tooltip]").tooltip();
});


  </script>
  <style>
  .tooltip.top .tooltip-inner {
    background-color:red;
  }
.tooltip.top .tooltip-arrow {
          border-top-color: red;
       }
.tooltip.right .tooltip-inner {
    background-color:blue;
  }
.tooltip.right .tooltip-arrow {
          border-right-color: blue;
       }
.tooltip.bottom .tooltip-inner {
    background-color:green;
    width:400px;
    height:50px;
    padding-top:10px;
  }
.tooltip.bottom .tooltip-arrow {
          border-bottom-color: green;
       }  
.tooltip.left .tooltip-inner {
    background-color:yellow;
  }
.tooltip.left .tooltip-arrow {
          border-left-color: yellow;
       }                    
  </style>
</head>
<body>

   <div class="container">
  <div class="row">
    <div class="col-sm-12">
        <h1>Colored Tooltip</h1>
    </div>
    <div class="col-sm-3">
      <h3>Top</h3>
        <a href="#" data-toggle="tooltip" title="This tooltip is on top!">Hover to see my tooltip</a>
    </div>
    <div class="col-sm-3">
      <h3>Right</h3>
        <a href="#" data-placement="right" data-toggle="tooltip" title="This tooltip is on the right!">Hover to see my tooltip</a>
    </div>
    <div class="col-sm-3">
      <h3>Bottom</h3>
        <a href="#" data-placement="bottom" data-toggle="tooltip" title="This tooltip is on the bottom!">Hover to see my tooltip</a>
    </div>
    <div class="col-sm-3">
      <h3>Left</h3>
        <a href="#" data-placement="left" data-toggle="tooltip" title="This tooltip is on the left!">Hover to see my tooltip</a>
    </div>
  </div>
</div>
</body>
</html>



回答3:


You can create a use some css tricks which will resize the tooltip according to content.

Here is some example with assumptions of width and height.

.tooltip{
    max-width:100px;
    padding:15px;
    min-height:30px;
    background:#000;/*change accordingly*/
}

Above css will create a div with fix maximum width and resizeable height which will fit you content.




回答4:


.tooltip {
    position: relative;
    display: inline-block;
    border-bottom: 1px dotted black;
}

.tooltip .tooltiptext {
   
    max-width:100px;
    padding:15px;
    min-height:30px;
    background:red;
    visibility: hidden;
    border: 1px solid black;
    color: #000;
    text-align: center;
    border-radius: 6px;
    /* Position the tooltip */
    position: absolute;
    z-index: 1;
}

.tooltip:hover .tooltiptext {
    visibility: visible;
}
<body style="text-align:center;">

<p>Move the mouse over the text below:</p>

<div class="tooltip">Hover over me 1
  <span class="tooltiptext">Tooltip text</span>
</div>
<div class="tooltip">Hover over me 2
  <span class="tooltiptext">Array [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,,1,11,1,1,1,11,1,,1,1,1,1,1,1,1,1,1,1,1,1,1,1]</span>
</div>
</body>


来源:https://stackoverflow.com/questions/36275678/how-to-create-a-tooltip

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!