What are some methods to ensure tables adhere to the grid in Bootstrap 3?

Even with col-xs-X classes defined, Bootstrap tables do not adhere to the grid layout system.

A simple example demonstrates how the table (highlighted in red) does not align properly with the grid (gray): http://jsfiddle.net/fm65v3e0/

.col-xs-1 {
  background-color: #eee;
  border: 1px solid #999;
}

th {
  background-color: #fee;
  border: 1px solid red;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
  <div class="row">
    <div class="col-xs-1">col</div><div class="col-xs-1">col</div><div class="col-xs-1">col</div><div class="col-xs-1">col</div><div class="col-xs-1">col</div><div class="col-xs-1">col</div><div class="col-xs-1">col</div><div class="col-xs-1">col</div><div class="col-xs-1">col</div><div class="col-xs-1">col</div><div class="col-xs-1">col</div><div class="col-xs-1">col</div>
  </div>
  <div class="row">
    <div class="col-xs-12">
      <div class="table-responsive">
        <table class="table">
          <tr>
             <th class="col-xs-5">
               5-col header
             </th>
             <th class="col-xs-3">
               3-col header
             </th>
             <th class="col-xs-4">
               4-col header
             </th>
          </tr>
        </table>
      </div>
    </div>
  </div>
</div>

The padding around columns causes the misalignment of the table within the grid. Removing column padding will fix the alignment issue but may affect the spacing between text in columns.

Furthermore, Bootstrap tables default to 100% width and distribute any excess space when not all 12 grid columns are utilized.

Given that my site primarily consists of tabular data, it is crucial for my tables to conform to the grid system. Is there an easy solution to this problem? Should I prioritize resolving this issue or is it insignificant?

Answer №1

Your bootstrap CSS applies padding-right: 15px and padding-left: 15px to all col-xx-xx elements, causing alignment issues.

To fix this, you can remove or override the padding. Check out the difference in alignment in this image once the padding is removed:
https://i.sstatic.net/htoEg.png

An effective way to override this issue is by creating a class named no-padding:

.no-padding {
     padding-left: 0;
     padding-right: 0;
}

You can then apply this class to specific elements where you don't want padding applied. See the updated demo below:

.col-xs-1 {
  background-color: #eee;
  border: 1px solid #999;
}

th {
  background-color: #fee;
  border: 1px solid red;
}
.no-padding {
  padding-left: 0 !important;
  padding-right: 0 !important;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
  <div class="row">
    <div class="col-xs-1">col</div><div class="col-xs-1">col</div><div class="col-xs-1">col</div><div class="col-xs-1">col</div><div class="col-xs-1">col</div><div class="col-xs-1">col</div><div class="col-xs-1">col</div><div class="col-xs-1">col</div><div class="col-xs-1">col</div><div class="col-xs-1">col</div><div class="col-xs-1">col</div><div class="col-xs-1">col</div>
  </div>
  <div class="row">
    <div class="col-xs-12 no-padding"> <!-- Add it Here -->
      <div class="table-responsive">
        <table class="table">
          <tr>
             <th class="col-xs-5">
               5-col header
             </th>
             <th class="col-xs-3">
               3-col header
             </th>
             <th class="col-xs-4">
               4-col header
             </th>
          </tr>
        </table>
      </div>
    </div>
  </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

Invoking an HTML popup within a JavaScript function

Here is a question. In my project, I have the following JavaScript function: <script type="text/javascript" language="javascript"> $(document).ready(function(){ PopUpHide(); }); function PopUpShow(popup_title, pk_post_add){ document.getEl ...

Ensure that the radio button is set to its default option when the user accesses the page

I have a model with a 'PartActionType' enum which includes Transfer, Harvest, and Dispose options. public enum PartActionType { Transfer, Harvest, Dispose } On my view page, I am displaying these options using ...

Deleting a <div> element solely by its unique identifier in plain HTML text without any tags can be achieved in the following way

After my previous question was closed, I realized that the provided answer did not meet my needs. I am actually looking to hide not just the HTML text Received, but the entire row containing 0 <3 Received. A simple display: none; would suffice for this ...

Iterating through data to showcase vertical columns for a specific time span of 3 years, highlighting only the months with available data

If data has been available for every month over the past 3 years, I need it to be displayed in a specific format. You can refer to this example fiddle for reference: https://jsfiddle.net/bthorn/ncqn0jwy/ However, there are times when certain months may no ...

What is the reason behind the fact that using margin: auto 0px; does not result in vertical centering of a block, whereas margin: 0px auto;

To effortlessly set the width to auto, use margin:0px, auto #container{ background-color: blue; height:600px; width:600px; margin:0px auto;} <div id="container"></div> However, why is it not possible to set the height to auto with margin:au ...

What is the best way to position a website in the center in the provided example?

Simple question here, couldn't find it in the search so sorry if it's a repeat. How is the content on this responsive website centered? I've checked the body margins but can't seem to locate anything. Thank you for your help. ...

Creating vibrant row displays in Vue.js: A guide to styling each row uniquely

I am not a front-end developer, so Vue and JS are new concepts for me. Currently, I am working on an application for managing sales. One of the components in my project involves displaying invoices in a list format. To make the rows visually appealing, I ...

What is the best way to remove a border piece with CSS?

Currently, I'm attempting to achieve a matrix effect purely through HTML and CSS. One method I have come across involves applying a solid border and then removing certain parts at the top and bottom. Does anyone know if it's possible to create th ...

I possess a primary menu with several submenus, yet I am encountering difficulty accessing the submenus. My goal is to efficiently navigate and access the appropriate submenu within the main menu

I am facing an issue with my CSS where the sub menu is currently showing from the left side, but I would like it to slide up and down instead. .outer { width: 100%; text-align: center; background-color: Gray; padding-top: 20px; bord ...

Struggling to vertically align divs within a parent div despite only using 3 lines of code

This question builds upon an answer provided in response to a similar query. It aims to explore the specific technique outlined in "Vertical(ly) align(ing) anything with just 3 lines of CSS", especially since I'm encountering difficulties making it wo ...

Animate the rotation of specific paths within the SVG

I've designed an SVG using Illustrator and I have a specific animation in mind for it. My goal is to create an animation where the upper part of the circle line rotates from left to right (considering that there are 4 circle lines, with number one st ...

Incorporate a CSS framework into the Angular vendor bundle

My current situation : The website is built with Angular 4 Started using Angular Starter Kit Utilizing Semantic UI as the CSS framework The challenge I'm facing : Integration of Semantic UI into webpack is not having any impact. Steps I've ...

Unexpected display issues with CSS shapes in Bootstrap framework

Been experimenting with creating custom shapes in Bootstrap - they display correctly in HTML and CSS, but Bootstrap distorts them. It seems like there are no conflicting classes. Could it be necessary to use '!important' on some of the elements? ...

Strategies for successfully passing and showcasing the result of a calculation on a fresh view or component within Angular

I have developed a basic calculator application with two main components: Calculator and Result. The Angular router is used to navigate between these components. Now, I am looking for a way to dynamically display the calculation result from the Calculator ...

Exploring the world of mathematics using JavaScript, adjusting the range input, and applying CSS transform with scale()

In my project, I have a container div set to a width of 794px and a range input with a maximum value of 250 and a minimum of 0. I am using CSS transform property like this: scale(1), where the scale is equivalent to 794px. The challenge arises when the br ...

The Hamburger Menu in the Bootstrap Navbar is failing to collapse

My website uses Bootstrap v5.3 and I've added a navbar, but the hamburger icon is not collapsing on mobile devices when clicked. Can someone please review my code to see if there are any issues? I can't figure out what's wrong. <!DOCTY ...

Customizing the DatePicker with a unique button in material-ui

For my current project, I am utilizing a Datepicker component. I am looking to incorporate a custom information button in the upper right corner of the calendar layout, similar to the example image provided below: https://i.stack.imgur.com/fHMbn.png Unfo ...

SVG Filter: The image is not completely covered by a width and height of 100%

Here's a basic SVG Filter. Click the example below to toggle the appearance of the filter: var image = document.querySelector('image'); var url = 'https://i.picsum.photos/id/202/2 ...

Tips for preventing a column from extending beyond the edge of the browser window during resizing

Issue arises when I resize the browser window, causing the left box to extend beyond the browser boundary, making it impossible to see the contents even with the scrollbar below. <!DOCTYPE html> <html lang="en"> <head> ...

Adjust the image's width to match the width of the browser

I am seeking assistance on how to resize an image to fit the width of the browser. Specifically, the image I want to resize is my header image and I want it to span the entire screen width. Additionally, I need to overlay a div on top of the image. Curren ...