Skip to content

Instantly share code, notes, and snippets.

@elky
Created September 1, 2016 11:55
Show Gist options
  • Select an option

  • Save elky/ce230d6fcab055104cdf1a3358037fc7 to your computer and use it in GitHub Desktop.

Select an option

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;
}
@AkiraVoid

Copy link
Copy Markdown

I always feel sad because you all wiser than me.

@Cerfio

Cerfio commented May 9, 2022

Copy link
Copy Markdown

This is an image
Thank you !

@alii

alii commented Jun 30, 2022

Copy link
Copy Markdown

image

@looskie

looskie commented Jun 30, 2022

Copy link
Copy Markdown

image

@hanandewa5

Copy link
Copy Markdown

1e7d6b32741721 5692081ca49c0

@Larissa-Fernandes

Copy link
Copy Markdown

image

@squal1

squal1 commented Jul 22, 2022

Copy link
Copy Markdown

Praise the lord.

@asosnovsky

Copy link
Copy Markdown

Thank you jezuzzz!!!

@dvlptuankiendo

Copy link
Copy Markdown

saved my days

@TauKuk

TauKuk commented Sep 28, 2022

Copy link
Copy Markdown

India

@gurkin33

gurkin33 commented Oct 5, 2022

Copy link
Copy Markdown

image

@radanovicnikola93

Copy link
Copy Markdown

luka-kancheli-mindblown-interactive-logo

THANKS !!!!

@mmrzena

mmrzena commented Oct 21, 2022

Copy link
Copy Markdown

image

@maleointeractive

Copy link
Copy Markdown

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

@insinfo

insinfo commented Mar 17, 2023

Copy link
Copy Markdown

@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

elky commented Mar 19, 2023

Copy link
Copy Markdown
Author

@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
Copy Markdown

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
Copy Markdown

image

@DenizUgur

Copy link
Copy Markdown

@Rexy-Gamaliel

Copy link
Copy Markdown

I can't thank you enough for this o7

@OwnageBanana

Copy link
Copy Markdown

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
Copy Markdown

Gigachad! THANKS!! โœจ

image

@Raihann22

Copy link
Copy Markdown

image

@narcis-ro

Copy link
Copy Markdown

Bless your whole family!

@quangnmwork

Copy link
Copy Markdown

God bless you , sir !

@alpharder

Copy link
Copy Markdown

With a grateful heart, I thank you, Divine One, for your blessings.

@JGeremiasfl

Copy link
Copy Markdown

thank you very much!!!!

@andrzey

andrzey commented Oct 3, 2024

Copy link
Copy Markdown

God bless you ๐Ÿ™

@douglas-franca

Copy link
Copy Markdown

saved my days

@lukashsasha

Copy link
Copy Markdown

695px-Master's_Blessing

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