I have a post title like this:
h2 {
width: 400px;
}
<h2>How SEO Optimization Helps Your Website to Become on First Page of Search Engine Result</h2>
I want to make it appear like this:
h2 {
width: 400px;
}
<h2>How SEO Optimization Helps Your Website to Become on First Page of...</h2>
How do I do that in JavaScript or even in JQuery?
I want to make my post title get hidden by ellipsis after the second line.
Thank you!
3
Answers
With webkit-line-clamp and webkit-box-orient like this:
Because firefox not supporting
webkit-line-clamp
(then doesn’t show ellipsis), we can do some tricks with::after
selector ( withouttext-overflow: ellipsis;
and-webkit-line-clamp: 2;
), something like this:For Firefox (also for IE or other browser):
You can use the canvas 2D rendering context to measure the text and determine how much text fits on two lines:
This could be a whole lot more efficient than it is, but you can get the general idea. This method “feels” for the correct height by adding and removing nodes and testing the height of the block.