Ways to enable scrollbars within a container when the content inside a code tag exceeds the visible area

How do I add scrollbars to a code element if the line of syntax is longer than the view? I've attempted using overflow: scroll, but it's not functioning as expected.

As shown in the image, using overflow causes the browser to scroll horizontally. https://i.sstatic.net/82yPbzzT.jpg

To see a live demonstration, visit

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 {
            }
        }
    }
}

Answer №1

Here are a couple of points to keep in mind:

  1. If you have set white-space:pre-line on code elements in your Stein.Article.min.css file, lines will break at spaces when necessary, reducing the chances of long lines that require scrolling (unless there's an exceptionally long word). To maintain single-line behavior, consider using white-space: nowrap.
  2. Without a specified max width for your pre blocks, there won't be any overflow; this means their content will not go beyond certain limits as there are none set. To prevent this, you can add either a width or a max-width property.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

CSS - Resizing Images

I am experiencing an issue with the width of an image on my website. I want it to span 100% of the browser frame, but it is currently limited to only 1000px wide. Please take a look at: The image in question is located at the top of the page. Is there a ...

I have noticed that the SVG viewBox appears different when using Vue.js

I cannot wrap my head around the fact that my svg viewBox acts differently in html compared to vuejs. Here is my code in vue: <template> <div class="venuecontainer" style="background-color:#808040;"> <svg class=&quo ...

Struggling with implementing ng-repeat in AngularJS for displaying HTML content

I stumbled upon a post that covers pretty much what I'm trying to achieve: AngularJS - Is it possible to use ng-repeat to render HTML values? Currently, the code appears like this and displays correctly as text: <div ng-repeat="item in items.Item ...

Overflowing goodness: Bootstrap 4 row spills beyond container boundaries

After deciding to experiment with Bootstrap, I chose version 4 to work with. As I started structuring the template, I encountered an issue where the main content row extended beyond the bounds of the container div, unlike the other rows on the page. My at ...

Interact with webpage dropdown menus using Python

I'm currently working on a Python script that interacts with a webpage, specifically , to retrieve a (DNA sequence in fasta format) file. The file is automatically downloaded when a user clicks on a dropdown menu. Here's the dropdown menu: Dow ...

What is the best way to align a logo in a log-in screen?

I am having an issue with centering the logo on my login form. The "signin" text is centered perfectly, but the logo always stays at the top left corner. I don't understand why the logo is not centered. If anyone has a solution to propose, I would gre ...

Angular 8 Input Validation: Implementing a Directive for Number Models

Criteria for Acceptance: • Input should only accept numbers • Numbers can have two decimal places • Both point and comma are valid separators (11,00 , 12.00) • Negative numbers should not be allowed • Point and comma cannot be ente ...

CSS style for centering double digit list items

My query is: <ul id="list"> <li> <a href=''>1</a> </li> <li> <a href=''>2</a> </li> <li> <a href=''>3</a> </li> <l ...

Adding gaps between hyperlinks in the sidebar navigation using Bootstrap 4

I created a sidebar containing links and I am attempting to add space between each link. Here is the code snippet I have tried: .sidebar-nav>li>a{ padding-bottom: 500px; } Unfortunately, this code is not producing the desired result. Are there any ...

Prevent the href from navigating to the next page when a doubleclick event listener is triggered

In my project, I am using an anchor tag that redirects to another page. However, if the user double clicks on it, I want to prevent the default behavior and stop the redirection. <a href="{location}">My Link</a> element.addEventListen ...

Switch between divs based on the current selection

var header = $("#accordion"); $.each(data, function () { header.append("<a id='Headanchor' href='javascript:toggleDiv($(this));'>" + this.LongName + "</a>" + "<br />", "&l ...

What's causing this unexpected wrapping issue?

I must be overlooking something simple. Could someone please review this page and explain why the BYOB section is appearing on the right, rather than directly below the American section? Thank you in advance. ...

What is the reason tailwind does not take precedence over locally defined styles?

I've been experimenting with changing the default text color using Tailwind CSS, but for some reason, it's not taking effect. I've noticed that Bootstrap is able to override the default style without any issues. I'm fairly new to Tailw ...

When the <button> element associated with the JavaScript code is clicked, no result is

The code below is causing me some trouble; I can't seem to find the error in it. <!DOCTYPE html> <html> <head> <title>Party Sequence</title> </head> <body> <label for="inputN">Enter the valu ...

list without specific order spacing

I have implemented an unordered list in the footer section of my HTML document. Within this list, the first two items consist of social media logos displayed as images. To enhance their visibility and prominence within the footer, I have assigned them a wh ...

An unusual problem arose within the Django template

When I use the action method in Django, I pass a parameter called deletable_objects to the template and it works perfectly. The code for this is: deletable_objects, perms_needed, protected = get_deleted_objects( queryset, opts, request.user ...

What is the purpose of the c() function in JavaScript?

I recently stumbled upon some interesting JavaScript code in the source of a web page: function fsb329142055() { var b=new Array(57,50,102,50,52,99,50,53,52,56,102,98,102,98,101,102,101,49,53,61,101,99,110,57,111,78,109,54,114,111,56,48,102,38,1 ...

a search-enabled dropdown menu within an Angular input field

I can't seem to get the input box to display in my code below. Can you help me figure out why? component.html <form [formGroup]="formGroup" (validSubmit)="onSubmit()"> <div class="form-group mb-3"> ...

What is the best way to inject child nodes into parent nodes without causing the view to shift to the location where the element is being

While I am rendering elements and appending them to a parent div, my screen keeps jumping to the bottom-most element as it loads instead of maintaining the current view. Ideally, it should start at the top of the screen and remain there without any sudden ...

Issues with Bootstrap slider functionality : Unable to initiate slider as it is not being recognized as

I'm having an issue with my Bootstrap slider not functioning properly. Despite ensuring that all CSS and JS files are loading correctly in my HTML, I keep receiving an error in the console stating: slider is not a function. I have even downloaded the ...