Post

πŸ’‘ CSS Tip! Creating ASCII art is cool, but making it responsive is better! 🀩 css-tip.com/ascii-art/ Now it's possible using the new text-fit property. A simple code and you are done.

21 reposts134 likes

The art you are creating may depend on the font. The technique I am using doesn't depend on the font. It's a uniform scaling of "everything".

That makes sense. So the visual output changes depending on the font, but the scaling method itself is font-agnostic. Keeps the technique flexible without locking into monospace.

I usually have more need for responsive block-axis text fit of single-line text. My go to for that is an inline svg with a script to size the svg's viewBox to the text element within it. const bbox = text.getBBox(); svg.setAttribute('viewBox', `${bbox.x} ${bbox.y} ${bbox.width} ${bbox.height}`);

Neat trick. Two getBBox gotchas I've hit: it returns zeros if the svg or an ancestor is display:none (visibility:hidden is fine), and measuring before webfonts finish loading gives you fallback-font metrics β€” worth awaiting document.fonts.ready before you set the viewBox.

Nice to know! My use cases in the past were for things like pill tags/badges that were using system-ui/sans-serif for the font, so I hadn't run into those gotchas yet. It's a great thing to make a web component for.

But seemingly according to the spec, any unit related to the text size like `ch` does not scale accordingly and in fact these indeed do not as they retain the values based on the initial font-size. I find that a shame in a way.