CSS Ellipsis Beginning of String

Publikováno: 18.12.2018

I was incredibly happy when CSS text-overflow: ellipsis (married with fixed width and overflow: hidden was introduced to the CSS spec and browsers; the feature allowed us to stop trying to marry JavaScript width calculation with string width calculation and truncation.  CSS ellipsis was also very friendly to accessibility. The CSS text-overflow: ellipsis feature is great but is […]

The post CSS Ellipsis Beginning of String appeared first on David Walsh Blog.

Celý článek

I was incredibly happy when CSS text-overflow: ellipsis (married with fixed width and overflow: hidden was introduced to the CSS spec and browsers; the feature allowed us to stop trying to marry JavaScript width calculation with string width calculation and truncation.  CSS ellipsis was also very friendly to accessibility.

The CSS text-overflow: ellipsis feature is great but is essentially meant to ellipsize strings only at the end; what if we want to ellipsize the beginning of a screen?  The use case is fairly reasonable: think displaying a file path — many times the directory for a set of files is the same, in which case you’d want to display the end of the string, not the beginning.

Let me show you a trick for ellipsis at the begging of the string!

The CSS

Showing an ellipsis at the front of a string is mostly the same as ellipsis at the end, only with one simple trick:

.ellipsize-left {
    /* Standard CSS ellipsis */
    white-space: nowrap;                   
    overflow: hidden;
    text-overflow: ellipsis;  
    width: 200px;
    
    /* Beginning of string */
    direction: rtl;
    text-align: left;
}

To add an ellipsis at the beginning of a string, use RTL and and text-align to clip the beginning of the string!

Playing RTL off of text-align is a genius way to get the desired effect of CSS ellipsis at the beginning of an element or string.  It would be great for the CSS spec to implement a more robust ellipsis system but, for now, I worship amazing CSS tricks like this!

The post CSS Ellipsis Beginning of String appeared first on David Walsh Blog.

Nahoru
Tento web používá k poskytování služeb a analýze návštěvnosti soubory cookie. Používáním tohoto webu s tímto souhlasíte. Další informace