as9876
as9876

Reputation: 956

Limit HTML Content Based on Its Size

I have a file (basically text) with a large amount of content, and I need to display it using HTML.

The thing is, I don't need to display all of it, only part of it. The amount that needs to be displayed is unknown. What is known is the size that the content will take up on the screen. So let's say, for example, the file contains 50,000 words. I want a screen size that's, say, 8 1/2 x 11 inches (whether I use absolute units or relative to the screen resolution is not relevant). So, in the HTML, I want to display the first x number of words from the text, such that it takes up exactly one page, not more, and not less.

Is there a way to dynamically determine the size the text will take up, and then only display the amount that takes up the amount of space I want?

All variables are known ahead of time, including page size, font size, spacing--everything. The only thing that's not known is how much content can fit within a finite, predetermined amount of space.

I think this question is somewhat unique, in that most HTML questions involve changing the formatting based on the content, whereas here, I'm looking to change the content based on the formatting.

I'm open to any client-side methods incluing jQuery or something else, and/or any server side methods (preferably ASP.NET).

Thanks,

Upvotes: 1

Views: 250

Answers (1)

Christophe
Christophe

Reputation: 28124

I don't think you can calculate it, as there are so many factors involved (and it might depend on the browser).

I would suggest to split the html into paragraphs (cutting the html at an arbitrary character position is usually not a good idea as you'll end up with broken tags). Then you can add the paragraphs one at a time, and check the container's offsetHeight to see if you've gone too far.

Upvotes: 1

Related Questions