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

Creating a grid layout with alternating patterns

I'm facing a challenge in creating an alternating grid layout using CSS, and it's proving to be more difficult than I anticipated. My goal is to design a layout with two boxes that alternate - first a box containing text, followed by a box displ ...

"Styling a form input with YAML and implementing a jQuery date picker field

Seeking help with a CSS question related to YAML. I've been struggling all day trying to position a jQuery date field next to a YAML CSS field, but so far I can only manage to have it display underneath the input field... Is there a way for the date ...

JS and its dynamic color changes

A game has been developed using JavaScript and HTML. The game features a table with cells that are assigned IDs for toggling colors using an onClick function. The objective is simple: when a cell is clicked, all neighboring cells, including the clicked one ...

Attempting to pass HTML content from a Laravel controller to display in data tables

Issue Description I am trying to send HTML tags to a table using editColumn but it is not working. AddColumn works fine for adding HTML code to a column, but editColumn does not display the HTML elements as expected. My question is how can I successfully ...

adjust the div's position based on the window's scrolling bars

Is there a way to make my div move down when the window scrolls down and up when the window scrolls up? How can I achieve this effect? ...

In Firefox 50.0, the button's resizing feature causes the parent div container outline to adjust size

I am currently working on developing a responsive design with the feature of having a button that appears to "float" under a div element. My desired outcome is achieved in Opera and Chrome: In Firefox, the result is not as expected: You can access my co ...

I'm having trouble getting my Retina image code to work perfectly. The high resolution image isn't loading at its full

After learning how to display my Retina optimized logo from this tutorial (link), I encountered an issue when testing on my iPad and Samsung Galaxy Note II (using Safari, Firefox, Dolphin apps). The Retina image is showing up at its full size of 1200px x 5 ...

Having difficulty grasping the concept of toggleClass and jQuery selectors

I am struggling to understand the getLiveSearchUsers function in my JS file. Could someone please help me? I don't quite grasp what selector[0] is and what toggleClass is doing here. $.post("includes/handlers/ajax_search.php", {query:value, userLogge ...

The CSS dropdown menu is malfunctioning on Internet Explorer and Firefox

After searching for a solution and coming up empty-handed, I have decided to ask a question. The drop-down menu on my computer and other devices at home works perfectly in Chrome but not in Firefox and IE. #menu { position: absolute; left: 50%; bottom ...

Issues encountered when using Vue Transition in conjunction with v-autocomplete

Attempting to conditionally display or hide a selection text field using Vue's Transition feature, but experiencing an issue where the text field appears and disappears without any transition effect. <template> <Transition v-if="is ...

Unable to attach a tooltip to a list item

As an Angular developer, I am working on a list element that displays all the cars and I am looking to add a tooltip to enhance its visual appeal. Here is what I have attempted so far: I created a span tag with the class "tooltip" to wrap around the ul el ...

Aligning divs, prevent duplicate HTML within multiple divs

Currently, I am attempting to troubleshoot a jsfiddle issue. The main problem lies in my desire for the first two divs (with class="fbox") to be aligned next to each other on the same level. Furthermore, I am looking to enable dragging the image into the ...

Issues with PHP not reflecting changes to MySQL database

I'm struggling to find a solution to the issue I'm facing on various forums. The problem involves two pages, an HTML page and a PHP page. The HTML page simply populates a dropdown list from a database column. Below is a simplified version of the ...

Upgrade the arrow accordion in bootstrap by incorporating images instead

I have implemented a Bootstrap accordion with a toggle arrow. Here is an example: http://jsfiddle.net/zessx/r6eaw/12/ My question is, if I want to change the arrow with an image, what do I need to edit? Or how can I implement it? This is the image I want ...

What causes the $_FILES superglobal to consistently appear empty?

Check out my page Take a look at my form: <h2>Upload Photo</h2> <form name="photo" enctype="multipart/form-data" action="<?php echo $_SERVER["PHP_SELF"];?>" method="post"> Photo <input type="file" name="image" size="30" /> & ...

Vue Quasar Drawer Layout with Bottom Bar

Struggling to implement a footer in my q-drawer. Below is the template and component code I am currently using: <template> <q-layout view="hHh lpR fFf"> <q-header elevated> <q-toolbar> <q-btn flat de ...

How can you achieve the effect of "hovering over an image to automatically start playing a muted video within the image itself"?

[![enter image description here][1]][1]I am working with WordPress and Elementor, and I want to create a hover effect where an image triggers a muted video to play within the image area on mouseover. The video should stop playing when the mouse is not hove ...

Tracking the movement of a handheld device through GPS technology

I am interested in creating a mobile application that can track the real-time location of users who have the app installed on their devices. The concept is to allow one or more users to follow the movement of another user using GPS on a map. I plan to deve ...

Utilizing absolute and relative positioning for setting up a flexible sidebar layout

I'm finding myself a bit puzzled when it comes to using Absolute and Relative positioning. In essence, I have a sidebar with about 4 divs in it. My goal is for div 3 to stay in the same position on every page, even if div 1 or div 2 are missing and c ...

Can HTML and CSS be used to create button text that spans two lines with different fonts?

When working with HTML attribute values, I am facing an issue where I can't get button text to display in two lines with different font sizes. I have attempted using whitespace in CSS for word wrap, but this solution does not solve my problem. I also ...