4

Suppose I have content of 1000 words. And I have multiple divs of 100X100 height and width and I can create new divs of the same height and width If I need. how do I decide that how much content is enough for one div and the rest of the content will be moved to the next div and so on until the content ends.

  • 1
    Thats a hard one. How many characters will fit, is based font-size as well as the used font-art. Why do you need some inflexible and hard coded stuff like this? – Tobias S Aug 22 '16 at 07:22
  • 1
    Agreed to Tobias. There should definately be a better way to achieve the result – Mike B Aug 22 '16 at 07:23
  • maybe you mean somthing like http://stackoverflow.com/a/25467363/5920627 – kay27 Aug 22 '16 at 07:24
  • @kay27 but this is crazy. remember that some words won't fix exactly in the line and will need to break (not sure if OP wants it) or just consider losing some space cause of pushing longer words at the end of line to new line. This is too much calculations for simple page imo. – Mike B Aug 22 '16 at 07:30
  • 2
    OP - explain more - why do u need such thing. what is the desired result and why? (editing question with visuals would help us help you – Mike B Aug 22 '16 at 07:32
  • Regardless of the OP's end goal, I'm curious if it's possible to do this – qxz Aug 22 '16 at 07:34
  • 2
    Hmm... you could try adding content to a `div` and check it's `offsetHeight`. That would be the actual size of the text on the page. The `div` would have to be added to the DOM and visible, though – qxz Aug 22 '16 at 07:41
  • 1
    @qxz, yes, it is surely possible even though the need is questionable, I used a technique like that discussed in my answer though the task was quite easier -- to show ellipsis at the end of a shortened string when this was not yet possible to do by CSS. – Andrew Sklyarevsky Aug 22 '16 at 08:06
  • Hi all, Thank you very much for your help and nice comments. Let me explain why i want this. Suppose you are making an application where users use Rich Text box and type some text. Then we need these text to be displayed on letter head and we need to decide how many of A4 size papers will be used. We need to show the user how all the content will fit in the page. The letter head also have a footer on last page where the user's signature image will be displayed when he electronically signs it. – Rao khurram adeel Aug 22 '16 at 08:09
  • @qxz I think it is better solution, I like it and I think it as the best possible way. Thanks – Rao khurram adeel Aug 22 '16 at 08:11
  • I noticed [a comment](http://stackoverflow.com/questions/294250/how-do-i-retrieve-an-html-elements-actual-width-and-height#comment2024253_294273) that warned about `offsetHeight` potentially being 0 right after DOM changes... thoughts? – qxz Aug 22 '16 at 08:14
  • @qxz, if you target IE8 and below then probably yes, you would need to use some clever `setTimeout`s to mitigate `offsetHeight` being `0` issues. However, all modern browsers seem to work as expected. – Andrew Sklyarevsky Aug 22 '16 at 08:35

1 Answers1

3

I agree with commenters on your question that usually there are better ways and such an idea is going to work slow and certainly not everywhere (JavaScript for layout is often a signal of problems in the page's architecture).

However, there is a solution for this.

You will need to measure how many words fit in 100x100 box and slice the content in appropriate way. For this, you may use an invisible div with width: 100px but height: auto and add content there until its offsetHeight becomes greater than 100. Since you probably use a normal font and not a monospaced one, the measurement will need to take place for every box. In the following example, we will slice lorem ipsum text to fit into as many 100x100 boxes as needed. It is not quite optimized and you may need further work on it to suite your purposes.

var loremIpsum = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.'

var boxParent = document.getElementById('parent');

while (loremIpsum.length > 0) {
    var boxContent = document.createElement('div');
    boxContent.className = 'content __measuring';
    boxParent.appendChild(boxContent);

    var indexOfSpace = loremIpsum.indexOf(' ');
    var lastIndexOfSpace = indexOfSpace;
    while (indexOfSpace != -1 && boxContent.offsetHeight < 100) {
        boxContent.innerHTML = loremIpsum.substring(0, indexOfSpace);
        lastIndexOfSpace = indexOfSpace;
        indexOfSpace = loremIpsum.indexOf(' ', indexOfSpace + 1);
    }
    if (indexOfSpace == -1) {
        boxContent.innerHTML = loremIpsum;
        loremIpsum = '';
    }
    else {
        loremIpsum = loremIpsum.substring(lastIndexOfSpace + 1);
    }

    boxContent.className = 'content';
}
#parent .content {
  display: inline-block;
  width: 100px;
  height: 100px;
  padding: 8px;
  margin: 8px;
  background: yellow;
  font-family: Arial, sans-serif;
  font-size: 16px;
  box-sizing: border-box;
  vertical-align: top;
  }

#parent .content.__measuring {
  height: auto;
  }
<div id="parent">
</div>
Andrew Sklyarevsky
  • 2,095
  • 14
  • 17
  • Thank you very much Andrew Sklyaresvsky for you solution. I find it very helpful. I think one modification in CSS will be to remove the font-family and font-size. As the text will come from Rich textbox. I think it is my mistake that i didn't mentioned it first time. – Rao khurram adeel Aug 22 '16 at 08:18