Wrap columns in a table if the width overflows the container

I would like to create a responsive HTML table that wraps columns below itself if the width of the table overflows the container. It should look like this:

Default table design

And when it’s wrapped it schould look like this:

enter image description here

What is the optimal solution to this problem and can it be solved without using javascript?

The table in the default appearance is here:

table {
    width: 100%;
    border-spacing: 0;
    border-collapse: collapse;
}

table th,
table td {
    border-top: 1px solid #edf2f9;
    border-bottom: 1px solid #edf2f9;
    padding: 10px;
    text-align: left;
}

table th {
    background: #f9fbfd;
    font-size: 10pt;
    text-transform: uppercase;
    font-weight: 400;
}
<table>
  <thead>
    <tr>
      <th>First column</th>
      <th>Second column</th>
      <th>Third column</th>
      <th>Fourth column</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>First column data 1</td>
      <td>Second column data 1</td>
      <td>Third column data 1</td>
      <td>Fourth column data 1</td>
    </tr>
    <tr>
      <td>First column data 2</td>
      <td>Second column data 2</td>
      <td>Third column data 2</td>
      <td>Fourth column data 2</td>
    </tr>
    <tr>
      <td>First column data 3</td>
      <td>Second column data 3</td>
      <td>Third column data 3</td>
      <td>Fourth column data 3</td>
    </tr>
  </tbody>
</table>