How to disable line-wrapping in code blocks?

Currently using the default theme and Publish.

When I have a long line in a code-block, it gets wrapped — both locally, and on the published site.

I tried disabling the “Readable line length” setting for both Obsidian and Publish, and that allowed the code blocks to be bigger, but it still wraps lines that are longer than the code block.

PS - I searched the help docs and found nothing. I searched the forums and only found CSS hacks, which I’d prefer not to rely on.

It’s not a CSS “hack”, I believe it’s the way you’re intended to customise Obsidian for this outcome. I couldn’t comment on Publish as I don’t use it.

By coincidence I needed to solve this for myself earlier today, so for anyone else looking for the CSS snippet, it’s:

/* Don't wrap codeblocks */
pre > code, code[class*="language-"] {
  white-space: pre !important;

Thanks. This was my first snippet.

Once I got it installed and enabled properly, it started working in the Reading view, though not the Editing view. I assume there’s a reason for that, and it’s a tolerable compromise, so that’s fine.

As for Publish, it had no effect on its own — I assume CSS snippets are not automatically incorporated into the published site. However, after copying the snippet to publish.css in the vault root and publishing that file, it did successfully alter the behavior of the published site.


Follow-up question: Is there a place under ~/.config/obsidian to stick snippets so they’re available to all vaults?

I didn’t look into the details, but saw this related post about code wrap. About half way through post people seem to be coming up with a css scrollbar idea, but at the bottom someone mentions a fixed width solution. Thought this might point you in the right direction.

I need an option to disable line wrap, similar to ‘readable line length’, instead of relying on a CSS hack. Currently, the code block is too difficult to read. Few websites on the internet cause the code block to wrap.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.