CSS text-overflow ellipsis in the beginning of the the text - Left Ellipsis using CSS in a Flex box

Problem:

If you want to have css text overflow ellipsis in the left (beginning of the text), there is no straight forward solution to get it working in all browsers (especially in Safari). There are several CSS tricks that you will end up only working few browsers. It will be further difficult, if you are trying to achieve this in flex container.

Solution:

Combining few ideas found in the stack-overflow, manged to get it working in most of the browsers including safari.

".cont" is flex box and either you can set a width to it, or can inherit the width from parent container element.

  .ellipsis:after {
        content: "";
        background-color: white;
        color: transparent;
        position: relative;
        z-index: 2;
        min-width: 100%;
        display: inline-block;
        min-height: 1rem;
    }
    .ellipsis {
        direction: rtl;
        display: inline-block;
        width: calc(100% - .1em);
        white-space: nowrap;
        overflow: hidden;
        position: relative;
        z-index: 3;
    }
    .ellipsis:before {
        content:"...";
        background-color: white;
        position: absolute;
        left: 0;
        z-index: 1;
        padding-right: 2px;
    }
    
    .cont{
        max-width: 200px;
        display:flex;
        border: 1px solid black;
    }

    <div class="cont">
      <span class="ellipsis">sdf s fsdf asdasd  sd asd asd asd fsfd sd sdfsdf sdf</span>
    </div>
    <div class="cont">
      <span class="ellipsis">sdfs sdfsdf</span>
    </div>











Comments

Popular posts from this blog

Embedding PowerBI with ASP.NET Core 2 and Angular(2,4,5)

Handling Exit Event of Console Application in C#