简体   繁体   English

在一定高度和宽度的div中可以容纳多少内容

[英]how much content can fit in a div of certain height and width

Suppose I have content of 1000 words. 假设我有1000个单词的内容。 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. 我有100X100高度和宽度的多个div,如果需要,我可以创建相同高度和宽度的新div。 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. 我如何确定一个div的内容足够多,其余内容将被移动到下一个div,依此类推,直到内容结束。

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). 我同意你的问题的评论者,通常有更好的方法,这样的想法将工作缓慢,当然不是无处不在(JavaScript布局通常是页面架构中的问题的信号)。

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. 您需要测量100x100框中适合的单词数量,并以适当的方式对内容进行切片。 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 . 为此,您可以使用width: 100pxheight: auto的不可见div ,并在其中添加内容,直到其offsetHeight大于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. 在下面的示例中,我们将根据需要将lorem ipsum文本切片为适合多个100x100的框。 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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM