# Preventing Awkward Line Breaks in Code Snippets with CSS

Original title: Preventing line breaks in \<code> elements

> Hacker News story 49036086 · language: en · submitted 2026-07-24
> Web version: https://zeli.app/story/49036086
> About this feed: https://zeli.app/llms.txt · Agent guide: https://zeli.app/skill.md

▲ 14 · by [surprisetalk](<https://news.ycombinator.com/user?id=surprisetalk>) · 💬 20
[original](<https://alexwlchan.net/2026/non-breaking-code/>) · [HN discussion](<https://news.ycombinator.com/item?id=49036086>)

## Summary

I recently enhanced my website by preventing awkward line breaks within short code snippets. Instead of manually replacing hyphens, which could alter the code's meaning for copy-paste users, I implemented a script that automatically adds a CSS class to short elements containing spaces or hyphens. This approach ensures that browsers render these snippets on a single line, improving readability without compromising the integrity of the code.

## Key quote

> This is the sort of typographical nicety that would be handled by a professional typesetter if I was writing a printed book with a fixed layout, but that’s not how websites work.

## HN discussion (20 comments)

### [danlitt](<https://news.ycombinator.com/item?id=49036849>)

> You can't parse [X]HTML with regex. Because HTML can't be parsed by regex. Regex is not a tool that can be used to correctly parse HTML.

### [pwdisswordfishq](<https://news.ycombinator.com/item?id=49038067>)

> Why even bother with the CSS class? Just apply text-wrap: nowrap; to all <code> elements and dispense with the fragile regex parsing.

### [pimlottc](<https://news.ycombinator.com/item?id=49038309>)

> Unfortunately this makes it much harder to read on narrow-width screens (e.g. mobile), where the use must scroll horizontally

---

## Cite

- Page: https://zeli.app/story/49036086
- Date: 2026-07-24
- Source: alexwlchan.net (https://alexwlchan.net/2026/non-breaking-code/)
- Hacker News: https://news.ycombinator.com/item?id=49036086

## Other languages

- zh: https://zeli.app/zh/story/49036086 · markdown: https://zeli.app/zh/story/49036086.md
- ja: https://zeli.app/ja/story/49036086 · markdown: https://zeli.app/ja/story/49036086.md
- ko: https://zeli.app/ko/story/49036086 · markdown: https://zeli.app/ko/story/49036086.md
- de: https://zeli.app/de/story/49036086 · markdown: https://zeli.app/de/story/49036086.md
- es: https://zeli.app/es/story/49036086 · markdown: https://zeli.app/es/story/49036086.md
