Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get scrollbars on container when syntax in code tag is longer than the view

When displaying syntax in a code element, how can I apply scrollbars if a line of syntax is longer than the view?
I’ve tried overflow: scroll, but it doesn’t seem to work how I want it.

As you can see in the picture, overflow causes the browser to scroll horizontal.
enter image description here

To see live demo, check out https://blog.sltech.no/Stein/Article/1

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

HTML:

<table class="syntax-content-wrap">
    <tr>
        <td class="line-nr-wrap"><span class="nr">6</span></td>
        <td class="syntax-line-wrap"><pre><code>    public DefaultRoute(string controller, string action) : base(Feature.DefaultRoute)</code></pre></td>
    </tr>
</table>   

Sass/CSS:

table.syntax-content-wrap {
    line-height: 1;
    margin-left: 7px;
    margin-top: 11px;

    & td.line-nr-wrap {
        vertical-align: middle;
        width: 44px;

        & > span.nr {
            font-size: 11px;
            font-weight: bold;
        }
    }

    & td.syntax-line-wrap {
        overflow: scroll;
        
        & > pre {
            display: block;
            margin: 0px;

            & > code {
            }
        }
    }
}

>Solution :

Two things:

  1. Since you have white-space:pre-line on code elements in Stein.Article.min.css, lines will break at spaces where necessary, so you’re less likely to get a line that is long enough to require scrolling (unless you have a very long word). If you want single-line behaviour, use white-space: nowrap instead.
  2. Since your pre blocks have no max width, there cannot be overflow; in other words, their content cannot go out of bounds since there are no bounds. Adding a width or max-width will fix that.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading