Utilizing Bootstrap's table-responsive feature to smoothly navigate through a table with overflow

I am working on a layout that involves:

  • A flex layout with a nav on the left side taking up 15% of the view width.
  • A main section occupying the rest of the horizontal space.

In one particular view, I have a table within the main section that exceeds the available space. My goal is to scroll the table horizontally without affecting the entire page. I tried wrapping the table with table-responsive using Bootstrap, but this solution added a scrollbar for the entire page instead of just the table or main content area as desired. Even though table-responsive seems to offer the functionality I need, it's not behaving as expected in my case and I suspect I might be overlooking something.

div.main {
  display: flex;
  min-height: 100%;
}

div.main>nav {
  background-color: coral;
  flex: 0 0 15vw;
}

div.main>main {
  flex: auto;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<div class="main">
  <nav>
  </nav>
  <main>
    <div class="table-responsive">
      <table class="table table-striped table-sm">
        <thead>
          <tr>
            <th></th>
            <th>Header Name</th>
            <th>Header Name</th>
            <th>Header Name</th>
            ...
          </tr>
        </thead>
        <tbody>
          <tr>
            <th>Column Name</th>
            <td>value</td>
            <td>value</td>
            ...
          </tr>
          ...
        </tbody>
      </table>
    </div>
  </main>
</div>

Answer №1

It appears that the reason for this behavior is due to the fact that the table-responsive class does not properly support the use of display: flex on its children elements (specifically the <main> in your situation) because the children with the d-flex class lack a specified width, causing the <main> element to stretch to the full available width.

To address this issue, there are two potential solutions. Either remove the <main> wrapper altogether or assign a specific width to the <main> element (refer to the code snippet provided). Given that your <nav> element is set to 15vw, applying a min-width: 85%, max-width: 85%, or even width: 85% to the <main> class should yield the desired outcome.

I personally recommend opting for the first approach as I am uncertain whether this behavior is intended by default in Bootstrap or if it may be considered a bug.

div.main {
      display: flex;
      min-height: 100%;
    }

    div.main>nav {
      background-color: coral;
      flex: 0 0 15vw;
    }

    div.main>main {
      min-width: 85%;
      flex: auto;
    }
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<div class="main">
  <nav>
  </nav>
  <main>
    <div class="table-responsive">
      <table class="table table-striped table-sm">
        <thead>
          <tr>
            <th></th>
            <th>Header Name</th>
            <th>Header Name</th>
            ...
          </tr>
        </thead>
        <tbody>
          <tr>
            <th>Column Name</th>
            <td>value</td>
            <td>value</td>
            ...
          </tr>
          ...
        </tbody>
      </table>
    </div>
  </main>
</div>

Answer №2

To avoid the need for a full-page scrollbar, consider enclosing your table within:

<div class="overflow-hidden">
</div>

This will retain the bootstrap-table scrollbar and maintain the intended appearance. (This solution worked for me) The use of table-responsive is unnecessary in this scenario.


As I am unable to comment on the selected answer directly, I will share my thoughts here:

The use of display: flex

removes the bootstrap-table horizontal scrollbar but retains the full-page scrollbar. This setup may lead to difficulties in navigating the page vertically as one would have to scroll all the way to the right to access the vertical scrollbar. Additionally, accessing buttons, search functions, and column selection tools could become more challenging.

Answer №3

By simply adding the .table-responsive class to the .col-md-12 div, I was able to make it work perfectly.

Previous Code:

<div class="row">
    <div class="col-md-12">
        <div class="table-responsive">
                ....
         </div>
    </div>
</div>

Updated Code:

<div class="row">
    <div class="col-md-12 table-responsive">
         .....
    </div>
</div>

The additional width adjustment made the table responsive thanks to the Bootstrap's .table-responsive class.

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 button contains a clickable link inside it, however the button itself is not clickable

I am encountering an issue with a button that contains a link. Whenever I hover over the button, the cursor changes as expected, but clicking on the button itself does not trigger any action. Instead, I have to click on the link inside the button to redi ...

I am unable to retrieve images using the querySelector method

Trying to target all images using JavaScript, here is the code: HTML : <div class="container"> <img src="Coca.jpg" class="imgg"> <img src="Water.jpg" class="imgg"> <img src="Tree.jpg" class="imgg"> <img src="Alien.jpg" class=" ...

"Incorporate a placeholder into Bootstrap's selectpicker for enhanced user

I have been working on a bootstrap selectpicker and I am trying to add a placeholder text inside the search input area that says Search here. However, my current methods do not seem to be effective. I attempted to use this script, but it did not produce t ...

After deployment, certain formatting elements appear to be skewed in the React application

After developing a React app with create-react-app, everything was working perfectly. However, upon deployment, I noticed that some styles weren't showing up as expected. The majority of the styling from Material UI is intact, but there are a few dis ...

Having divs stacked on top of each other with one positioned fixed and the other

In my HTML document, there is a main div element called container that contains several child elements also in the form of divs. One specific child div, named top_bar, has its positioning set to fixed, while the rest of the child divs have relative posit ...

Create a list item that includes an image with a corresponding label positioned beneath it

Currently attempting to convert my design into HTML and CSS for the first time, I've hit a roadblock with this particular task: I'm trying to create a series of white boxes, with each item having a white border. However, I'm struggling to c ...

"Implementing jQuery toggle and addClass functions in your

How do I modify the class within the same action as a toggle effect, which works independently very well? (I have multiple blocks with the same classes) $(".w_content").hide(); $(".w_target").click(function() { $(this).parent().next('.w_content&apos ...

The CSS styles are not being applied to the PHP code

I seem to have a tangled mess of CSS / PHP / HTML to deal with. The issue I'm facing is that something works on one PHP script but not the other. Here's a snippet of my code: <?php if ($daten->getData_DB_User($get_page_num) != fa ...

Problem encountered while executing RSpec within the container in Jenkinsfile

I'm currently working on creating a Jenkinsfile with the following requirements: Builds the image of a RoR project from a Dockerfile Checks if there is a database; if not, it creates and migrates it Conducts RSpec tests and removes the containers Las ...

Designing a div with a proportional size amidst elements of fixed dimensions

I need help figuring out how to create a dynamically sized div based on the browser's height. I'm not sure where to start or how to approach this problem. The layout I have in mind includes a 50px high header, followed by the dynamic div and the ...

Spread out the menu horizontally across the entire page

Hey everyone, I'm having an issue with my menu: https://i.sstatic.net/kPcP3.png I'm trying to make the menu fill horizontally across the entire page, but it's only filling part of it. Could it be because I'm using the wrong bootstrap ...

What causes the CSS class and jQuery to become unresponsive when rapidly moving the cursor over a specific selector while utilizing .hover(), .addClass() and .removeClass()?

After creating a customized h1 element, I implemented a feature where a class is added and removed from it with a smooth transition of 300ms when the mouse hovers over and off its parent DIV #logo. This was achieved using jQuery's .hover() method alon ...

Show Data on the Right-hand Side

Objective: Arrange the component names in two columns - left and right, with different objects like input textboxes based on a browser width of 981px. The desired layout is showcased in this link "https://jsfiddle.net/2w9kskr2/". Challenge: After impl ...

The width of a CSS border determines its height, not its width

When I try to use border-width: 100px; to set the horizontal width to 100px, it ends up setting the height to 100px instead. Any help would be greatly appreciated. .border-top-green { border-top: 1px solid #4C9962; border-width: 100px; padding-t ...

Choose and adjust the size of images with a specific aspect ratio using jQuery

As I work on my Tumblr blog, I am experimenting with using jQuery to target photo entries (imgs) based on specific aspect ratios and resizing them accordingly. To be more precise: If an image's width exceeds 250px, it should be resized so that the h ...

What is the best way to customize the appearance of Image tags located inside SVGs, whether they are in separate files or embedded within the code

I am developing a custom SVG editor application and facing an issue with implementing a highlighted effect when an <image> element is clicked. I have successfully accomplished this for other elements like <circle> by adjusting attributes such a ...

How to make the first item in a bootstrap 4 nav-pills full width even when justified

I'm running into an issue with the display of nav-pills on mobile devices when I set it to justified. Specifically, when viewing on an iPhone, the tabs are justified but the "It NavTab" pill appears on two lines. Is there a way to have the first nav-p ...

Equalizing column heights in Bootstrap layouts

It seems like a straightforward issue, but I'm having trouble finding a solution. I am utilizing Bootstrap5 for this project. You can find the complete code here : Due to some problems with Codeply, I have also uploaded it on jsfiddle. https://jsf ...

Retrieving a page using jQuery

Below is the JavaScript code that I am using: $.ajax({ url: "test.html", error: function(){ //handle error }, success: function(){ //perform actions here with the received data } }); After retrieving test.html, I am wo ...

The text on a page automatically enlarges when the browser is in full-screen mode, but I no longer wish for this to happen

Recently, I encountered an issue while trying to adjust the fonts on my html page according to the screen size. Initially, I used body{ font-size:1vmax; } However, even after deciding not to use this code anymore, the font continued to change based on ...