我正在寻找一种方法来使等宽文本在其容器中尽可能宽,但不会溢出或破裂。我已经看过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>
当我运行该片段时,它似乎几乎占据了整个宽度:
但是当我单击该Full page
选项时,很明显它没有并且它不可靠,尤其是当我减小窗口大小时:
因此,使用单位设置字体大小vw
似乎对我没有用。
有什么想法可以正确实现吗?