Ensure consistency in the width of all div child pre elements by adjusting their size in the CSS code, even if one exceeds 100

As I'm working on formatting a diff in the browser, I have a parent div container and each line of the diff file is displayed within a pre tag.

The pre tags are set to display one per line, with the ability to scroll horizontally if they expand beyond 100% of the parent container's width.

While this setup works well, there seems to be a minor issue where only the pre tag that exceeds 100% width has its full length shown, while the others stop at that limit. Is there a way to ensure consistent widths for all pre tags, regardless of whether they exceed 100%?

If possible, I would prefer a solution using just HTML and CSS. However, I am open to a jQuery solution if it simplifies the process.

  .diff-wrapper {
    border: 1px solid #d2d2d2;
    margin:20px;
    overflow-x: scroll;
    position: relative;
  }

  .diff-wrapper pre {
    background:transparent;
    border:0;
    border-bottom:1px solid #eee;
    border-radius:0;
    display: inline-block;
    height:28px;
    line-height:28px;
    margin:0;
    min-width:100%;
    overflow: visible;
    padding:0;
    padding-left:10px;
    padding-right:10px;
    position: relative;
    white-space: pre;
    word-wrap:normal;
  }

  .diff-wrapper pre.added {
    background-color:#ddffdd;
  }

  .diff-wrapper pre.removed {
    background-color:#fee8e9;
  }
<div class="diff-wrapper">
    <pre>+ some short text here</pre>
    <pre>+ some short text here</pre>
    <pre class="added">+ some loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong text here</pre>
    <pre>+ some short text here</pre>
    <pre class="removed">+ some short text here</pre>
    <pre class="removed">+ some short text here</pre>
</div>

Answer №1

To achieve this effect, follow these steps:

Firstly, include another container inside your .diff-wrapper

<div class="diff-wrapper">
    <div class="diff-content">
        <pre>+ enter brief text here</pre>
        <pre>+ enter brief text here</pre>
        ...
    </div>
</div>

Next, apply the following CSS code

.diff-content {
    display:table;
}

Answer №2

Taking inspiration from various responses, I have made some modifications to the code. I included an extra <div> to manage the display and maintain the width, while the diff-wrapper took care of the overflow. Additionally, I made adjustments to the <pre> elements by setting display: block; so they can fully extend their width for the background-color, ensuring that padding is calculated accordingly.

  .diff-wrapper {
    border: 1px solid #d2d2d2;
    margin:20px;
    overflow-x: scroll;
    position: relative;
  }
  .diff-wrapper > div {
    display: table;
    width: 100%;
  }
  .diff-wrapper pre {
    background:transparent;
    border:0;
    border-bottom:1px solid #eee;
    border-radius:0;
    display: block;
    height:28px;
    line-height:28px;
    margin:0;
    min-width:100%;
    overflow: visible;
    padding:0;
    padding-left:10px;
    padding-right:10px;
    position: relative;
    white-space: pre;
    word-wrap:normal;
  }
  .diff-wrapper pre.added {
    background-color:#ddffdd;
  }
  .diff-wrapper pre.removed {
    background-color:#fee8e9;
  }
<div class="diff-wrapper">
    <div>
        <pre>+ some short text here</pre>
        <pre>+ some short text here</pre>
        <pre class="added">+ some loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong text here</pre>
        <pre>+ some short text here</pre>
        <pre class="removed">+ some short text here</pre>
        <pre class="removed">+ some short text here</pre>
    </div>
</div>

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

Exploring the connections between Ajax, asp.net mvc3 routing, and navigating relative URLs

My ASP.NET MVC3 application is live at a URL like this: http://servername.com/Applications/ApplicationName/ In my code, I am making jQuery AJAX requests in this manner: $.get(('a/b/c'), function (data) {}, "json"); When running the applicati ...

Utilizing jQuery Autocomplete with JSON to fetch consistent results

I have this specific code snippet within my index.php file: $(document).ready(function(){ $("#searchInput").autocomplete({ source: "getResults.php" }); Furthermore, the getResults.php script is as follows: <?php $result = array(); array_push($result, ...

How can I make an AJAX request in Rails 3 to retrieve an HTML page?

I am experimenting with using Rails to display an .html.erb template via an ajax request under specific conditions. By default, I am consistently receiving the .js.erb file when making an ajax request. Without delving into the reasons behind this choice, ...

Adding fadeIn effect to a prepended element using jQuery

I am struggling with this piece of code: $.ajax({ url : url, data : {ids : JSON.stringify(jsonids), hotel_id: hotel_id}, success : function(response) { $('#be-images ul').prepend(response).fadeIn(&apos ...

A guide on creating a Utility function that retrieves all elements in an array starting from the third element

I've been working on a tool to extract elements from an array starting after the first 2 elements. Although I attempted it this way, I keep getting undefined as the result. // ARRAYS var arr = ['one', 'two', 'three', &a ...

User controls in ASP.NET may not trigger the jQuery (document).ready() function

I wrote a jQuery function within my ASP.net user control that is not functioning as expected: <head> <title></title> <script src="~/Scripts/jquery-1.4.1.js" type="text/javascript"></script> <script language="javascript" ty ...

What is the best way to horizontally center my HTML tables and ensure that they all have uniform widths?

I'm currently facing an issue where I'd like to center align all tables similar to the 'Ruby Table.' However, I'm unsure of how to achieve this. You may also observe that some tables have varying widths. What CSS rule can I apply ...

Establishing connection with SignalR JavaScript client

I'm unfamiliar with SignalR and I find myself feeling lost. My task is to establish a connection to a stream and retrieve certain data. The owner provided some guidance, but they are unsure how to accomplish this using JavaScript. They shared the fol ...

Guide to positioning a card in the middle of a container using Bootstrap

I am struggling to center a card inside a container using CSS properties like justify-content-center, mx-auto, and hardcoding. However, I have not been able to achieve the desired result. Here is the code snippet from my HTML file: <div class="cont ...

Encountering redundant links during the scraping process

I'm currently working on extracting "a" tags with the class name "featured" from a specific website . Although the code I've written seems to be error-free, it unfortunately duplicates the links. How can I resolve this issue and avoid the duplica ...

"Facing an issue where jQuery autocomplete is not retrieving data from PHP source

Trying to implement autocomplete functionality in an input field while typing. Here is what I have attempted so far: jquery This code snippet is placed within $(document).ready(function(){ $(".auto").autocomplete({ source: "../ ...

Switch classes according to scrolling levels

My webpage consists of multiple sections, each occupying the full height and width of the screen and containing an image. As visitors scroll through the page, the image in the current section comes into view while the image in the previous section disappe ...

Implementing the Audio() Element with JavaScript

I've written the code below, but it's not working properly! When I click on the play button, nothing happens HTML: <button id="play"><img id="playicon" src="img/Polygon 1.svg"></button> JS: I have a variable named 'song0 ...

How come the data in the datatable does not appear sorted as it was retrieved from the ajax response?

I am successfully able to load the Datatable using ajax. However, I am facing an issue where the table does not display the data in the same order as it is received. The data is fetched from report_ajax.php [json] {"data":[ ["22:00:00" ...

Updating data in a table using identifiers from another table

I would greatly appreciate any assistance in advance! Within my database, I have two tables - var table and val table. var table varid image vartype val table valid sig winner varid These tables are related in a one-to-many relationship, indicating t ...

Krajee Bootstrap File Input: Easily upload files along with your form

I am facing an issue where I am unable to submit the form along with the selected files. I have tried both methods mentioned in the documentation. My requirement is to send everything together by clicking the submit button on the form. Here is the HTML co ...

I'm trying to position this div within a table so that it lines up with one of the cells. Can you help me figure out

After implementing the code, here is the current output: <table><tr> <td><div id="adt"></div></td> <!-- 336x280 ad code --> <td id="butw"><div id="butto">Follow @Twitter</div></td> ...

Maintaining fixed position for cursor events in CSS

Here is a simple popup I created using tailwindcss to display project descriptions. Take a look at the code snippet below: // Code snippet here... I am facing an issue where both the container (outer div) and the first inner div have the same click event. ...

Performing two simultaneous AJAX requests using tokeninput

I've been playing around with the tokeninput plugin and it's been working really well for me. However, I recently came across a new requirement - I need to make two separate ajax calls on the same input bar. Situation: My input bar is as follows ...

What is the best way to determine the size of the URLs for images stored in an array using JavaScript?

I am working on a project where I need to surround an image with a specific sized 'div' based on the image's dimensions. The images that will be used are stored in an array and I need to extract the height and width of each image before disp ...