我知道这个问题可以很容易地使用JavaScript解决,但我只对纯CSS解决方案感兴趣。
我想找到一种方法来动态调整文本大小,以便它始终适合固定的div大小。这是示例标记:
<div style="width: 200px; height: 1em; overflow: hidden;">
<p>Some sample dynamic amount of text here</p>
</div>
我在思考,也许可以通过以em为单位指定容器的宽度,并使字体大小继承该值来实现这一点?
我知道这个问题可以很容易地使用JavaScript解决,但我只对纯CSS解决方案感兴趣。
我想找到一种方法来动态调整文本大小,以便它始终适合固定的div大小。这是示例标记:
<div style="width: 200px; height: 1em; overflow: hidden;">
<p>Some sample dynamic amount of text here</p>
</div>
我在思考,也许可以通过以em为单位指定容器的宽度,并使字体大小继承该值来实现这一点?
注意:此解决方案基于视口大小而非内容量而变化。
我刚发现可以使用VW单位实现这一点。它们是与设置视口宽度相关联的单位。虽然存在一些缺点,比如不支持旧浏览器,但这绝对是值得认真考虑的东西。此外,您仍然可以为旧浏览器提供后备方案,如下所示:
p {
font-size: 30px;
font-size: 3.5vw;
}
http://css-tricks.com/viewport-sized-typography/和https://medium.com/design-ux/66bddb327bb1
font-size:calc(100% + 2vw);
或类似操作来防止文本变得太小,并获得一些控制权。这有点像“最小字体大小”。浏览器对于calc
的支持与对于vw
的支持类似。 - Prinzhorndiv
(200px)的宽度。这如何解决该问题? - Floremin编辑:注意attr(),它与css中的calc()相关。你可能会在未来实现它。
很遗憾,目前没有纯CSS的解决方案。这是我建议你做的:为你的元素添加title属性,并使用文本溢出省略号来防止设计断裂,并让用户知道还有更多的文本。
<div style="width: 200px; height: 1em; text-overflow: ellipsis;" title="Some sample dynamic amount of text here">
Some sample dynamic amount of text here
</div>
.
.
.
或者,如果你只想根据视口缩小尺寸,CSS3 支持相对于视口的新尺寸。
body {
font-size: 3.2vw;
}
你可能会对calc
方法感兴趣:
font-size: calc(4vw + 4vh + 2vmin);
完成。调整数值直到符合您的口味。
唯一的方法可能是为不同的屏幕尺寸设置不同的宽度,但这种方法非常不准确,您应该使用js解决方案。
h1 {
font-size: 20px;
}
@media all and (max-device-width: 720px){
h1 {
font-size: 18px;
}
}
@media all and (max-device-width: 640px){
h1 {
font-size: 16px;
}
}
@media all and (max-device-width: 320px){
h1 {
font-size: 12px;
}
}
供参考,一个非CSS解决方案:
以下是一些JS代码,根据文本是否溢出容器来调整字体大小。
回顾来看,这个逻辑的一个有用的下一步应该是根据溢出的量来计算新的大小,以实现“适应可用空间的文本”。
Codepen 使用稍作修改的代码,但与下面的想法相同:
function checkTextOverflow(element) {
var container = document.getElementById(element);
// Reset font-size to 100% to begin
container.style.fontSize = "100%";
// Check if the text is wider than its container,
// if so then reduce font-size
if (container.scrollWidth > container.clientWidth) {
// TODO: Add logic to adjust the value according to overflow difference.
container.style.fontSize = "70%";
}
}
checkTextOverflow("my_container_div");
#my_container_div {
width: 200px; /* width required for text-overflow to work */
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
calc(42px + (60 - 42) * (100vw - 768px) / (1440 - 768));
使用这个公式。
对于比1440和768更大或更小的任何尺寸,你可以给它一个静态值,或者采用相同的方法。
vw方案的缺点是你无法设置比例尺,例如在1440屏幕分辨率下5vw可能最终成为60px的字体大小,你想要的理想字体大小,但当你将窗口宽度缩小到768时,它可能会变成12px,而不是你想要的最小值。通过这种方法,你可以设置上限和下限,字体将在两者之间自动调整。
clamp()
可以使最小和最大字体大小紧凑表达,易于阅读。 - Isabelle Wedinclamp
。 - Creepinfunction fitText(el){
var text = el.text();
var fsize = parseInt(el.css('font-size'));
var measured = measureText(text, fsize);
if (measured.width > el.width()){
console.log('reducing');
while(true){
fsize = parseInt(el.css('font-size'));
var m = measureText(text, fsize );
if(m.width > el.width()){
el.css('font-size', --fsize + 'px');
}
else{
break;
}
}
}
else if (measured.width < el.width()){
console.log('increasing');
while(true){
fsize = parseInt(el.css('font-size'));
var m = measureText(text, fsize);
if(m.width < el.width()-4){ // not sure why -4 is needed (often)
el.css('font-size', ++fsize + 'px');
}
else{
break;
}
}
}
}
好的,你的动态文本一定来自某个地方。在我的情况下,看起来是这样的:
<div class="large" :data-contentlength="Math.floor(item.name.length/7)">[[ item.name ]]</div>
以及我的CSS类:
.large[data-contentlength="1"]{ font-size: 1.2em; }
.large[data-contentlength="2"]{ font-size: 1.1em; }
.large[data-contentlength="3"]{ font-size: 1.0em; }
.large[data-contentlength="4"]{ font-size: 0.9em; }
.large[data-contentlength="5"]{ font-size: 0.8em; }
.large[data-contentlength="6"]{ font-size: 0.7em; }
.large[data-contentlength="7"]{ font-size: 0.6em; }
我还有针对“非大型”文本的类:
[data-length="1"]{ font-size: 1.00em; }
...
编辑: 当所有浏览器都支持attr()时,这变得更容易了: https://developer.mozilla.org/en-US/docs/Web/CSS/attr#browser_compatibility
此外,如果css可以将2个单位值(例如px和ch)相除,则可以使其更具动态性,但现在必须手动完成。
请参见此处:
https://jsfiddle.net/qns0pov2/3/
创建一个1ch的立方体,并查看它在目标单位中的大小(在fiddle中为px),计算每行字符数,并使用该值获取每个内容长度的完美字体大小。
fiddle还显示了该方法的问题:平均字符宽度小于1ch
(基于0
),但是有像M
这样的字符比较大(约为70%)。
因此,如果您希望确保字符适合空间,请将fiddle调整为:--ch-width: calc(8.109 * 1.7);
如果您更感兴趣的是平均情况:--ch-width: calc(8.109 * 0.92);
Math.floor(item.name.length)
不是。 - mloureiro我从BootStrap的某个地方得到了这个动态字体大小的calc()函数,并进行了一些调整。基于4pt系统和remhttps://www.finsweet.com/client-first/docs/sizes,适用于Webflow项目:
html {font-size: 16px;}
@import url('https://fonts.googleapis.com/css2?family=Poppins&display=swap');
body {font-family: 'Poppins', sans-serif;}
/*---SETUP BASE SIZE ---*/
html {font-size: 16px;}
/*---LINE-HEIGHTS + MARGINS---*/
[class^="display"], h1, h2, h3, h4 {
margin-top: 0;
margin-bottom: 1rem;
font-weight: 600;
}
.display-1, .display-2, .display-3, .display-4 {
line-height: 1.2;
}
h1, h2, h3, h4 {
line-height: 1.4;
}
p, ul, ol {
margin-bottom: 0.7rem;
line-height: 1.45;
}
.lead {
margin-bottom: 1rem;
line-height: 1.4;
}
/*---FONT SIZES 1279px DOWN---*/
@media (max-width: 1279px) {
.display-1 {
font-size: calc(1.625rem + 4.5vw);
}
.display-2 {
font-size: calc(1.575rem + 3.9vw);
}
.display-3 {
font-size: calc(1.525rem + 3.3vw);
}
.display-4 {
font-size: calc(1.475rem + 2.7vw);
}
/*---HEADINGS---*/
h1 {
font-size: calc(1.375rem + 1.5vw);
}
h2 {
font-size: calc(1.325rem + 0.9vw);
}
h3 {
font-size: calc(1.3rem + 0.6vw);
}
h4 {
font-size: calc(1.275rem + 0.3vw);
}
/*---PARAGRAPHS/UL/OL---*/
p, ul, ol {
font-size: calc(0.823rem + 0.3vw);
}
.lead {
font-size: calc(1.01rem + 0.3vw);
}
}
/*---FONT SIZES ABOVE 1279px---*/
@media screen and (min-width: 1280px) {
.display-1 {
font-size: 5.22rem;
}
.display-2 {
font-size: 4.7rem;
}
.display-3 {
font-size: 4.16rem;
}
.display-4 {
font-size: 3.63rem;
}
/*---HEADINGS---*/
h1 {
font-size: 2.58rem;
}
h2 {
font-size: 2.05rem;
}
h3 {
font-size: 1.78rem;
}
h4 {
font-size: 1.52rem;
}
p, ul, ol {
font-size: 1.0625rem;
}
.lead {
font-size: 1.25rem;
}
}
<section>
<div class="container">
<p style="color:#8C8C8C;"><i>Note: Resize window too see text grow/shrink in browser window <= 1279px</i></p>
<br>
<h1 class="display-1">Display 1</h1>
<h1 class="display-2">Display 2</h1>
<h1 class="display-3">Display 3</h1>
<h1 class="display-4">Display 4</h1>
<br>
<br>
<br>
<br>
<h1>h1. The quick brown fox jumps over the lazy dog</h1>
<h2>h2. The quick brown fox jumps over the lazy dog</h2>
<h3>h3. The quick brown fox jumps over the lazy dog</h3>
<h4>h4. The quick brown fox jumps over the lazy dog</h4>
<p>The earliest known appearance of the phrase was in The Boston Journal. In an article titled "Current Notes" in the February 9, 1885, edition, the phrase is mentioned as a good practice sentence for writing students: "A favorite copy set by writing teachers for their pupils is the following, because it contains every letter of the alphabet: 'A quick brown fox jumps over the lazy dog.'"[2] Dozens of other newspapers published the phrase over the next few months, all using the version of the sentence starting with "A" rather than "The"</p>
<p>The earliest known use of the phrase starting with "The" is from the 1888 book Illustrative Shorthand by Linda Bronson.[4] The modern form (starting with "The") became more common even though it is slightly longer than the original (starting with "A").</p>
<p>A 1908 edition of the Los Angeles Herald Sunday Magazine records that when the New York Herald was equipping an office with typewriters "a few years ago", staff found that the common practice sentence of "now is the time for all good men to come to the aid of the party" did not familiarize typists with the entire alphabet, and ran onto two lines in a newspaper column. They write that a staff member named Arthur F. Curtis invented the "quick brown fox" pangram to address this.</p>
<br>
<br>
<br>
<br>
<p class="lead">Lead paragraph: As the use of typewriters grew in the late 19th century.</p>
<p>The phrase began appearing in typing lesson books as a practice sentence. Early examples include How to Become Expert in Typewriting: A Complete Instructor Designed Especially for the Remington Typewriter (1890),[6] and Typewriting Instructor and Stenographer's Hand-book (1892). By the turn of the 20th century, the phrase had become widely known. In the January 10, 1903, issue of Pitman's Phonetic Journal, it is referred to as "the well known memorized typing line embracing all the letters of the alphabet".</p>
<p>Robert Baden-Powell's book Scouting for Boys (1908) uses the phrase as a practice sentence for signaling.</p>
<p>The first message sent on the Moscow–Washington hotline on August 30, 1963, was the test phrase "THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG'S BACK 1234567890".</p>
<br>
<br>
<br>
<br>
<ul class="list-unordered">
<li>During the 20th century, technicians tested typewriters and teleprinters by typing the sentence.</li>
<li>During the 20th century, technicians tested typewriters and teleprinters by typing the sentence.</li>
<li>During the 20th century, technicians tested typewriters and teleprinters by typing the sentence.</li>
<li>During the 20th century, technicians tested typewriters and teleprinters by typing the sentence.</li>
<li>During the 20th century, technicians tested typewriters and teleprinters by typing the sentence.</li>
<li>During the 20th century, technicians tested typewriters and teleprinters by typing the sentence.</li>
<li>During the 20th century, technicians tested typewriters and teleprinters by typing the sentence.</li>
</ul>
<br>
<br>
<br>
<br>
<ol class="list-ordered">
<li>During the 20th century, technicians tested typewriters and teleprinters by typing the sentence.</li>
<li>During the 20th century, technicians tested typewriters and teleprinters by typing the sentence.</li>
<li>During the 20th century, technicians tested typewriters and teleprinters by typing the sentence.</li>
<li>During the 20th century, technicians tested typewriters and teleprinters by typing the sentence.</li>
<li>During the 20th century, technicians tested typewriters and teleprinters by typing the sentence.</li>
<li>During the 20th century, technicians tested typewriters and teleprinters by typing the sentence.</li>
<li>During the 20th century, technicians tested typewriters and teleprinters by typing the sentence.</li>
</ol>
<br>
<br>
<br>
<br>
</div>
</section>
欢迎使用
em
中指定width
的事情是相反的。它是取决于font-size
的width
。@JosephSilber 这正是我所想的。 - Ana