I have a long string, without whitespace, and a I want to place it in a div, such that
- It fills the div as much as possible, without overflowing.
- It uses the same amount of characters on each line (except the last, which line may be less).
The div is a responsive div, with width 90vw and height 84vh.
The HTML is:
<div class = "content"><p><code>Some-very-large-string-without-white-space"</code></p></div>
Scaling the font so it just fits is easy with a bit of JavaScript:
function font_fiddle (selection) {
let element = document . querySelector (selection)
let fontSize = element . clientHeight
if (!element . innerHTML . length) {
return;
}
element . style . fontSize = fontSize + 'px';
while (element . scrollHeight > element . clientHeight && fontSize > 0) {
element . style . fontSize = -- fontSize + 'px';
}
}
font_fiddle (".content") is called when the document loads, and when the window resizes. This parts works well.
The CSS for the code element is:
code {
word-break: break-all;
color: lightgreen;
background: black;
font-style: initial;
font-family: 'Source Code Pro', monospace;
font-size: 95%;
}
Due to the monospace font, and the word-break: break-all, I would expect all lines (with the exception of the last) to have the same number of characters, and to have a straight right "edge". And this would be the case if the string contains just letters and digits. But my string doesn't, and this leads to some lines being a one or a few characters shorter than possible:
Changing the CSS to word-break: break-word; has some effect, but we're still left with a jagged right edge:
Is there a way to get every line contain the same amount of characters, regardless of what those characters are?
This all is with Firefox on MacOS.


You could try with overflow-wrap: anywhere;