New Release: Bootstrap4 beta introduces improved table features and d-inline-block styling

I need to arrange multiple tables side by side and allow horizontal scrolling overflow if they don't fit on the page.

Here is a sample in jsfiddle: https://jsfiddle.net/c949Lspy/10/

Using Bootstrap:

<div style="white-space: nowrap;">

<table class="table table-bordered d-inline-block align-top" style=" padding: 0px;">
    <thead>
      <tr><th> Heading 1</th></tr>
    </thead>
    <tbody>

      <tr><td>Row content </td></tr>
      <tr><td>Row content </td></tr>
    </tbody>
</table>

</div>
  1. The inspector indicates that Bootstrap is setting the table width equal to the viewport's width. Why is this happening? (1st row in jsfiddle)
  2. If I manually set the width of the table, the table elements do not fill up the space properly, which looks odd. (2nd row in jsfiddle)
  3. How can I adjust the tables to exactly fill up the space given to them?

Answer №1

This is my solution. I incorporated borders to enhance clarity.

To make it more concise, I had to simplify and modify the HTML structure a bit.

Visit this link for better visualization

table {
  border: solid blue!important;
  float: left!important;
  width: 200px!important;
}
#table-container {
 border: solid red;
  height: 300px;
  width: 1000px;
  display: inline-block;
  overflow-x: scroll;
}
#big {
  border: solid green;
  height: 100%;
  display: inline-block;
  width: 10000px;
}

Explanation

I enclosed the tables within two divs. The first div determines the width at 300px and allows horizontal scrolling with an overflow property. The child div expands to accommodate all floated tables.

To calculate the width for #big (using jQuery) :

var tableWidth = 200; // obtain from any table if needed

var nrTables = $('table').length;

$('#big').css('width', (nrTables * tableWidth + 30) + 'px');

var tableWidth = 200; // retrieve from a table as per your preference

    var nrTables = $('table').length;

    $('#big').css('width', (nrTables * tableWidth + 30) + 'px');
table {
  border: solid blue!important;
  float: left!important;
  width: 200px!important;
}
#table-container {
 border: solid red;
  height: 300px;
  width: 500px;
  display: inline-block;
  overflow-x: scroll;
}
#big {
  border: solid green;
  height: 100%;
  display: inline-block;
  width: 10000px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css">
<div id="table-container" style="white-space: nowrap;">
<div id="big">

  <table class="table table-bordered d-inline-block align-top" style="background: #f7fec0; padding: 0px;">
    <thead>
      <tr>
        <th> Heading 1</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Row content </td>
      </tr>
      <tr>
        <td>Row content </td>
      </tr>
      <tr>
        <td>Row content </td>
      </tr>
    </tbody>
  </table>
  <!-- more similar tables follow -->
  
  </div></div>

Answer №2

UPDATE X2: Highlighting differences in responsive design between BS3 and BS4

To optimize your layout for both Bootstrap 3 and Bootstrap 4, I recommend utilizing the grid system and table-responsive classes. You can wrap your content in a container and consider using container-fluid for certain sections. Additionally, I have modified the styling by removing the d-inline-block align-top. Here is an overview of the changes:

<div class="container" style="white-space: nowrap;">
<div class="container-fluid">
<div class="row">
  <div class="col-md-6 col-xs-12">
  <table class="table table-bordered table-responsive" style="background: #f7fec0; padding: 0px;">
    <thead>
        <th> Heading 1</th>
    </thead>
    <tbody>
      <tr>
        <td>Row content </td>
      </tr>
      <tr>
        <td>Row content </td>
      </tr>
      <tr>
        <td>Row content </td>
      </tr>


    </tbody>
  </table>
</div>
<div class="col-md-6 col-xs-12">
  <table class="table table-bordered table-responsive" style="background: #cac1fd; padding: 0px;">
    <thead>
      <tr>
        <th> Heading 2</th>
      </tr>
    </thead>
    <tbody>

      <tr>
        <td>Row content </td>
      </tr>
      <tr>
        <td>Row content </td>
      </tr>
      <tr>
        <td>Row content </td>
      </tr>


    </tbody>
  </table>
</div>
</div>

View non-responsive display in BS4

See how responsiveness works in BS3

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 initial render of Next.js is not properly loading the CSS files

Encountering an issue with the initial load of the mobile app version; it seems that the CSS of my component covering the page is not loading correctly on the first screen load. However, when resizing to desktop and then switching back to mobile view, the ...

Please select a text color for displaying tabular data in HTML using XML and XSL as a guide

<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"> <xsl:template match="/"> <html> <head> <style type="text ...

The jQuery find and replace feature is causing my entire content to be replaced instead of just specific paragraphs

Within this section, there are multiple paragraphs and an input field. The concept is simple: the user inputs text into the box, then hits "ENTER" to trigger a jquery function below. The process involves identifying matches between the paragraph content a ...

Sometimes the last column might not be visible in an HTML table within a collapsed Bootstrap element while using Chrome browser

I've encountered a strange issue when trying to wrap an HTML table inside a collapsing div element using Bootstrap. The last column is not consistently displayed in Chrome (44.0.2403.107 m) depending on the window width, but it works fine in Firefox. ...

Transforming the pen into a creative assortment of animated social media icons for top-notch production

My goal is to incorporate animated social media icons on my website using only CSS without any JavaScript. I came across a pen called "Yet Another Set of Animated Social Icons" that I'm attempting to modify. The issue at hand is that instead of the c ...

Creating a cascading layout with React Material-UI GridList

Can the React Material-UI library's GridList component be used in a layout similar to Pinterest's cascading style? I have utilized Material-UI Card components as children for the GridList: const cards = props.items.map((item) => <Card ...

Using Selenium WebDriver to locate elements by their XPath within a WebElement

I am facing an issue with locating an element in my code using the following snippet: elements = driver.find_elements_by_xpath("//div[@class='Display']") After successfully finding the elements, I want to access the second "Display" element and ...

Tips for changing form field values using jQuery

Is there a way to use jQuery to retrieve the values of multiple checkboxes and insert them into a hidden form field? Here is how my checkboxes are structured: <form> <input type="checkbox" name="chicken" id="chicken" />Chicken <in ...

Issues with the execution of Jquery functions

My website is currently running on the latest version of Jquery, but I am experiencing two issues: Upon initially loading the site, the content slider displays correctly. However, if you navigate to another page and then return, the slider fails to load ...

Significant distance between the content and the footer section of the page

Having trouble with a large gap between the content and footer ad on my website. I've tried to troubleshoot it myself, but suspect the issue may be related to the content generated by another site (indeed.com). Check out the link here: Any assistanc ...

Renaming file names for Bootstrap 4.5 demonstrations

Trying to utilize a Bootstrap 4.5 example, but running into issues when renaming the HTML or CSS files - it breaks the formatting link for the page. Is there a way to duplicate the original example files and modify the names (such as "my_page.html" or "m ...

Troubleshooting problems with the height of nested DIV elements

Issue I am attempting to replicate the layout depicted in the image below. The black area represents the body with id="library", the grey div is id="body" (which is not visible due to lack of margins for inner divs), the light blue div is id="sideBar", th ...

Modifying dynamic input fields based on the selected input field type

Seeking advice on a challenge I'm facing while testing a website. My task is to mask input fields in screenshots after executing tests because we share data with other teams. I've tried using JS before the script by changing the input type to &ap ...

Can CSS be used to generate these shadows?

Check out this image I made showcasing a board with an elongated shadow cast behind it. The intention is to emulate the look of the board leaning against a wall, resulting in a triangular shadow on either side. I'm curious if it's achievable to ...

Having trouble implementing basic CSS on an Angular Materials component

Check out this Stackblitz example. I am attempting to add the CSS style color: blue to the element with class mat-button-toggle-label-content, however, it does not seem to be working. Interestingly, a similar CSS style is successfully being applied to a ...

Basic color scheme

I'm attempting to change the background color behind a partially transparent .png div. The CSS class that modifies the div is ".alert". Manually editing the .alert class background color works perfectly, and now I want to automate this on a 1-second c ...

Attempting to generate two columns out of a list of divs, with additional white space in the left column

I am attempting to render a series of divs into two columns. However, when viewing the example in this [JSFiddle](http://jsfiddle.net/nb32P/2/) link, I noticed there is extra spacing above Group Heading 4 in the left column that I cannot seem to remove. ...

"Enhance your Rails print design by adding custom colors with the link

I'm attempting to apply a background color to my link based on the "bin.color" property, but I am encountering difficulties and cannot remember how I achieved it previously in my application. Here is what I have tried so far: <%= link_to bin.label ...

Changing the parent div element based on the child div element

Is there a way to dynamically apply style properties, such as 'float', to the parent div based on its child div element, preferably using JavaScript without the need for additional div elements? <div class="chat-message"> <span clas ...

Creating an indentation on one side of a div using CSS for a visually appealing effect

https://i.stack.imgur.com/io6m0.jpg I'm in the process of trying to create two shapes using CSS. I've been able to almost achieve the first shape on the left, but it extends out too far. As for the second shape, I'm having difficulties cre ...