Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing truncated text on hover using CSS ellipsis overlaps text below it

Tags:

html

css

ellipsis

I have a name tag in the sidebar which should display single line and truncate if long text follow by triple dots (lorem ipsum...) and should show full text on hover.

I am able to achieve this using css but my problem is when full text is displayed it overlaps the text below it. (Images attached)

HTML

<p class="name">
    Lorem ipsum lorem ipsum lorem ipsum
</p>

CSS

.name{
    color: #0079c1;
    height: 2em; 
    line-height: 1em; 
    font-size: 20px;
    font-weight: 400;
    text-overflow: ellipsis;
    margin-bottom: 12px;
    cursor: pointer;
    word-break: break-all;
    overflow:hidden;
    white-space: nowrap;
}

.name:hover{
    overflow: visible; 
    white-space: normal; 
}

Here is a JSFiddle

Text overlapping on hover. Expected behaviour is it should push the content below it. enter image description here

like image 607
Nakib Avatar asked Jan 17 '16 00:01

Nakib


People also ask

How do you overflow text on an ellipsis?

To clip at the transition between characters you can specify text-overflow as an empty string, if that is supported in your target browsers: text-overflow: ''; . This keyword value will display an ellipsis ( '…' , U+2026 HORIZONTAL ELLIPSIS ) to represent clipped text.

How do I know if a text is truncated CSS?

We can check if a piece of text is truncated with the CSS text-overflow property by checking whether the offsetWidth of the element is less than its scrollWidth .


2 Answers

You can just add height:auto to the hover state and it'll work just fine:

JS Fiddle

.name{      width:120px;      color: #0079c1;      height: 2em;       line-height: 1em;       font-size: 20px;      font-weight: 400;      text-overflow: ellipsis;      margin-bottom: 12px;      cursor: pointer;      word-break: break-all;      overflow:hidden;      white-space: nowrap;  }  .name:hover{      overflow: visible;       white-space: normal;      height:auto;  /* just added this line */  }
<p class="name">  Lorem ipsum lorem ipsum lorem ipsum ipsum lorem ipsum  </p>  <span>    Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quidem voluptate deserunt consequatur velit, alias ullam fuga aspernatur, ut doloremque eos fugiat quo a accusamus minus distinctio quasi, recusandae excepturi molestiae.  </span>
like image 77
Mi-Creativity Avatar answered Sep 28 '22 04:09

Mi-Creativity


Facing similar problem with some long email addresses in a form I created this solution where the full content is displayed on hover in a tooltip-style pseudo element.

body{    background:#eee;  }  .box{    background:#fff;    box-shadow: 0 25px 30px 0 rgba(0,0,0,0.15);    border:rgba(0,0,0,0.3);    width:10rem;    margin:2rem auto;    padding:2rem;  }  .ellipsis {      display: block;      white-space: nowrap;      overflow: hidden;      text-overflow: ellipsis;      transition: all .2s linear;      white-space: nowrap;      padding:.5rem 1rem;  }  .ellipsis:focus, .ellipsis:hover {    color:transparent;  }  .ellipsis:focus:after,.ellipsis:hover:after{      content:attr(data-text);      overflow: visible;      text-overflow: inherit;      background: #fff;      position: absolute;      left:auto;      top:auto;      width: auto;      max-width: 20rem;      border: 1px solid #eaebec;      padding: 0 .5rem;      box-shadow: 0 2px 4px 0 rgba(0,0,0,.28);      white-space: normal;      word-wrap: break-word;      display:block;      color:black;      margin-top:-1.25rem;    }
<div class="box">    <p class='ellipsis' data-text='f6cd8edc-60c2-11e7-9919-ef706e78474f'>f6cd8edc-60c2-11e7-9919-ef706e78474f</p>          <p class='ellipsis' data-text='Mauris ac eros odio. Etiam imperdiet vitae dolor eu feugiat. Pellentesque at ante dignissim, tincidunt ipsum quis, rutrum ante. Donec ac lorem nisl. Cras fringilla ex ut rutrum imperdiet. Fusce accumsan id est vel lacinia. Cras nec ligula eu velit mattis mollis. Nunc venenatis neque nibh, id dapibus orci aliquet id. Ut dictum mollis eros sed imperdiet. Phasellus quis enim nec felis sagittis varius. Ut et rutrum quam. Morbi id interdum felis. Mauris id dignissim odio.'>Mauris ac eros odio. Etiam imperdiet vitae dolor eu feugiat. Pellentesque at ante dignissim, tincidunt ipsum quis, rutrum ante. Donec ac lorem nisl. Cras fringilla ex ut rutrum imperdiet. Fusce accumsan id est vel lacinia. Cras nec ligula eu velit mattis mollis. Nunc venenatis neque nibh, id dapibus orci aliquet id. Ut dictum mollis eros sed imperdiet. Phasellus quis enim nec felis sagittis varius. Ut et rutrum quam. Morbi id interdum felis. Mauris id dignissim odio.</p>  </div>

https://codepen.io/natalitique/pen/gRdmre

Requires using data attribute with the full content.

like image 45
Natalia Avatar answered Sep 28 '22 03:09

Natalia