【问题标题】:Adjust font-size to the width of the div [duplicate]将字体大小调整为 div 的宽度 [重复]
【发布时间】:2015-06-30 16:00:56
【问题描述】:

我有以下html:

<div class="title">
    <span>Some title</span>
</div>

我需要跨度的font-size 相对于 div 的宽度。我用vw试了一下:

.title span {
    display: table-cell;
    text-align: center;
    vertical-align: middle;
    font-size: 5vw;
}

但这不是解决方案,因为视口的容器似乎不是&lt;div class="title"&gt;,因为当我将它设置为5vw 时,它比我的&lt;div&gt; 大得多。

那么我如何设置一个 &lt;span&gt; 相对于它的父 div 的 font-size

【问题讨论】:

  • 你不能单独使用 CSS。您可以使用 javascript,但请先查看 fittext.js
  • 与 div 相关的意思是什么?
  • 视口单位是指窗口大小

标签: jquery html css


【解决方案1】:

嗯.. 这应该能让你到达那里,对吧?

根据元素 .width 编写一个简单的 if / else 条件!

if($('div.banner').width() < 800)
{
 //do first
 $('div.banner').css("font-size", newFontSize);
}
else
{
 // do else
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-01
    • 2016-10-31
    • 2011-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多