Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect text-overflow has worked and add tooltip containing full text

Tags:

html

jquery

css

I have a simple span containing an email address.

<span id="email">[email protected]</span>

In my CSS, the span is set to a fixed width with ellipsis overflow.

#email {
    display: inline-block;
    width: 100px;
    overflow: hidden;
    text-overflow: ellipsis;
}

This works fine. However, I'd like to detect that it's worked somehow and display the full text in the span's title attribute.

How can I do this? Pure CSS would be perfect, but if that's not possible then jQuery is the next best thing.

like image 966
Connell Avatar asked Oct 03 '13 09:10

Connell


1 Answers

You can't detect an overflow with CSS. But using JavaScript it's simply this:

JavaScript

var e = document.getElementById('email');

if (e.scrollWidth > e.clientWidth) {
    alert("Overflow");
}

Demo

Try before buy

like image 135
insertusernamehere Avatar answered Sep 22 '22 14:09

insertusernamehere