我的HTML代码如下所示:
<div class="Basic-Text-Frame _idGenPageitem-6" id="fitin">
<p class="Basic-Paragraph ParaOverride-1">
<span class="CharOverride-2" ><?php echo $userdata->display_name;?></span>
</p>
</div>
CSS:
div.Basic-Text-Frame {
border-style:solid;
}
p.Basic-Paragraph {
color:#000000;
font-family:"Minion Pro", serif;
font-size:12px;
font-style:normal;
font-variant:normal;
font-weight:normal;
line-height:1.2;
margin-bottom:0;
margin-left:0;
margin-right:0;
margin-top:0;
orphans:1;
page-break-after:auto;
page-break-before:auto;
text-align:left;
text-decoration:none;
text-indent:0;
text-transform:none;
widows:1;
}
div._idGenPageitem-6 {
height:27px;
left:0px;
overflow:hidden;
position:absolute;
top:0px;
width:227px;
z-index:0;
}
p.ParaOverride-1 {
text-align:center;
}
span.CharOverride-2 {
color:#5b9b98;
font-family:Garamond, serif;
font-size:x-large;
font-style:normal;
font-variant:small-caps;
font-weight:normal;
text-transform:none;
max-height: 29px;
}
编辑:我之前尝试了resize font-size according to div size,this suggested answer以及flowType plugin和FitText plugin,我很想做到这一点,这就是我为什么会这样做的原因。
我的问题是,较大的文本只是在新行中,并且没有相应地适合此div框。如此长的文本结果不在div中,也无法看到。当我将跨度高度设置为“绝对”值时,它总是变为“自动”
以下是我正在处理
的问题的图片如何解决这个问题?
答案 0 :(得分:2)
在页面加载时运行此函数以在必要时调整文本大小。 css中最重要的是white-space: nowrap
,它阻止文本换行到第二行
不要忘记给出测试范围&#34; d&#34;与真实文本相同的字体系列,样式和重量。
function fittext()
{
var maxW = 227, maxH = 27, maxSize = 20;
var c = document.getElementsByClassName("fitin");
var d = document.createElement("span");
d.style.fontSize = maxSize + "px";
for (var i = 0; i < c.length; i++)
{
d.innerHTML = c[i].innerHTML;
document.body.appendChild(d);
var w = d.offsetWidth;
var h = d.offsetHeight;
document.body.removeChild(d);
var x = w > maxW ? maxW / w : 1;
var y = h > maxH ? maxH / h : 1;
var r = Math.min(x, y) * maxSize;
c[i].style.fontSize = r + "px";
}
}
fittext();
&#13;
DIV
{
width: 227px;
height: 27px;
overflow: hidden;
margin-bottom: 9px;
background-color: silver;
}
P
{
margin: 0px;
color: black;
font-size: 20px;
text-align: center;
white-space: nowrap;
}
&#13;
<DIV>
<P>
<SPAN CLASS="fitin">Short Guy</SPAN>
</P>
</DIV>
<DIV>
<P>
<SPAN CLASS="fitin">Just A Random Regular Person</SPAN>
</P>
</DIV>
<DIV>
<P>
<SPAN CLASS="fitin">A Really Very Extraordinarily Long And Tall Woman</SPAN>
</P>
</DIV>
&#13;