javascript - 使等宽文本尽可能大而不会导致溢出或换行

标签 javascript html css font-size monospace

我正在寻找一种方法来使等宽文本在其容器中尽可能宽,但不会溢出或破裂。我已经看过 CSS3 Make Text As Big As Possible To FIll Element Without Overflowing并尝试实现建议的解决方案,但在我的情况下似乎不起作用。如果我将字体大小设置为 3vw而不是 7vw正如对链接问题的回答所建议的那样,它似乎很接近,但是当我更改线条的长度或页面的宽度时,它又关闭了。

这是我的代码:

https://jsfiddle.net/de7qbu19/ ( fiddle 中的代码是一样的)

#song-container {
    text-align: center;
    margin: 20px;
}
#song {
    color: #000;
    font: normal 3vw Courier New,monospace,Courier;
    white-space: pre;
    word-break: break-all;
    text-align: left;
    display: inline-block;
}

#song > span { /* Chords*/
    color: #007fbf;
    cursor: pointer;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
            
<div class="container-fluid">
    <div id="song-container">
        <div class="panel panel-default">
            <div class="panel-body">
                <span id="song">
[Placeholder]

<span>Em</span>         <span>G</span>
Placeholder Placeholder Placeholder Placeholder
<span>D</span>          <span>C</span>
Placeholder Placeholder Placeholder Placeholder
                </span>
            </div>
        </div>
    </div>
</div>


当我运行片段时,它似乎几乎占据了整个宽度:
small
但是当我点击 Full page选项,很明显它没有并且它不可靠,尤其是当我减小窗口大小时:
full screen small win

因此使用 vw 设置字体大小单位似乎对我不起作用。

有什么想法可以正确实现吗?

最佳答案

如果您对 javascript 解决方案很满意,这里有一个简单的解决方案。

它包括检索div的宽度和字体大小,计算单个字符的宽度,最后根据这些信息计算出新的最大字体大小。

您还必须在脚本中指定最长文本行上可以包含的最大字符数:

Placeholder Placeholder Placeholder Placeholder

In this example above, there are 47 characters on the longest line of text;



var container = document.getElementById("container"),
  cs = getComputedStyle(container),
  containerPB = parseFloat(cs.paddingLeft) + parseFloat(cs.paddingRight) + parseFloat(cs.borderLeftWidth) + parseFloat(cs.borderRightWidth), //containerPadding + containerBorderWidth
  containerWidth = container.offsetWidth - containerPB,
  containerFontSize = parseFloat(cs.fontSize),
  containerFontFamily = cs.fontFamily,
  span = document.createElement("SPAN"),
  char = document.createTextNode("a"),
  charMax = 47, //max characters per line
  alpha = 1.00; //represent font scaling based on width
span.style.fontFamily = containerFontFamily;
span.appendChild(char);
span.style.visibility = "hidden";
document.body.appendChild(span);
//GETTING THE WIDTH OF A SINGLE CHAR
var charWidth = span.getBoundingClientRect().width;
document.body.removeChild(span);
container.style.fontSize =
  (containerWidth * containerFontSize * alpha) / (charWidth * charMax) + "px";
window.addEventListener("resize", function(e) {
  //update the width of container
  containerWidth = container.offsetWidth - containerPB;
  //update the font-size
  container.style.fontSize =
    (containerWidth * containerFontSize * alpha) / (charWidth * charMax) + "px";
});
* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}

#container {
  border: 1px solid #ddd;
  color: #000;
  font-family: Courier New, monospace, Courier;
  margin: 0 auto;
  padding: 0.375rem;
  width: 90%;
}

#container>span {
  /* Chords*/
  color: #007fbf;
  cursor: pointer;
}
<pre id="container">[Placeholder]

<span>Em</span>         <span>G</span>
Placeholder Placeholder Placeholder Placeholder
<span>D</span>          <span>C</span>
Placeholder Placeholder Placeholder Placeholder</pre>

关于javascript - 使等宽文本尽可能大而不会导致溢出或换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61444143/

相关文章:

javascript - 如何添加对象数组属性?

html - 背景图像裁剪

css - Bootstrap 4 Flexbox 侧边栏在使用位置 :fixed 时变薄

css - MVC 5 捆绑配置找不到 Jquery 图像

javascript - 在点击事件上禁用侧边栏

javascript - 我如何使用 jQuery 解析 xml?

javascript - 如何替换 html textarea 中当前选定的文本?

html - 为什么我的菜单项位于实际菜单栏 (CSS) 下方?

javascript - jQuery:检查按钮是否被点击

javascript - dojo 1.7 dojox/form/Uploader 不显示文件选择器对话框