Skip to content

Instantly share code, notes, and snippets.

@elky
Created September 1, 2016 11:55
Show Gist options
  • Star 63 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
  • Save elky/ce230d6fcab055104cdf1a3358037fc7 to your computer and use it in GitHub Desktop.
Save elky/ce230d6fcab055104cdf1a3358037fc7 to your computer and use it in GitHub Desktop.
text-overflow ellipsis with 100% width
/*
Demo: https://jsfiddle.net/elky/f6khaf2t/
<div class="element">
<div class="truncate">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco
laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in
voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat
non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
</div>
*/
.element {
display: table;
table-layout: fixed;
width: 100%;
}
.truncate {
display: table-cell;
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
}
@rraj-eGain
Copy link

@gffuma
Copy link

gffuma commented Jan 20, 2022

@justsedaunal
Copy link

god bless you

@damjanvucina
Copy link

the hero we need but do not deserve

@sabrinaveras
Copy link

you are amazing!

@IngEoGeoBuk
Copy link

you are great! thx!!

@AkiraVoid
Copy link

I always feel sad because you all wiser than me.

@Cerfio
Copy link

Cerfio commented May 9, 2022

This is an image
Thank you !

@alii
Copy link

alii commented Jun 30, 2022

image

@Looskie
Copy link

Looskie commented Jun 30, 2022

image

@hanandewa5
Copy link

1e7d6b32741721 5692081ca49c0

@Larissa-Fernandes
Copy link

image

@squal1
Copy link

squal1 commented Jul 22, 2022

Praise the lord.

@asosnovsky
Copy link

Thank you jezuzzz!!!

@dvlptuankiendo
Copy link

saved my days

@TauKuk
Copy link

TauKuk commented Sep 28, 2022

India

@gurkin33
Copy link

gurkin33 commented Oct 5, 2022

image

@radanovicnikola93
Copy link

luka-kancheli-mindblown-interactive-logo

THANKS !!!!

@mmrzena
Copy link

mmrzena commented Oct 21, 2022

image

@maleointeractive
Copy link

Waow, nice trick, it works like a charm, many thx!

@insinfo
Copy link

insinfo commented Mar 17, 2023

@elky
I just saw that it doesn't work if the parent element is a button

<button class="element">
  <span class="truncate">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt
    ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco
    laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in
    voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat
    non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
  </span>
</button>
.element {
  display: table;
  table-layout: fixed;
  width: 100%;
}

.truncate {
  display: table-cell;
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
}

@elky
Copy link
Author

elky commented Mar 19, 2023

@insinfo thanks for noticing. For button you don't even need that table-hack. Just use the following:

.element {
  width: 100%;
}

.truncate {
  display: block; /* in case you use span */
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
}

@szymonzakiewicz
Copy link

7 years ago You Sir created this Peace of Art, this Shard of Cosmic Wisdom worthy of Lovecraftian Outer Gods themselves.
And today it saved my life.
I salute You!

@askhat-arslanov
Copy link

image

@DenizUgur
Copy link

@Rexy-Gamaliel
Copy link

I can't thank you enough for this o7

@OwnageBanana
Copy link

I will be your disciple and spread the good word of how to actually get ellipsis overflow working as reasonable person expects it to work.

@kedarnhegde
Copy link

Gigachad! THANKS!! ✨

image

@Raihann22
Copy link

image

@narcis-ro
Copy link

Bless your whole family!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment