简体   繁体   中英

How to make pure css floating tooltips (absolutely positioned span) dynamically resize to accommodate text

I recently had an idea for using the CSS pseudo-class :hover to display a styled tooltip when the mouse is hovered over a link.

The basic code for the link looks like this:

 .hasTooltip { position:relative; } .hasTooltip span { display:none; } .hasTooltip:hover span { display:block; background-color:black; border-radius:5px; color:white; box-shadow:1px 1px 3px gray; position:absolute; padding:5px; top:1.3em; left:0px; max-width:200px; /* I don't want the width to be too large... */ } 
 <a href="#" class="hasTooltip">This link has a tooltip!<span>This is the tooltip text!</span></a> 

The result is exactly what I want , but with one annoying problem: the span does not expand to accommodate text, and if I don't specify a width, the text is squashed.

I did some searching on Google, found a couple examples of work people had done ( this example is creepily similar to what I've gotten), but no one seems to have addressed the span width problem I'm having.

I know this answer is extremely late, but it appears the key to your issue would be to use:

white-space: nowrap;

inside of your span, and get rid of any sort of width definition. Of course the drawback to this will be that the tooltip will only be able to support a single line. If you want a multiline solution you will most likely have to use javascript.

Here is an example of of this method: http://jsbin.com/oxamez/1/edit

An added bonus is that this works all the way down to IE7. If you do not need to support IE7, I would suggest folding the span, and img styles into a :before, and :after for the .tooltip. Then you can populate the text using the data-* attribute.

I don't think there's a perfect solution to this problem with pure CSS. The first problem is that when you place the span inside the a tag the span only wants to expand as far as the width of the link. If you place the span after the the a it's possible to get close to what you're trying to do but you'll have to set the margin-top: 1.3em and then have to set a negative margin to slide the tooltip left. However, it's going to be a fixed setting so it won't sit exactly at the start of each link.

I whipped up a jQuery solution that sets left dynamically (and a nice little fade effect for good measure).

Demo: http://jsfiddle.net/wdm954/9jaZL/7/


$('.hasTooltip').hover(function() {
    var offset = $(this).offset();
    $(this).next('span').fadeIn(200).addClass('showTooltip');
    $(this).next('span').css('left', offset.left + 'px');
}, function() {
    $(this).next('span').fadeOut(200);
});

These tool tips can also be integrated into a word press theme easily. Just copy the CSS into your style. Css file and when creating your posts, just take help of the HTML code and create your own tool tips. Rest is all styling, which can be altered according to your own choice. You may also use images inside the tool tip boxes.

http://www.handycss.com/how/how-to-create-a-pure-css-tooltip/

I found this and it was working for me. It's a good solution when you have a lot of elements and jquery plugins on the same page and you can't work with

<a href="#">Text <span>Tooltip</span></a>

View pure CSS solution: JS BIN

Credit to trezy.com

Even though this question is a bit older already, I would suggest the following compromise:
Just use max-width: 200px; and min-width: 300%; or so,
whereas the min-width could result higher than the max-width.
Just figure it out.
This way you could not have entirely liquid tooltips but the width would stand in kind of a correlation with the width of the containing link element.
In terms of optical pleasantness this approach could be of value.


edit :
Well I must admit it is nonsense what I wrote. When the min-width can be higher than the max-width, there is no sense to it. So just putting the min-width in percent would achieve what I tried to suggest. Sorry for that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM