It would be overly complex to use only javascript to accurately calculate all the factors.
In css3, there is a property called line-clamp.
However, this property is only supported on modern browsers.
p{
margin:20px;
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
}
DEMO
http://jsfiddle.net/MM29r/
This allows you to specify the number of lines before adding the ellipsis.
If you want it to be 300px in height, you can do the following:
var p=document.getElementsByTagName('p')[0],
lineheight=parseInt(window.getComputedStyle(p).getPropertyValue("line-height"));
var lines=Math.floor(300/lineheight);
p.style['-webkit-line-clamp']=lines;
Therefore, you can achieve an element that is 300px or less in height.
DEMOS
http://jsfiddle.net/MM29r/1/
http://jsfiddle.net/MM29r/2/
REQUIRED VALUES: line-height
If you want to make the box exactly 300px in height, you can add margins or paddings to the paragraphs according to your preferences.
If you have any questions, feel free to ask.
Note
Implementing a javascript function to add ellipsis by calculating words would be too resource-intensive for a real-world website.
A more efficient approach would be to calculate each paragraph once and store the truncated result in a database or on the static website.
However, keep in mind that different browsers may display fonts differently.
EDIT
Another method to display partial content is using max-height and -webkit-column-count
.
DEMO
http://jsfiddle.net/HNF3d/10/
This method has slightly better support than line-clamp
and allows you to display the entire content.
EDIT2
Adding a fading image at the bottom.
p{
width:300px;
max-height:300px;
overflow:hidden;
}
p:before{
content:"";
display:block;
position:absolute;
margin-top:240px;
background:-webkit-linear-gradient(top,rgba(255,255,255,0) 0%,rgba(255,255,255,1) 80%);
height:60px;
width:300px;
}
http://jsfiddle.net/MM29r/9/
EDIT3
Implementing a fading image for older browsers (use real image links, not base64).
http://jsfiddle.net/MM29r/13/