0

I was looking other answers, and they are all based on something like this:

.text-limit{
    line-height:20px;
    height: 20 * n. lines;
    overflow: hidden;
}

However what i want i something like Youtube does, so if the text is longer than 2 lines, truncate it and add ... at the end.
However, i was able to do it using Node on my terminal, because every char on the terminal has the same width, but on browser it's not true, so i was wondering if there was already something that can help me doing this.

Code i was using:

const nLines = ...;
const lineWidth = ...;
const charWidth = ...;
let text = "...";
text = text.substring(0, nLines * Math.floor(lineWidth / charWidth) - 3) + "..."
console.log(text);
3
  • don't do that with javascript - its something you should do with css Commented Jul 6, 2020 at 20:19
  • @DanielA.White how are you going to add the ... with css? Commented Jul 6, 2020 at 20:21
  • stackoverflow.com/questions/17779293/… Commented Jul 6, 2020 at 20:21

1 Answer 1

1

You can add these styles.

{
    overflow: hidden;
    text-overflow: ellipsis;
    -webkit-line-clamp: 2;
}

Read about: line-clamp

Sign up to request clarification or add additional context in comments.

2 Comments

i can't make ti work, can you please give a more significant example please?
got it working with overflow: hidden;text-overflow: ellipsis;display: -webkit-box;-webkit-line-clamp: 2; -webkit-box-orient: vertical; however -webkit-box-orient is supported in a very few browser, are there any alternatives?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.