Avoiding interference with adjacent elements caused by long content in a div

Hey there, I could really use some help with my CSS layout,

I'm pretty new to CSS and Flexbox and I'm trying to create a simple layout.

The issue I'm running into is how to stop long content inside a pre tag from pushing other divs.

Here's what it looks like before the long code content:

https://i.sstatic.net/eyg7N.png

And after the long code content:

https://i.sstatic.net/cGHI1.png

From the image, there are 2 problems that stand out:

  1. The left sidebar and right sidebar are being pushed by the main content
  2. The pre content should wrap in a scrollable way similar to StackOverflow

I was considering setting a max-width on the pre tag and using overflow: scroll, but how can I set the max-width based on the parent container? (the parent container is fluid)

Here's the link to the codepen:

https://codepen.io/cyberflyx/pen/VwaxNWp

<html>

<head>
  <title>Learn Max Width</title>
</head>

<body>
  <div class="flex justify-between">
    <div class="left-sidebar bg-blue p-4">
      <h3>This is Left Sidebar</h3>
    </div>
    <div class="main-content flex-1 bg-yellow p-4">
      <div class="content-body">
        <h3>Main Content</h3>
        <p>This is long content between all of use</p>
        <pre>
        Sep 10 17:10:30 api-testapp-03 testapp.commands.generate_logs.run_test(): NOTICE: Got events:/home/m/app/testapp/queues/test.queue 


      </pre>

      </div>

    </div>
    <div class="right-sidebar bg-red p-4">
      <h3>This is Right Sidebar</h3>
    </div>
  </div>
</body>

</html>

CSS

.flex {
  display: flex;
}

.justify-between {
  justify-content: space-between;
}

.left-sidebar {
}

.content-body {

}

.right-sidebar {
}

pre {
  background-color: pink;
  padding: 4px;
  border-radius: 10px;
/*     overflow: scroll; */
/*   max-width: 500px; */
}

.flex-1 {
  flex: 1 1 0%;
}

.p-4 {
  padding: 4px;
}

.bg-blue {
  background-color: blue;
}

.bg-yellow {
  background-color: yellow;
}

.bg-red {
  background-color: red;
}

Answer №1

The issue lies with the <pre> element that is not wrapping its content properly. You can address this by utilizing the white-space CSS property:

.box {
  display: flex;
}

pre {
  background-color: gray;
  border-radius: 8px;
  padding: 6px;
  white-space: pre-wrap;
}

.p-6 {
  padding: 6px;
}

.bg-green {
  background-color: green;
}

.bg-purple {
  background-color: purple;
}

.bg-orange {
  background-color: orange;
}
<div class="box justify-between">
  <div class="side-panel bg-green p-6">
    <h3>Left Panel</h3>
  </div>
  <div class="main-section flex-1 bg-purple p-6">
    <div class="content">
      <h3>Main Section</h3>
      <p>This section contains important information.</p>
      <pre>
        Feb 20 09:45:22 log-server test.commands.generate_logs.process_data(): WARNING: Error in processing data:/logs/test/server/log_file.log 
      </pre>

    </div>

  </div>
  <div class="side-panel-right bg-orange p-6">
    <h3>Right Panel</h3>
  </div>
</div>

Answer №2

To make a <pre> element scrollable, I applied the CSS property overflow: auto; directly to the <pre> tag. Additionally, as there are 3 child elements within a flex container, I set a max-width of 33% to each child element with the class of flex. Below is the finalized CSS code snippet:

.left-sidebar, .main-content, .right-sidebar {
    max-width: 33%;
}

pre {
    overflow: auto;
}

The crucial step is assigning a maximum width of 33% to all child elements, effectively dividing the parent container into nearly equal parts.

Furthermore, keep in mind that using justify-content: space-between; may introduce spacing between the 3 divs. To eliminate this space, apply your custom flex-1 class to the left-sidebar and right-sidebar div elements.

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

Using JavaScript in PHP files to create a box shadow effect while scrolling may not produce the desired result

Issue at hand : My JavaScript is not functioning properly in my .php files CSS not applying while scrolling *CSS Files are named "var.css" #kepala { padding: 10px; top: 0px; left: 0px; right: 0px; position: fixed; background - c ...

What are the steps to designing a Vertical Curve UI?

Is there a way to replicate the unique vertical curve on the login page, as shown in the image below? I've come across horizontal SVGs that can achieve this effect, but I'm struggling to find resources on implementing it vertically. How is this ...

Why isn't my textarea in jQUERY updating as I type?

On my website, I have a comment script that is not functioning correctly in some parts of the jQuery/JavaScript code. Instead of posting an edited comment to PHP, I created a notification window to test if the value passed through is actually changing. W ...

Tips on removing pesky favicons

Not too long ago, I purchased a domain and configured it with Wordpress. Eventually, I decided to switch to a static landing page so I uninstalled Wordpress. But now there's an unfamiliar favicon that has appeared on my site. Even after deleting all ...

What are the steps to insert a plotly graph into a webpage so that it responds to different

I have been working on integrating a plotly pie chart into my website. In order to make the iframe responsive, I decided to utilize this specific tool to generate the necessary html/css. While the chart appears satisfactory on a laptop screen, it is barely ...

The body and HTML elements are bloated with an excessive amount of

Before we dive in, check out this code pen. CSS <body> <div class="header"> <img src="https://images.unsplash.com/photo-1586244439413-bc2288941dda?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=cro ...

The Electronjs application encountered an error while loading with the message "ReferenceError: require is not defined."

Having trouble with an electron application as I encounter an error when running npm start: $ npm start > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="483c2d3b3c087966786678">[email protected]</a> start C:& ...

Various JSON Tag values occurring more than once

Upon receiving a JSON Object in HTML, I am passing it to a JavaScript function defined below. The issue arises when the same folderName appears repeatedly on the HTML page. JSON Object: { "folderName": "arjunram-test", "objects": [ { ...

Remove all objects from a model based on a specific value by implementing a button in Django

For reference, please take a look at the table screenshot: https://i.sstatic.net/evBSK.png I am attempting to remove a row from the table using the corresponding Delete Asset button (meaning deleting all rows in a django model with Symbol: 1INCHUP) The s ...

Creating a dynamic user interface with HTML and JavaScript to display user input on the screen

I'm currently working on creating an input box that allows users to type text, which will then appear on the screen when submitted. I feel like I'm close to getting it right, but there's a problem - the text flashes on the screen briefly bef ...

In Vue, when you want to display text after reaching a height of 50px, any additional text will automatically be replaced by five full

https://i.stack.imgur.com/mp0YJ.png >>>>>>>>>Explore Sandbox Example <div style="height:50px"> ...

Enhance your picture links with a:hover box-shadow styling

I've been struggling to add a box shadow on hover to a Facebook icon image. I assumed it would be straightforward, but as always, I was wrong. I'm working with a child theme in WordPress because I recently started, thinking it would be an easy wa ...

Vue Quasar Drawer Layout with Bottom Bar

Struggling to implement a footer in my q-drawer. Below is the template and component code I am currently using: <template> <q-layout view="hHh lpR fFf"> <q-header elevated> <q-toolbar> <q-btn flat de ...

AngularJS File Input element triggering only once when selecting the same file

I have created an Angular directive to customize the file upload input. directive('ngFile', function () { return { link: function (scope, element, attrs) { var button = element.find('button[data-fileInputButton]&apos ...

problem with transmitting information through $.ajax

I'm facing a frustrating issue with the $.ajax()... function in my PHP code. I am unable to retrieve the data sent by the ajax request. <?php if ($_POST){ include 'Bdd_connexion.php'; $filiere = $_POST['filiere']; ...

Which specific HTML element triggers the initiation of ajax in Jquery?

I have a situation where various HTML elements trigger ajax requests when clicked. My goal is to determine which specific element was clicked inside the ajaxComplete event. Although I attempted to use event.target, it only returns the entire document inst ...

Use multiple lines to create a stunning visualization using morris.js. Let your data

I need help creating a graph using morris.js with 2 lines. I have two sets of data in one array, but I can't seem to display both lines on the graph simultaneously. When I use todos[0], only the first line is visible, and when I use todos[1], only the ...

Issue observed with alignment not being corrected when the browser is resized on a responsive webpage

Utilizing Bootstrap3 for my responsive page design. In the banner content, I am including a box with text inside: <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"/> <div class="row"> <s ...

Steps for generating a div, link, and image that links to another image using Javascript

Hey there, I have a cool picture to share with you! <img src="cards.png" id="img"> <!--CARD PICTURE--> Check out what I want to do with it: <div class="container_img"> <img src="cards.png" id="img"> <!--CARD PICTURE--> ...

A guide on incorporating an event to multiple elements in vue.js

I am trying to implement a double-click event on multiple elements in Vue3. My goal is to create a table of contents data and add a double-click event to the checkboxes and favorite icons without using @dblclick="". I find it more convenient to assign a c ...