skip to Main Content

In my previous question, I asked about keeping 4 columns in view, even if there are more than 4 columns.

@G-Cyrillus provided a solution using the cqw unit, which works well. However, I face an issue when I have only two columns. I want these two columns to each occupy 25% of the width.

I tried using max-width with values of 25%, 25cqw, 200px, and also experimented with width: max(...) and width: min(...), but none of these approaches worked.

Is it possible to achieve this?

<div class="table-container">
  <table>
    <thead>
      <tr>
        <th>Column 1</th>
        <th>Column 2</th>
        <!-- <th>Column 3</th>
        <th>Column 4</th>
        <th>Column 5</th>
        <th>Column 6</th>
        <th>Column 7</th>
        <th>Column 8</th> -->
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Data 1</td>
        <td>Data 2</td>
        <!-- <td>Data 3</td>
        <td>Data 4</td>
        <td>Data 5</td>
        <td>Data 6</td>
        <td>Data 7</td>
        <td>Data 8</td> -->
      </tr>
      <!-- More rows as needed -->
    </tbody>
  </table>
</div>
* {
  box-sizing: border-box;
  /* Include border and padding in size calculations */
}

.table-container {
  max-width: 80vw;
  overflow: auto;
  container-type: inline-size;  /* Make it a container that you can query its size */
  border: solid;
  margin: auto;
}

table {
  width: 100%;
  table-layout: fixed;
  border-collapse: collapse;
}

th,
td {
  width: 25cqw;
  /* Each column occupies 25cqw (25% of the container's width) */
  border: 1px solid #ddd;
  padding: 8px;
  text-align: left;
}

2

Answers


  1. You could try using display grid:

    * {
      box-sizing: border-box;
      /* Include border and padding in size calculations */
    }
    
    .table-container {
      max-width: 80vw;
      overflow: auto;
      border: solid;
      margin: auto;
      display: grid;
      grid-auto-columns: 25%;
      grid-auto-flow: column;
      overflow: auto;
    }
    
    .inner-cell {
      background-color: red;
      border-right: 2px solid black;
    }
    
    .header{
      border-bottom: 2px solid black;
    }
    <div class="table-container">
      <div class="inner-cell">
        <div class="header">Header</div>
        <div class="data">Data</div>
      </div>
      <div class="inner-cell">
        <div class="header">Header</div>
        <div class="data">Data</div>
      </div>
      <!--<div class="inner-cell">
        <div class="header">Header</div>
        <div class="data">Data</div>
      </div>
      <div class="inner-cell">
        <div class="header">Header</div>
        <div class="data">Data</div>
      </div>
      <div class="inner-cell">
        <div class="header">Header</div>
        <div class="data">Data</div>
      </div>
      <div class="inner-cell">
        <div class="header">Header</div>
        <div class="data">Data</div>
      </div>-->
    </div>
    Login or Signup to reply.
  2. Try this one for each column occupies 25%

    HTML Code

    <div class="table-container">
      <table>
        <thead>
          <tr>
            <th>Column 1</th>
            <th>Column 2</th>
            <th>Column 3</th>
            <th>Column 4</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>Data 1</td>
            <td>Data 2</td>
            <td>Data 3</td>
            <td>Data 4</td>
          </tr>
        </tbody>
      </table>
    </div>
    

    CSS Code

    <style>
    * {
      box-sizing: border-box;
      /* Include border and padding in size calculations */
    }
    
    .table-container {
      max-width: 80vw;
      overflow: auto;
      container-type: inline-size;  /* Make it a container that you can query its size */
      border: solid;
      margin: auto;
    }
    
    table {
      width: 100%;
      table-layout: fixed;
      border-collapse: collapse;
    }
    
    th,
    td {
      width: 25%;
      /* Each column occupies 25% of the table's width */
      border: 1px solid #ddd;
      padding: 8px;
      text-align: left;
    }
    </style>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search