Ensure that the <pre> tag's vertical and horizontal overflow is clipped outside of its container, displaying scrollbars as needed

Within a div element set to a specific size of 300px by 300px, there lies a pre tag containing a snippet of source code. The size of the snippet can vary. My goal is to style both the div and the pre tag in a way that prevents horizontal wrapping of the source code unless there is a line break. Additionally, any overflow should be hidden with scroll bars displayed, catering to both horizontal and vertical overflow scenarios.

Currently, I have managed to implement a vertical scroll bar while facing issues with text wrapping horizontally. Below is the CSS code I am using (largely adapted from StackOverflow's code formatting):

pre {
    max-height: none\9;
    padding: 5px;
    font-family: Consolas, Monaco;
    margin-bottom: 10px;
    overflow: auto;
    text-overflow: clip;
    width: 300px;
    height: 300px;
}

Furthermore, here is an example of how the HTML structure looks like:

<div class="item" data-handle=".snippetHeader">

<h5 class="snippetHeader">Snippet7</h5>

<div class="code">
    <pre>
        <code>

DWORD WINAPI ValueFunc(LPVOID arg){
    //printf(&quot;Thread Created\n&quot;);
    x = (i + .5)*step;
    sum = sum + 4.0 / (1. + x*x);
    return 0;
}

int main(int argc, char* argv[]) {
    clock_t start, stop;
    step = 1. / (double)num_steps;
    start = clock();

    HANDLE hThread[num_steps];
    for (i = 0; i&lt;num_steps; i++) {         

        hThread[i] = CreateThread(NULL, 0, ValueFunc, NULL, 0, NULL);

    }

    WaitForMultipleObjects(num_steps, hThread, TRUE, INFINITE);

    pi = sum*step;
    stop = clock();
    printf(&quot;The value of PI is %15.12f\n&quot;, pi);
    printf(&quot;The time to calculate PI was %f seconds\n&quot;, ((double)(stop - start) / 1000.0));

}
        </code>
    </pre>
</div>

UPDATE:

I have incorporated Bootstrap css on this page, leading to unwanted display behavior. Upon commenting it out, the text appears as desired. A jsfiddle showcasing the issue can be found here: http://jsfiddle.net/Y29AM/. Removing the two external resources specified there will highlight the impact of Bootstrap. Any suggestions on what aspects of Bootstrap need modification to achieve the intended result?

Answer №1

display: inline-block;

Disable automatic line breaks.

Answer №2

After tweaking the CSS styles for pre, I achieved the desired outcome:

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

In addition, I included the following CSS:

pre code {
    white-space: inherit;
}

The text is now displaying without horizontal wrapping and functioning as intended.

Answer №3

I found success with the following solution:

pre {
    white-space: pre-wrap;
}

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

The lower division is encroaching on the upper section

I am facing an issue getting the mid section content div to display below the top div. The bottom div is currently overlapping and showing up underneath or at the bottom of the top div. To help you understand better, here are two images and the relevant c ...

What is the best way to have an HTML radio button automatically display as selected upon loading if the stored value is "on"?

Utilizing Javascript alongside html: I am facing an issue where a list containing radio buttons is dynamically loaded based on stored data when the page launches. Despite the stored value of the radio being set to "on", the radio button does not show up a ...

What is the best way to eliminate the blue border surrounding the input element in Bootstrap 4?

https://i.sstatic.net/a5BKg.png I have noticed that when I click on the input field, a blue border appears around it. Is there a way to remove or change this border? <div class="input-group mb-3"> <input type="tex ...

Mobile view causes malfunction in Bootstrap Toggle Burger Menu functionality

<nav class="navbar navbar-expand-lg navbar-dark px-2 " style="background-color:#E53935;"> <a class="navbar-brand" href="#">FoodFusion</a> <button class=&quo ...

Transforming a pandas dataframe into an interactive HTML table, emphasizing particular data points

I need help with converting pandas data frames to HTML and highlighting specific elements. My task involves creating two pandas dataframes, rendering them to HTML, and comparing their elements: import pandas as pd import webbrowser data1 = [1, 2, 3, 4, ...

Why do I need to double-click? (JavaScript onclick event handler)

As a beginner in JavaScript and web development, I encountered a peculiar issue for the first time. In a simple example, I have two divs - left and right. The left div contains five images, while the right div is empty. There are two buttons - copy and del ...

Is there a way to prevent an external script from making changes to an inline style?

There is a mysterious script running on a page that seems to be controlling the height of an inline style. The source of this script modifying the height property is unknown. <div class="vgca-iframe-wrapper wpfa-initialized" style="heigh ...

Displaying a specific section of HTML only if a certain resource bundle key is present

Working with JSF and needing to load a bundle named Extra, I have encountered an issue. <f:loadBundle basename="com.ni.lib.extra.delivery.ExtraBundle" var="extra" /> Within the variable "extra", there exists a value known as downtime_notice. To imp ...

How can I use jQuery to create a new div element if it is not already present

I am familiar with how to add a class if it does not already exist, for example: $("ul:not([class~='bbox'])").addClass("bbox"); Alternatively, if(!$("ul").hasClass("class-name")){ $("ul").addClass("bbox"); } However, I am unsure how to c ...

Validation on the client side will now support the input of comma-separated values

Is there a way to use the jQuery.validator.addClassRules method in order to validate input fields and restrict my textbox to only accept comma-separated values? I also want it to display a default message if incorrect. <input type="text" cla ...

Loop through individual divs containing radio buttons and check them one by one with a slight delay

I have implemented HTML tabs using radio buttons, similar to the demo showcased at https://css-tricks.com/examples/CSSTabs/radio.php. Here's the basic markup: <div class="tab"> <input type="radio"> <p>Content displayed when radio bu ...

Barba.js Revolutionizes Page Reloading for Initial Links

On my Wordpress site, I am using Barba js v.2 for page transitions. However, I have encountered an issue where I need to click on a link twice in order to successfully change the page and make the transition work. Interestingly, the first time I click on t ...

What methods can be used to identify the specific Router component being rendered in React?

I am currently working with a Navbar component that I want to render with different CSS styles using styled-components based on the route of the component being rendered. How can I determine whether that component is being rendered or not? const NavbarCont ...

Troubleshooting Cross-Origin Resource Sharing (CORS) problem in Jquery

When using AJAX post from jQuery to call two REST services on different domains for business logic, a CORS issue arises. By setting crossDomain: true in my AJAX call following Google references, it works fine without specifying headers. However, if I inc ...

Customize the appearance of the placeholder text in ui-select-match

I am trying to style the placeholder text of the following element: <ui-select-match placeholder="My hint"> {{$someItem}} </ui-select-match> I want to change the style of the placeholder text to be bold, italic, or other formatting option ...

Retrieving the chosen option from a drop-down menu using FormCollection in MVC

I need to extract the selected value from a drop-down list in my HTML form, which is posting to an action using MVC. How can I achieve this? Here is my HTML form: <% using (Html.BeginForm()) {%> <select name="Content List"> <% ...

Increase the font size of a div using CSS to make it appear larger

Utilizing a WYSIWYG-editor, I am creating content that I want to display with double font-size, without directly altering the HTML code. Here is an example of the HTML structure: <div id="contents"> <p style="text-align:center"> <spa ...

The search functionality in an Html table is currently malfunctioning

Currently, I am working on developing a search mechanism in HTML. It seems to be functioning properly when searching for data for the first time. However, subsequent searches do not yield the expected results. Additionally, when trying to search with empty ...

Reveal Visual Content upon Hovering

Is there a way to show an image only when the mouse hovers over it? Additionally, can we underline the adjacent text at the same time? If the mouse moves away from the image, I'd like it to hide again and revert the text back to its original state. T ...

Unusual marker appearing on every page utilizing ionic v1 and angularjs

For some unknown reason, a dot appears at the upper left side of each page in my webapp: https://i.stack.imgur.com/nN61t.png It seems to be an issue with the HTML code. When I run the snippet below, the dot is visible: <ion-view view-title="Send fe ...