How to set a tooltip width dynamically?

15,464

Solution 1

You can do that with CSS, just use min-width and max-width together instead of width

Also you can simply remove width from your CSS or change it into width: auto;

Solution 2

the css for the tooltops looks like this (according to your link)

div.tooltip {   
  position: absolute;           
  text-align: center;           
  width: 60px;                    /* Width and Height are fixed */            
  height: 28px;                 
  padding: 2px;             
  font: 12px sans-serif;        
  background: lightsteelblue;   
  border: 0px;      
  border-radius: 8px;           
  pointer-events: none;         
}

Try removing the width property of the CSS. Above you can see that this is set to a fixed-width of 60 pixels.

Share:
15,464
Belphegor
Author by

Belphegor

Updated on June 13, 2022

Comments

  • Belphegor
    Belphegor almost 2 years

    I want to create a tool-tip that will have flexible size according to the text length. For example, I have the following tool-tip:

    enter image description here

    Now, for this text, the width is OK (fixed in the css). But, when I have a very smaller string:

    enter image description here

    the tool-tip looks too big. My question is: how do I make the tool-tip flexible according to the text length? Is there a way to do this in the .css maybe? I work with d3.js, so an answer from this point of view would be acceptable too.

    Thank you in advance for your answer!

    EDIT: I use this tutorial in order to accomplish my goal, my code is something like that (not exactly, but close enough). It would be best to provide an answer based on that example, since my code is too big to post here.

  • Belphegor
    Belphegor almost 10 years
    That's not exactly what I'm looking for. Tnx for the answer anyway!
  • MujtabaFR
    MujtabaFR almost 10 years
    @Belphegor I've updated the answer, take a look at it
  • Belphegor
    Belphegor almost 10 years
    What an obvious answer, lol! Can't believe I didn't try with removing the width from the css. Tnx a lot!
  • Belphegor
    Belphegor almost 10 years
    Yeah, it works like that, tnx for the answer! Anyway, @Mujtaba Fathel has answered few seconds before you, sorry, I should accept his answer. +1 for the answer, though!