How to make Twitter Bootstrap's <pre> blocks scroll horizontally?

HtmlCssTwitter BootstrapHorizontal ScrollingPre

Html Problem Overview


Per the examples https://getbootstrap.com/docs/4.3/content/code/#code-blocks, bootstrap only supports vertically-scrollable and word-wrapped <pre> blocks out of the box. How do I make it so that I have horizontally-scrollable, unwrapped code blocks instead?

Html Solutions


Solution 1 - Html

The trick is that bootstrap overrides both the white-space and the CSS3 word-wrap attributes for the <pre> element. To achieve horizontal scrolling, ensure there's this in your CSS:

pre {
  overflow: auto;
  word-wrap: normal;
  white-space: pre;
}

Solution 2 - Html

This worked for me:

pre {
    overflow-x: auto;
}
pre code {
    overflow-wrap: normal;
    white-space: pre;
}

Solution 3 - Html

I keep coming back to this answer when I start new projects and have to read through comments to remember, oh yea, don't override the bootstrap class and don't forget overflow-wrap, etc...

So you have the stock pre-scrollable, which is vertical only scrolling, you may also want:

Horizontal only scrolling

.pre-x-scrollable {
    overflow: auto;
    -ms-word-wrap: normal;
    word-wrap: normal;
    overflow-wrap: normal;
    white-space: pre;
}

Vertical and Horizontal scrolling

.pre-xy-scrollable {
    overflow: auto;
    -ms-word-wrap: normal;
    word-wrap: normal;
    overflow-wrap: normal;
    white-space: pre;
    max-height: 340px;
}

Solution 4 - Html

Newer versions of Bootstrap apply styles to both pre and code that wrap words. Adding this to my stylesheet fixed the issue for me:

pre code {
        white-space: pre;
        word-wrap: normal;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSuanView Question on Stackoverflow
Solution 1 - HtmlSuanView Answer on Stackoverflow
Solution 2 - HtmlMatt MoreyView Answer on Stackoverflow
Solution 3 - HtmlRick GlosView Answer on Stackoverflow
Solution 4 - Htmljarhill0View Answer on Stackoverflow