Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aligning Tooltip (div) to Text (span). Possible?

I am struggling to think of a way in which I can achieve the following, I have thought of it logically and I am pretty sure I cannot do it without javascript (jQuery).

I simply would like to know whether anybody has any clever CSS tricks that will achieve what I am trying to achieve.

Onto the description :-). I am trying to vertically align a div to a span, where the div may be of different heights dependant on the content. Please see image below to get a better understanding of what I would like to do:

enter image description here

This is my starting point for the code (please forgive minification, just how I type).

HTML:

<ul>
  <li><a href="address" title="no need for this as custom tooltip will replace">Link Text here</a>
    <div class="tooltip"><span>Description of the link here</span></div>
  </li>
  <li><a href="address">Link 2 Text here</a>
    <div class="tooltip"><span>Description of the link here</span></div>
  </li>
</ul>

CSS:

ul {list-style-type:none;margin:0;padding:0 0 0 30px;overflow:visible;}
li {position:relative;border:1px solid #000;margin:5px 0;}
li a {font-size:14px;}

li .tooltip {position:relative;margin-left:100px;width:140px;padding:10px;font-size:12px;background-color:#fff;border:1px solid #999;border-radius:4px;z-index:10;}

UPDATE

Please do not post up any answers regarding the jQuery or javascript (or any css code that shows or hides the tooltip), I can write this myself. I simply want to know whether this can be achieved with CSS. The functionality of the tooltip is irrelevant here, I simply want to hear your opinions/solutions to my aligning issue :-)

like image 494
Ben Carey Avatar asked Oct 03 '12 14:10

Ben Carey


People also ask

How do I add tooltip to text in HTML?

HTML: Use a container element (like <div>) and add the "tooltip" class to it. When the user mouse over this <div>, it will show the tooltip text. The tooltip text is placed inside an inline element (like <span>) with class="tooltiptext" .

How do I show the tooltip on the right side?

Here is how to do it with position: absolute and relative . On the container, set position: relative , and display: table (shrink to fit). For the position: absolute tooltip, set top: 0 , and left: 100% (moves to the right edge of the relative container).


1 Answers

Here you go! http://jsfiddle.net/25UWs/2/

And here is an example that uses CSS3 transitions to show and hide the tooltip http://jsfiddle.net/25UWs/5/

NO javascript used. I also took the liberty to add in tooltip arrows with just css.

The key is to use these css properties (in the proper place of course)

display:table;
display: table-cell;
display: inline-block;
vertical-align: middle;

Here's the css from jsfiddle example:

ul {
    padding:0;
    margin: 20px;
    list-style-type:none;
    overflow:visible;
    display: table;
}

li {
    position:relative;
    margin:5px 0;
    display: table-cell;
    vertical-align: middle;
    width: 100%;
    float:left;
}

li a {
    color: #555;
    font-size:14px;
    display: inline-block;
    width: 100px;
    vertical-align: middle;
}

li .tooltip {
    background: #ff;
    width:140px;
    padding:10px;
    margin-left: 15px;
    font-size:12px;
    background-color:#fff;
    border:1px solid #999;
    border-radius:4px;
    display: inline-block;
    vertical-align: middle;
    position: relative;
}

/* arrow */
li .tooltip:before,
li .tooltip:after {
    position: absolute;
    top: 50%;
    left: -8px;
    margin-top: -5px;
    content: '';
    width: 0; 
    height: 0; 
    border-top: 6px solid transparent;
    border-bottom: 6px solid transparent; 

    border-right: 8px solid #fff; 
}

li .tooltip:before {
    border-right-color: #999;
    left: -9px;
}

like image 183
Kevin Jantzer Avatar answered Sep 27 '22 03:09

Kevin Jantzer