简体   繁体   English

使段落文本适合div

[英]Fit paragraph text to div

JSFiddle: http://jsfiddle.net/8jB4j/2/ JSFiddle: http : //jsfiddle.net/8jB4j/2/

<div class="question" data-next-url="/questions/2">
  A fisherman&#39;s day is rated as good if he catches 9 fish, fair if he catches 7 fish and bad if he catches 5 fish. He catches 53 fish in a week and claims that he had all of good, fair and bad days in the week. So how many good, fair and bad days did the fisherman have in that week?
</div>

<div class="options">  
  <div class="option" data-correct="true">
    (4, 1, 2)
  </div>

  <div class="option" data-correct="true">
    (3, 3, 1)
  </div>
</div>

I'm making a faux slideshow using a web app, where I need a question to exactly fill about 40% (vertically) of the slide area. 我正在使用网络应用制作人造幻灯片,在那里我需要一个问题来准确填充(垂直)大约40%的幻灯片区域。 The font-size is immaterial. 字体大小无关紧要。

For this, I have a div that takes up 40% of the slide area. 为此,我有一个div占据了幻灯片区域的40%。 I wish the contents of the div (paragraph text) to grow in font-size to just about fit this div (such that a 1px increase will result in an overflow). 我希望div(段落文本)的内容以字体大小增长,以便恰好适合该div(这样,如果增加1px,将导致溢出)。

I have come across many solutions that take care of this for one line of text, but none for paragraph text. 我遇到过很多解决方案,它们只针对一行文本,而没有针对段落文本的解决方案。 What would be a good way to go about doing this? 什么是进行此操作的好方法?

This should work. 这应该工作。 Using jQuery you could grow you question until it fills the desired size. 使用jQuery,您可能会question直到它填满所需的大小为止。

First, enclose the .question inside a parent div : 首先,将.question括在父div

<div id="theParent">
<div class="question" data-next-url="/questions/2">
  A fisherman&#39;s day is rated as good if he...
</div>
</div>

Second, edit your CSS like this: 其次,像这样编辑CSS:

#theParent{
    height:40%;
    overflow: hidden;
}

.question {
  height: auto;
  padding: 2% 5% 5% 5%;
  font-size: 1em;
}

Now, use this function (remember we are using jQuery ): 现在,使用此函数(请记住我们正在使用jQuery ):

function() resizeTheQuestion{
   var base=1;
   var inc=0.1;   
   while ($(".question").outerHeight() < $("#theParent").height())
   {
       $(".question").css({"font-size":(base+inc)+"em"});    
       inc+=0.1;
   }
    inc-=0.1;
    $(".question").css({"font-size":(base+inc)+"em"});
}

See it working: http://jsfiddle.net/8jB4j/3/ 看到它正常工作: http : //jsfiddle.net/8jB4j/3/

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

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