Ensure that a bootstrap table's cell occupies the entire available space, while also allowing for scrolling when there is overflow

Many questions have been raised about Bootstrap, Tables, and responsiveness. However, I have yet to find one that addresses my specific issue with getting a particular cell (<td> element) to:

(a) Fill the available space, stopping at the window boundaries (i.e., be fluid), and

(b) Add horizontal scrollbars if the cell would be too large.

Objective:

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

Current Solution:

Here is what I have come up with so far. Points to consider:

  • I am not using any custom CSS, only the one provided by Bootstrap 4.1.3
  • The
    <div class="table-responsive">
    is commented out as I do not want the entire table to be scrollable.
  • The
    <pre class="pre-scollable">
    part successfully adds a vertical scrollbar when the log has many lines, but it does not add a horizontal scrollbar.
  • Attempts to wrap the results-log cell in a bootstrap class="container-fluid" div did not yield the desired results.
  • Using
    <pre class="pre-scollable" style="width: 250px; overflow-x: scroll">
    achieves the horizontal scrollbar but sacrifices responsiveness.
  • Adjusting width: inherit does not resolve the issue either.
  • The entire content is within a
    <main class="container-fluid-page">
    and another div, which I believe should not impact this specific problem.
<!DOCTYPE html>
<head>
...
</head>
<body>
<!-- other stuff that doesn't matter -->
<main class="container-fluid-page">
<div id="content">

   <div id="results">
   <h2>Results:</h2>
   <!--<div class="table-responsive">-->
      <table class="table table-sm">
         <tbody>
            <tr>
               <th>Status</th>
               <td>
                  <div id="results-status">
                     foo
                  </div>
            </tr>
            <tr>
               <th>ReturnValue</th>
               <td>
                  <div id="results-return-value">
                     <pre><code class="json">{
  "__type__": "RecordRef",
  "name": "foo"
}</code></pre>
                  </div>
               </td>
            </tr>
            <tr>
               <th>ReturnValue</th>
               <td>
                  <div id="results-log">
                     <pre class="pre-scollable"><code>2019-05-23 07:51:32Z Log text
2019-05-23 07:51:33Z log text that can have a really really really long line in it and I want to scroll
2019-05-23 07:51:34Z log text</code></pre>
                  </div>
               </td>
            </tr>
         </tbody>
      </table>
      <!--</div>--> <!-- class="table-responsive" -->
   </div> <!-- id=results -->

</div> <!-- id=content -->

</main>
</body>

Edits:

  1. Removed col-* from <th> and <td> definitions.

Answer №1

Reflection

In my opinion, it would have been better not to alter your question based on my feedback. By doing so, you have potentially diminished the helpfulness of the question for future users facing the same issue:

"Why not utilize col-* classes on table cells?"

This is a valid question, especially given Bootstrap's adaptability and multitude of features.

For future reference, here is the original code snippet from the question:

<!DOCTYPE html>
<head>
...
</head>
<body>
<!-- irrelevant content omitted -->
<main class="container-fluid-page">
<div id="content">

   <div id="results">
   <h2>Results:</h2>
   <table class="table table-sm">
      <tbody>
         <tr>
            <th class="col-2">Status</th>
            <td class="col-10">
               <div id="results-status">
                  foo
               </div>
            </tr>
            <tr>
            ...
            ...
               // more table rows and cells
            ...
            ...
         </tbody>
      </table>
   </div> <!-- id=results -->

</div> <!-- id=content -->

</main>
</body>


Solution

The primary reason why col-* classes cannot be applied to table cells is due to a change in Bootstrap's approach from previous versions. Starting from version 4, Bootstrap utilizes display:flex and flex-basis for creating grid layouts, enabling sophisticated alignment, distribution of space, both vertically and horizontally, as well as other benefits accompanying flexbox (e.g., order).

In this scenario, my recommendation is to adhere to the native grid layout system provided by Bootstrap:

pre.pre-scrollable {
  max-height: 132px;
}

#results pre {
  overflow: auto;
  background-color: #f8f9fa;
  padding: 1rem;
  border-radius: .25rem;
  margin-bottom: 0;
}

#results>.row>[class^="col-"] {
  padding-top: 1rem;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>

<main class="container-fluid" id="results">
  <div class="row">
    <div class="col-2">
      <label>Status</label>
    </div>
    <div class="col-10" class="results-status">
      foo
    </div>
  </div>
  <div class="row">
  ...
  ... 
    // additional grid rows and cells
  ...
  ...
  </div>
</main>

This implementation ensures seamless responsiveness. Eliminating the -sm component from the column class names guarantees that the columns maintain their side-by-side presentation on narrower displays, mimicking the behavior of a table.

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

How can you determine whether the CSS of a <div> is defined in a class or an id using JavaScript/jQuery?

Hey there, I'm currently working on dynamically changing the CSS of a website. Let's say we have a div like this: <div id="fooid" class="fooclass" ></div> The div has a class "fooclass" and an ID "fooid", and we're getting its ...

Determining the active status of a class using oTree and JavaScript

Upon running the following code: <div id="1-round" class="btn-group btn-group-toggle btn-lg" data-toggle="buttons" > {% for checkbox in form.players_choice %} <label class="btn btn-primary rounded mr-3 active btn-lg 1-round" style="ma ...

Is there a way to use jQuery to verify if an LI element contains a parent LI element?

Struggling with customizing the comments section of a WordPress theme? Utilizing jQuery for styling can be tricky, especially when dealing with nested UL elements like admin comments. The challenge lies in traversing the DOM structure to target specific el ...

What is causing this issue with the basic HTML and CSS code that is preventing it from displaying correctly

What does the HTML code look like? <html> <head> <title>Homepage</title> <link rel="stylesheet" type="text/css" href="style.css" /> </head> <body> <div id="container"></div> </body> </html> ...

Creating CSS rounded corners with pseudo-elements

Take a look at this fiddle: https://jsfiddle.net/xnr7tqm1/ This is the html code I'm working with: <div class="media-container"> <iframe width="365" height="200" src="https://www.youtube.com/embed/JqjIb6FhU_k" frameborder="0" al ...

Safari and Internet Explorer 9 prioritizing min-height over height in rendering

For testing a website, I am using Safari 5.0.5 and IE9 on Windows. Within the CSS file, the following code is present: #contentarea { background-image:url('../images/img.jpg'); background-repeat: no-repeat; background-position: right to ...

What is the best way to modify React state?

Can someone help me troubleshoot an issue with toggling React state after a button click? I want to be able to change "Work From Office" to "Work From Home" and vice versa, but it's only working once. Is there a way to achieve this using an if stateme ...

Removing all table rows except one in Jquery

I currently have this code in my view: <script> $(document).ready(function() { $("#add_instruction").click(function(){ $("#instructions").append("<tr><td></td><td><input type='text' name='rec ...

Working towards ensuring my website is responsive

Hello, I am a CSS beginner currently working as an intern. My task is to make a website's CSS compatible with Internet Explorer, and then make it responsive and scalable. Essentially, the design should retain its appearance when the window size change ...

Issue with Angular 2 NgFor Pattern Error Message Display Absence

I am attempting to incorporate inputs with a regex requirement within an ngFor loop, but I am not receiving the expected error message when entering something that does not match the required pattern. Even when I input an incorrect pattern, "Test" remains ...

The form is not being submitted when I click on the submit button because I have two buttons that trigger AJAX requests

I am facing an issue with a form where there is a submit button inside it, as well as another button within the form that has attributes type=button and onclick=somefunction(). The button with the onclick function works perfectly fine, but the other button ...

Error encountered in angular.forEach syntax

Can someone help me figure out why my array sum calculation using angular.foreach is resulting in a "syntax error" message? I believe my syntax is correct, as indicated by var sum += value.fare;. Any insights on why this error occurs would be appreciated. ...

What is the process for a webpage to save modifications made by JavaScript?

I am working on a simple web page with a form that contains checkboxes representing items from a database. When the submit button is clicked, these items may be retrieved. Additionally, there is an option to add a new item at the bottom of the page. My go ...

Unfinished card or cut-off content within a bootstrap accordion

Greetings to the StackOverflow community! I am facing an issue with my accordion and card setup, where the card is not fully displayed within the button at the bottom. The following image illustrates the problem: https://i.sstatic.net/aSFsc.png Here is ...

Creating aesthetically pleasing and uniform rows of responsive Bootstrap 4 cards with consistent heights

I'm a beginner in the world of design and Bootstrap, so please be patient with me. My goal is to create a series of cards that have equal height and width (responsive, not fixed) in each row. In other words, I want all the cards to be as tall and wid ...

Issue with the left margin of the background image

I am currently facing an issue with a series of background images that are supposed to fade in and out behind my content. These images are centered, wider than the content itself, and should not affect the width of the page. However, there is an unexpected ...

Rotating Cursor during AJAX loading process

I utilize a WebGrid across many of my pages to showcase various products. Within the code snippet below, you can see how an item is added to the database when a user clicks on it: public bool ToCart(int userId, string partNumber, ...

The website takes forever to load on Internet Explorer and it keeps freezing

When attempting to access a certain website, there is a noticeable delay before the page actually loads (especially when using older versions of Internet Explorer). The issue does not occur with other web browsers. The website in question is a simple Word ...

Differentiate your borders with CSS styling of various colors

Is there a way in CSS to create multiple lines with different colored borders stacked on top of each other without using a background image? Take a look at the example below: https://i.sstatic.net/w9F44.jpg ...

chosen=chosen based on data from mysql database

Currently, I'm developing my own CMS that allows users to add and update their projects. While the adding function works smoothly, the updating process also functions properly. However, a problem arises when I click on a project (retrieving informatio ...