1

I have a text that I have truncated using the css - height and overflow property.

.container {
  width: 600px;
  border: 1px solid #888;
  padding: 0.5em;
  line-height: 1.2em;
  margin-bottom: 10px;
}

.summary {
  height: 47px;
  /* adjust based on line-height * rows desired */
  overflow: hidden;
}

.ellipsis {
  height: 0;
}

.ellipsis span {
  background: white;
  padding-left: 0.5em;
  position: relative;
  top: -1.2em;
  left: 3.2em;
  color: blue;
}
<div class="container">
  <div class="summary">
    <p>
      Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
    </p>
    <p>
      Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
    </p>
    <ul>
      <li>test</li>
      <li>test333</li>
    </ul>
    <strong>testststststs</strong>
  </div>
  <div class="ellipsis"><span>... Read more</span></div>
</div>

however my issue is when I try placing the "...read more" using he position relative property it does not work: ex below: http://jsfiddle.net/ctges45w/3/

As you can see the above fiddle the text is floating in middle of the sentence. http://jsfiddle.net/ctges45w/5/

how can I make sure that the "read more" text always get placed at the end of the line where its truncated regardless of the width of the content?- something like this: http://jsfiddle.net/ctges45w/4/

I have tried looking up at sources on stack overflow with similar issue and those havent or partially worked for me: ex here:how to place " ...view more " text next to the truncated sentence in a container-Javscript

any ideas?

user1234
  • 3,000
  • 4
  • 50
  • 102
  • [Many, many examples](https://www.google.com/search?q=css+ellipsis+read+more+site:stackoverflow.com) – mplungjan Jan 06 '21 at 18:43
  • @mplungjan: these examples talk about multi line but does not fix my multi element issue. I have multiple elements in a div and I need ellipsis on the last element. those dont work for me – user1234 Jan 06 '21 at 20:55

1 Answers1

2

Forget all the relative positioning. Just simply put the readmore at the end of the text and style it as you wish.

let summary = document.querySelector('.summary')

if (summary.textContent.length > 100){
    let truncated = summary.textContent.substring(0, 100)
    
    summary.innerHTML = `<p style="margin:0;">${truncated}<span class='ellipsis'>... Read more</span></p>`
}
.container {
  width: 600px;
  border: 1px solid #888;
  padding: 0.5em;
  line-height: 1.2em;
  margin-bottom: 10px;
}

.summary{
  overflow: hidden;
  height: 47px;
  padding: 0;
}

.ellipsis{
  color: blue;
}
<div class="container">
  <div class="summary">
    <p>
      Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
    </p>
    <p>
      Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
    </p>
    <ul>
      <li>test</li>
      <li>test333</li>
    </ul>
    <p>
      <strong>testststststs</strong>
      <span class="ellipsis">... Read more</span>
    </p>

  </div>
</div>
koder613
  • 1,486
  • 5
  • 21
  • I could have but the text is dynamically generated and not static- I have just shown this static DOM structure for fiddle purpose. This div container is empty in the beginning and on user inputs the div starts populating the text- I dont have control where to put the Read me text – user1234 Jan 06 '21 at 19:07
  • Are you able to insert the readme within the `summary` div? – koder613 Jan 06 '21 at 19:13
  • I can append it as another child to the summary div-like: `

    testststststs

    ... Read more` but not like `

    testststststs ... Read more

    ` as there may not be `

    ` in the content. will this work too if i append it as child to div?

    – user1234 Jan 06 '21 at 19:19
  • The former works, see my edit – koder613 Jan 06 '21 at 19:24
  • I tried https://jsfiddle.net/o7vax1hs/, but I dont see the "read more" text even when there is long text- im missing something? – user1234 Jan 06 '21 at 19:47
  • You need to scroll down – koder613 Jan 06 '21 at 19:49
  • hmm thats the problem- I cannot have a scroll- I need to truncate the content and on click of read more it will open up a new container. Scroll cannot be an option, hence I had used the overflow: hidden property. I think i'll try to refer the example posted in the comment above and try them out. thanks for your help! – user1234 Jan 06 '21 at 20:28
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/226949/discussion-between-koder613-and-user1234). – koder613 Jan 06 '21 at 20:57