Is the table column width not aligning with the grid container width in bootstrap?

I've been grappling with trying to construct a table using Bootstrap 4 grid container. While everything from styling to alignment seems to be in place, there's one nagging issue: the table doesn't adhere to the grid container as anticipated. It appears that only the first column stretches/shrinks when the browser window is resized.

My attempt at using the display property for building the table was successful in addressing alignment challenges, but incorporating col-# as class names for the columns doesn't seem to yield the desired outcome.

.my-table {
  border: 1px solid grey;
  display: table;
  padding: 0;
}

.my-table-row-header {
  display: table-header-group;
  height: 50px;
}

.first-header-column {
  display: table-cell;
  vertical-align: middle;
  text-align: center;
}

.my-table-body {
  display: table-row-group;
}

.my-table-row {
  height: 50px;
  border-left: 1px solid grey;
  border-bottom: 1px solid grey;
  display: table-row;
}

.row-cell {
  display: table-cell;
  vertical-align: middle;
  border-right: 1px solid grey;
}

.c-column {
  display: table-cell;
  vertical-align: middle;
  text-align: center;
  padding-left: 11px;
  padding-right: 11px;
}
<div class="container-fluid no-gutters">
  <div class="my-table col-9 table">
    <div class="my-table-row-header row">
      <div class="first-header-column col-1 row-cell">
        <span class="c-column">c</span>
      </div>
      <div class="second-column col-4 row-cell">
        <span>text</span>
      </div>
      <div class="third-column col-4 row-cell">
        <span>text</span>
      </div>
    </div>
    <div class="my-table-body">
      <div class="my-table-row row">
        <div class="first-column col-1 row-cell">
          <span class="c-column">c</span>
        </div>
        <div class="second-column col-4 row-cell">
          <span>text</span>
        </div>
        <div class="third-column col-4 row-cell">
          <span>text</span>
        </div>
      </div>
      <div class="my-table-row row">
        <div class="first-column col-1 row-cell">
          <span class="c-column">c</span>
        </div>
        <div class="second-column col-4 row-cell">
          <span>text</span>
        </div>
        <div class="third-column col-4 row-cell">
          <span>text</span>
        </div>
      </div>
    </div>
  </div>
</div>

Jsfiddle: https://jsfiddle.net/4zn5oby6/2/

If you resize your browser, you'll notice the first column adjusting accordingly. I'm hopeful it will strictly adhere to the size constraints of the grid container.

P.S. Given that my table comprises other components beyond text, I prefer retaining the display properties for ensuring precise styling. Your assistance would be greatly appreciated. Thank you.

Answer №1

I'm a bit unsure about the exact question, but one thing I did notice is that the col-9 needed to be in its own div. To make it equal 12 columns, add the col-3 div afterwards.

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<style>
.my-table {
  border: 1px solid grey;
  display: table;
  padding: 0;
}

.my-table-row-header {
  display: table-header-group;
  height: 50px;
}

.first-header-column {
  display: table-cell;
  vertical-align: middle;
  text-align: center;
}

.my-table-body {
  display: table-row-group;
}

.my-table-row {
  height: 50px;
  border-left: 1px solid grey;
  border-bottom: 1px solid grey;
  display: table-row;
}

.row-cell {
  display: table-cell;
  vertical-align: middle;
  border-right: 1px solid grey;
}

.c-column {
  display: table-cell;
  vertical-align: middle;
  text-align: center;
  padding-left: 11px;
  padding-right: 11px;
}
</style>
</head>

<body>
<div class="container-fluid no-gutters">
<div class="col-9">
  <div class="my-table  table">
    <div class="my-table-row-header row">
      <div class="first-header-column col-1 row-cell">
        <span class="c-column">c</span>
      </div>
      <div class="second-column col-4 row-cell">
        <span>text</span>
      </div>
      <div class="third-column col-4 row-cell">
        <span>text</span>
      </div>
    </div>
    <div class="my-table-body">
      <div class="my-table-row row">
        <div class="first-column col-1 row-cell">
          <span class="c-column">c</span>
        </div>
        <div class="second-column col-4 row-cell">
          <span>text</span>
        </div>
        <div class="third-column col-4 row-cell">
          <span>text</span>
        </div>
      </div>
      <div class="my-table-row row">
        <div class="first-column col-1 row-cell">
          <span class="c-column">c</span>
        </div>
        <div class="second-column col-4 row-cell">
          <span>text</span>
        </div>
        <div class="third-column col-4 row-cell">
          <span>text</span>
        </div>
      </div>
    </div>
</div></div>
<div class="col-3"></div>
</div>
</body>
</html>

Answer №2

I discovered a clever workaround. Instead of relying on display: table-cell with all the issues related to vertical-align and text-align, I opted for display: flex. Surprisingly, this approach still adhered to the bootstrap grid column width (even though I'm not entirely sure why).

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

I am encountering challenges in ensuring that my code is adaptable to smaller screen sizes

https://i.sstatic.net/5WtRR.jpg https://i.sstatic.net/1eV0M.png I'm working on ensuring that my code is responsive, so that the layout adapts well to smaller screens, maintaining the same side-by-side display of times and days as on larger screens. ...

QWebEngineView - handling content larger than 2 megabytes

While working with PyQt5's QWebEngineView, I encountered a 2 MB size limitation when using the .setHTML and .setContent methods. After researching potential solutions, I came across two approaches: One option is to use SimpleHTTPServer to serve the f ...

Tips on adjusting section height as window size changes?

Working on a website that is structured into sections. Initially, all links are aligned perfectly upon load. However, resizing the window causes misalignment issues. Check out my progress at: karenrubkiewicz.com/karenportfolio1 Appreciate any help! CSS: ...

Ensure that scripts with the type of "module" are completed before proceeding to execute the following script

In my HTML document, I have two script tags with type="module" at the bottom of the body tag. Following those, there is another script tag with embedded code that relies on the results of the first two scripts. Is there a method to ensure that this code ...

The alignment of list bullets is inconsistent across different web browsers

Check out this code snippet on JSFiddle: http://jsfiddle.net/PZgn8/8/. It's interesting to see how the rendering differs between Chrome 27 and Firefox 21. Despite the difference, I believe Firefox is actually displaying the desired outcome. <ul st ...

Tips for setting up a grid where boxes have a consistent width but varying heights to maximize space utilization

Seeking design inspiration from Pinterest, where fixed width columns are used but each grid item fills all available space with varying heights. Check out this example: https://i.sstatic.net/i2WDo.jpg Initially considered using flexbox for this layout, b ...

Bootstrap - Custom CSS styles applied to specific grid prefixes

I am currently working on developing a mobile site using bootstrap and I have a need to apply different CSS rules based on the grid prefix: col-xs, col-sm Here is a snippet of the HTML code: <header class="col-xs col-sm"> Hi </header> For ...

Browsing a nearby document in JavaScript

I have a text file named plaintext.txt that is stored locally, and I'm attempting to read it in JavaScript from the same folder where my script is located. In my HTML, I include the following: <script id="plaintext" src="plaintext.txt" type="text ...

I'm looking to replicate this navigation layout using Vuetify's 'navigation drawer' component. How can I go about doing this?

I'm currently utilizing Vuetify and implementing the Navigation Drawer component. I am attempting to replicate a similar navigation layout found here. The fixed menu on the left should maintain its width even when resizing the browser window. Upon ...

How to create space between elements in CSS without using margin or padding?

Recently, I stumbled upon a fascinating website while working on my own CSS grid project: I am curious to know how it was achieved. The Portfolio elements on the website have space between them without any visible margins or paddings that I could identify ...

Can you explain the differences between offsetHeight, clientHeight, and scrollHeight for me?

Have you ever wondered about the distinction between offsetHeight, clientHeight, and scrollHeight? What about offsetWidth, clientWidth, and scrollWidth? Understanding these differences is crucial for working effectively on the client side. Without this kn ...

jquery allows for creating a list with customizable options that can change dynamically

I am looking to create a list with attachments and have the responding options displayed in a content box when a user clicks on the buttons. Afterwards, I plan to save the content box as an HTML file into my database. Do jQuery plugins support this func ...

Eliminating "www" from the domain name leads to a separate website

I am new to this. The lack of support from mediaTemple and my current hosting provider has been frustrating, so I am turning to stackOverflow for help. My issue is that entering "www" before the domain name or leaving it out leads to different servers. ...

The CSS property that controls the background color of an element

This syntax for a CSS background property is quite effective: .my-element { background: linear-gradient(rgba(131,131,131,0.8), rgba(98,98,98,0.8)), url('img/background.jpg') } However, I am not looking for a gr ...

The Bootstrap navbar stubbornly refuses to hide after being clicked on

Is there a way to adjust the data-offset-top for mobile view? Additionally, I am having trouble hiding the menu when clicking on a link. I have tried some code from stackoverflow without success. Can someone please assist with this issue: <nav class= ...

Place the application bar at the bottom of the screen

https://i.sstatic.net/0B0Ad.png I am working on a project aimed at monitoring employees, and I have developed a user interface that allows for modifications within the site. However, there is an issue with the "bottom App Bar" section at the bottom of thi ...

JavaScript utilizing an API to retrieve specific data values

Below is some API Data that I have: [ { "symbol": "AAPL", "name": "Apple Inc.", "price": 144.98, "changesPercentage": -1.22, "change": -1.79, ...

Retrieving information from mongoDB and showcasing it on an HTML page

I need some help with fetching data from my MongoDB database and displaying it on an HTML page. I have already set up the server.js file for the data retrieval. Here is the content of my server.js file: const path = require('path'); const expre ...

What is the best way to display multiple divs in a single location?

I am facing an issue with displaying different text on mouseover for three buttons. I successfully implemented it for one button, but when I added the other two, it stopped working. Here is the code I used for the first button: Using jQuery- document.get ...

What is the best way to use a CSS selector to target multiple divs such as block-11, block-12, and so

Here are several block IDs I have: <div id="block-11">content Here</div> <div id="block-12">content Here</div> <div id="block-13">content Here</div> <div id="block-14">conten ...