Increasing the size of a card beyond the container using the CSS PRE tag

I am facing an issue with a card on my webpage. It contains a pre tag inside it, which is causing the size of the card to extend beyond its container, resulting in a horizontal scroll bar appearing. How can I make the content within the pre tag scroll horizontally if it exceeds the size of the card? Similar to how code snippets work on Stack Overflow answers, where they have a scroll bar within the snippet itself to prevent horizontal overflow.

Below is the CSS code:

.main {
    width: 100%;
    background-color: green;
    display: flex;
    flex-direction: row;
    justify-content: stretch;
    align-items: flex-start;
    padding: 20px;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box; 
}
.menu {
    width: 200px;
    height: 300px;
    background-color: red;
    flex-grow: 0;
    flex-shrink: 0;
}
.card-container {
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
    background-color: blue;
    height: 500px;
    display: flex;
    flex: 1;
    flex-direction: column;
    align-items: stretch;
    justify-content: flex-start;
}
.card {
    position: relative;
    flex-grow: 1;
    flex-shrink: 1;
    flex-basis: 0;
    margin: 20px;
    background-color: yellow;
    max-width: auto; height: 300px;
}
pre {
    background-color: black;
    color: white;
    overflow: scroll;
}

And here is the HTML code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="flex-style.css">
    <title>Document</title>
</head>
<body>
    <div class="main">
        <div class="menu"></div>
        <div class="card-container">
            <div class="card">
                <h1 class="heading">Heading</h1>
                <p class="paragraph">
                    Lorem ipsum dolor sit amet consectetur adipisicing elit. Iste quaerat earum cum dolore saepe eius dignissimos porro, natus ut omnis maiores deleniti repudiandae distinctio quod? Vero tempore laboriosam maxime quidem accusantium ea qui enim doloremque? Maxime accusantium fuga inventore veniam vero quaerat possimus, id magni consectetur, omnis obcaecati recusandae ipsam.
                </p>
                <pre>
                    Lorem ipsum dolor sit amet consectetur adipisicing elit. Culpa placeat quia cum, corporis amet quas repellat, nostrum inventore numquam accusamus possimus minima maxime error iusto obcaecati consequuntur fugiat magnam. Temporibus?
                </pre>
            </div>
        </div>
    </div>
</body>
</html>

Any assistance on this matter would be highly appreciated.

Answer №1

The solution you need is to use the CSS property white-space: pre-wrap:

pre {
    white-space: pre-wrap;
}

This property maintains the original behavior of the <pre> element, preserving multiple spaces and line breaks. However, if the content is too wide for its container, it will be wrapped.

Answer №2

https://www.w3schools.com/tags/tag_pre.asp

When text is placed within a "pre" element, it will be displayed in a fixed-width font and will retain both spaces and line breaks. This ensures that the text appears exactly as it was written in the HTML source code.

If you would like to achieve a similar effect without using the "pre" tag, you can use the following code:


          <div
            class="container-outer"
            style="
              overflow: scroll;
              overflow-y: hidden;
              width: 350px;
              height: 210px;
            "
          >
              <div class="container-inner" style="width: 500px">
                Lorem ipsum dolor sit amet consectetur adipisicing elit. Culpa
                placeat quia cum, corporis amet quas repellat, nostrum inventore
                numquam accusamus possimus minima maxime error iusto obcaecati
                consequuntur fugiat magnam. Temporibus? Lorem ipsum dolor sit amet
                consectetur adipisicing elit. Culpa placeat quia cum, corporis
                amet quas repellat, nostrum inventore numquam accusamus possimus
                minima maxime error iusto obcaecati consequuntur fugiat magnam.
                Temporibus?
              </div>
          </div>

Referance

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

Break down and simplify the code into individual components

<input type='button' class="myButton" onclick="document.body.style.cssText+=';background-image: url(img01.jpg);background-repeat: no-repeat; -moz-background-size: cover; -o-background-size: cover; background-size: cover;';" value="S ...

Playing only audio and no video in an Android WebView using HTML5

I am currently facing a challenge with a webpage that includes an html5 video and css3 animations. While the web page functions perfectly on an android device's browser, I am experiencing laggy, glitchy, and jumpy animations when using hardware accele ...

How to adjust the font size and font style for the [pageSizeOption] in mat-paginator

I am having trouble adjusting the font-size of the [pageSizeOptions] in my mat-paginator element in the application. <mat-paginator [pageSizeOptions]="[10, 20, 30]"></mat-paginator> The "10" appears too small compared to the text "items per p ...

Achieving Vertical Alignment in a Bootstrap 4 Image Overlay Card

I am facing a challenge in trying to vertically center a FontAwesome icon within an Image Overlay Card. Despite attempting various methods like using d-flex and justify-content-center, none of them seem to be effective. What could I be overlooking? < ...

`The Conundrum of Basic HTML Parsing`

Hello, I am currently working on extracting professor names and comments from the ratemyprofessor website by converting each div into plaintext. The following is the structure of the div classes that I am dealing with: <div id="ratingTable"> <div ...

What could be causing jQuery animate to malfunction on mobile devices when a viewport is present?

Everything seems to be working fine on my desktop webpage, but when I try it on mobile, there is no scroll... $("HTML, BODY").animate({ scrollTop: 500 }, 1000); This post suggests that mobile devices may not scroll on the body, but on the vi ...

Conceal navigation buttons on the first and last pages of the slider

I'm attempting to develop a slider that hides the "previous" button when it is on the first slide and hides the "next" button when it reaches the last slide. The slider will be created using a combination of PHP, forms, and JavaScript. Below is the H ...

Out of the blue, the CSS functionality in my React app completely ceased to

I've been developing this website using React and Material UI, and I chose to implement standard CSS for styling. However, after closing my code editor and reopening it, some parts of the CSS do not seem to be loading properly. I'm completely puz ...

Firefox only loads images and jquery after a refresh of the page

Upon initially accessing my site after clearing the cache (a jsp page from a spring app), I noticed that the images and styles were not being applied. However, upon refreshing the page (ctrl-r), everything loaded perfectly. In Firefox's console outpu ...

Error: The absence of type definition

I am struggling to implement the functionality of an Add Question button, encountering an error message that reads: TypeError: form.questionText is undefined Can anyone point out where I went wrong? <script type="text/javascript"> fun ...

Tips for removing the default hover and click effects from a Material-UI CardActionArea

Check out the card sample with a lizard photo on https://material-ui.com/components/cards. When you hover over the cardActionArea, it will get darker or lighter based on the theme. Clicking on the card will make the picture change its brightness relative ...

Experiencing a problem when attempting to activate the html5 mode feature in AngularJS to eliminate the #

I've been scouring through various sources like stackoverflow and the web, but I haven't found a clear answer to my question. Can someone please help me out? Here's what I'm struggling with: In my AngularJS application, I enabled &apos ...

How can I alter the text color on hover within a Material UI card? My goal is to have the text color change when hovering over the card itself, rather than just the text

When I apply CSS to the card hover effect, it works fine. However, I also want to change the text color along with the card color on hover. card: { maxWidth: 350, height: 300, '&:hover': { backgroundColor: '#373737 !impor ...

The content is not wrapped when transferred from App.js through props, although it is being wrapped in other scenarios

As I work on developing a basic invoice generator application, I encounter an issue with overflowing content in the "notes" section when passing data through props from the App.js file. Instead of wrapping the text, it overflows its container. import "./Ap ...

Issues with vertical repeating in CSS Sprites

Hey there, I'm currently working on implementing css sprites in my web application. The challenge I'm facing is that I have organized the background of my website vertically within a sprite image. Now, one specific portion of the sprite needs to ...

Display a variety of body background images depending on the user's selection

Is it possible to change the background of the page when an option is selected from a dropdown menu? Additionally, can this be achieved with a smooth fade in and fade out animation? Here is a code example on CodePen. body ...

Adjusting the Size of an HTML Slideshow

While designing a homepage for my band, I encountered an issue when trying to integrate a slideshow element from an external source. The size of the slideshow is fixed at 600 x 300px and cannot be adjusted. Additionally, I found it challenging to position ...

Two elements occupy the rest of the vertical space

In the center, there is a div with content inside. I want the top and bottom divs to occupy the remaining space. Similar to this example <div class="container"> <div class="top"> </div> <div class="middle"> C ...

Is there a potential white-space issue with jQuery CSS in Internet Explorer versions 7 and 8

Recently, we encountered an unusual issue with jQuery and CSS while working on compatibility with IE7 and IE8. Our project involves animations and height measurements, and during the animations, we wanted to prevent text from wrapping. To achieve this, we ...

Improving the method for adding an element to an HTML document with jQuery

What is the best way to add this element to a specific DIV Class using JQUERY in an optimized manner? The elements are generated dynamically and I want to use .AppendTo to display them inside <div class='parent-list-workorder'>. This is wh ...