Table designed using the Flexbox layout model

I am looking to create a table with multiple columns that can handle dynamic data. The challenge is that using the base <table> element would cause the column width to change when the content changes and I also need the ability to hide columns using JavaScript.

As a result, I have opted to build a flexbox-based table. However, I am facing difficulties in managing column widths when hiding some of them.

For instance, consider the following table:

.table {
  display: flex;
  flex-direction: column;
  flex-wrap: nowrap;
  width: 100%;
  min-height: 200px;
}

.row {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
}

.table-head {
  color: #a4a;
  font-weight: 500;
}

.table-cell {
  padding: 10px;
  flex-grow: 1;
  width: 100%;
  overflow: hidden;
}

.table-cell:nth-child(1) {
  width: 20%;
}

.table-cell:nth-child(2) {
  width: 10%;
}

.table-cell:nth-child(3) {
  width: 30%;
}

.table-cell:nth-child(4) {
  width: 20%;
}

.table-cell:nth-child(5) {
  width: 20%;
}

In this example, I want to hide the 4th column. As a result, there will be extra space that needs to be redistributed among the remaining columns while maintaining their relative sizes. How can I achieve this? For instance, ensuring that the "Address" column is larger than the "Country" column. Additionally, if only one column remains, it should take up 100% of the width.

I attempted to use flex-grow to control the width distribution but encountered issues where the columns were not equal in size between the header row and body rows, leading to a lack of consistency in the table layout when columns are hidden.

Answer №1

Avoid using width, opt for flex:1 or 2, 3,... instead

.table {
  display: flex;
  flex-direction: column;
  flex-wrap: nowrap;
  width: 100%;
  min-height: 200px;
}

.row {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
}

.table-head {
  color: #a4a;
  font-weight: 500;
}

.table-cell {
  padding: 10px;
  flex:1;
  overflow: hidden;
}

.table-cell:nth-child(1) {
  flex:3;
}

.table-cell:nth-child(2) {
flex:3;
}

.table-cell:nth-child(3) {
flex:3;
}

.table-cell:nth-child(4) {
flex:6;
}

.table-cell:nth-child(5) {
flex:2;
}
<div class="table">
  <div class="row">
    <div class="table-cell table-head">
      Name
    </div>
    <div class="table-cell table-head">
      Phone number
    </div>
    <div class="table-cell table-head">
      Email
    </div>
    <div class="table-cell table-head">
      Address
    </div>
    <div class="table-cell table-head">
      Country
    </div>
  </div>

  <div class="row">
    <div class="table-cell">Peter</div>
    <div class="table-cell">123123</div>
    <div class="table-cell"><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="25554051405765405d44485549400b464a48">[email protected]</a></div>
    <div class="table-cell">asdsad</div>
    <div class="table-cell">empty</div>
  </div>

  <div class="row">
    <div class="table-cell">Bob</div>
    <div class="table-cell">123124</div>
    <div class="table-cell"><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bbd9d4d9fbd9d4d995d8d4d6">[email protected]</a></div>
    <div class="table-cell">sadsads</div>
    <div class="table-cell">empty</div>
  </div>
</div>

Using jQuery to hide the fourth table cell:

$(document).ready(function(){
  $('.table-cell:nth-child(4)').fadeOut(1000);
})
.table {
  display: flex;
  flex-direction: column;
  flex-wrap: nowrap;
  width: 100%;
  min-height: 200px;
}

.row {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
}

.table-head {
  color: #a4a;
  font-weight: 500;
}

.table-cell {
  padding: 10px;
  flex:1;
  overflow: hidden;
}

.table-cell:nth-child(1) {
  flex:3;
}

.table-cell:nth-child(2) {
flex:3;
}

.table-cell:nth-child(3) {
flex:3;
}

.table-cell:nth-child(4) {
flex:6;
}

.table-cell:nth-child(5) {
flex:2;
}
<div class="table">
  <div class="row">
    <div class="table-cell table-head">
      Name
    </div>
    <div class="table-cell table-head">
      Phone number
    </div>
    <div class="table-cell table-head">
      Email
    </div>
    <div class="table-cell table-head">
      Address
    </div>
    <div class="table-cell table-head">
      Country
    </div>
  </div>

  <div class="row">
    <div class="table-cell">Peter</div>
    <div class="table-cell">123123</div>
    <div class="table-cell"><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9aeaffeeffe8daffe2fbf7eaf6ffb4f9f5f7">[email protected]</a></div>
    <div class="table-cell">asdsad</div>
    <div class="table-cell">empty</div>
  </div>

  <div class="row">
    <div class="table-cell">Bob</div>
    <div class="table-cell">123124</div>
    <div class="table-cell"><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bfddd0ddffddd0dd91dcd0d2">[email protected]</a></div>
    <div class="table-cell">sadsads</div>
    <div class="table-cell">empty</div>
  </div>
</div>

<script src="https://code.jquery.com/jquery-3.3.1.js"></script>

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

What are the steps to restrict the scope of a <style> declaration in HTML?

Currently, I am in the process of creating a user interface for a webmail. Whenever I receive an email, I extract its content and place it within a <div> element for display purposes. However, one challenge that I am facing is that each email contain ...

Utilizing combined selectors in styled-components: A comprehensive guide

My existing CSS code is structured like this: .btn_1 { color: #fff; background: #004dda; display: inline-block; } .btn_1:hover { background-color: #FFC107; color: #222 !important; } .btn_1.full-width { display: block; width: 100%; } I ...

The footer is floating somewhere in the middle of the page, not at the bottom

Currently working on a webpage with the Twitter Bootstrap Framework. The footer is supposed to be at the bottom of each page with lots of content, but on one sub-page it's appearing in the middle instead: http://gyazo.com/45232ab25cdeb7705f9775a969051 ...

Error encountered during PHP GET submission

I am currently working on a PHP page where users can select a season and episode from a form, which will then be sent to a page to play the selected episodes. Below is the form: <form action="view" method="get"> <select> <option name="s" ...

Creating a function for loading dynamic content in XSLT can be achieved by following these steps

When I call the function collapseandexpand() for static elements only, it does not work. Now, how can I create the same function to handle dynamic content in xslt? Javascript Code: <script language="javascript" type="text/javascript> <xsl:text&g ...

Having difficulty adding spacing between the border and the content of a label

I've designed labels to function as buttons for radio buttons. I'm struggling to figure out why I can't add padding between the border and the background color content. Should I reconsider the structure of the radio/label elements, or is th ...

The form features a "Select all" button alongside a series of checkboxes for multiple-choice options

I am facing difficulties trying to get the "Select all" and "Remove all" buttons to function properly within my form. Whenever I remove the "[]" (array) from the name attribute of the checkboxes, the JavaScript code works perfectly fine. However, since I n ...

The background image refuses to load

I've been working on a website project using nodejs, expressjs, and pug as my template engine. I designed the home page in pure pug but ran into an issue when trying to add a background image to a specific section of the site. Despite double-checking ...

Troubleshooting a unique CSS bug in jQuery mouseover functionality

Check out this pen: https://codepen.io/anon/pen/eKzEVX?editors=1111 I recently created a Form Select in Laravel: {!! Form::select('status_id', $statuses, $post->status_id, ['class' => 'form-control post-sub-items-label &apo ...

How can I ensure text remains within a parent div and is positioned above an image?

After writing the following HTML and CSS, an issue arose when resizing the page: <div style="position: relative;"> <img width="100%" src="images/some.jpg"> <div class="header"> <h1>my header</h1> ...

Utilizing a Clear Button to Reset Specific Fields in a Form Without Clearing the Entire Form

I am currently working on a multipart form that includes 'Name', 'Email', and 'Phone Number' fields. It's important to note that the phone number field is actually composed of 10 individual single-digit fields. To enhan ...

The POST request did not yield an HttpResponse object; instead, it returned None

When I submit a selection from a dropdown form to views as a POST request, and then use this selection to query data from Django, I encounter an issue while trying to map Django models data to Highcharts following this approach. The error message "the view ...

Discover the child of the delegated event div using jQuery

Can you assist me in resolving this issue? My HTML structure appears as follows: <div id="main-randompart"> <!--Inserted into the DOM during runtime--> <div class="close"> X </div> <img src="somesrc..."> ...

Use the jQuery library to detect scrolling and toggle the CSS class between 'selected' and

I am trying to dynamically add the "selected" class to a[href] when a specific DIV comes into view while scrolling. However, I want to remove this class completely once the DIV has been scrolled past. const links = $(".cd-faq-categories li a"); // Find al ...

What is the best way to obtain a direct file link from a server URL using JavaScript?

After acquiring a file located at /home/johndoe/index.html, I am utilizing a tool like XAMPP to host, with the folder /home being hosted on localhost. The variables in play are as follows: $server_addr = "localhost"; $server_root = "/home"; $file_dir = " ...

jQuery UI Datepicker extending beyond its bounds

My Datepicker is expanding and filling the entire screen. What could be causing this issue? Here is my code: Additionally, how can I change the date format in jQuery to appear as dd-mm-yyyy? https://i.stack.imgur.com/IQzVV.png HTML: <!DOCTYPE html&g ...

Tips for automating file uploads in HTML

I am having trouble filling the <input type="file"> element programmatically when trying to upload a file using a form. Can someone please provide me with guidance on how to accomplish this task? The method does not matter, I just want to achieve m ...

Create a basic chart using JavaScript

I am looking to replicate a specific chart using JavaScript. The red point in the chart is variable. So far, I have attempted to create it using the flot library: var d3 = [[7,7]]; $.plot("#placeholder", [{ data: d3, points: { show: true } }], { ...

Looking to spice up your static HTML site? Dive into the world of Ruby on

I recently developed an app that features a static HTML webpage containing text and images, and now I'm interested in incorporating Ruby on Rails to explore its capabilities further. After creating a basic RoR application, I copied the HTML content f ...

Align Bootstrap navigation bar items at the center horizontally

I am currently working on a navigation bar that features two icons, evenly distributed. To achieve this look, I have been experimenting with scaling the icons horizontally in order to make them center-aligned within the navigation bar. However, I have not ...