Carol.Kar
Carol.Kar

Reputation: 5355

Make tables in container-fluid div responsive

I have created two tables with bootstrap 4.0:

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" />
<div class="container-fluid">
  <div class="row">
    <div class="col-6">
      <div class="pr-1">
        <h2>Specifications</h2>
        <table class="table stats">
          <tbody>
            <tr>
              <th>Price :</th>
              <td class=" text-right">
                100 </td>
            </tr>
            <tr>
              <th>Manufacturer:</th>
              <td class=" text-right">
                Gigabyte </td>
            </tr>
            <tr>
              <th>Wattage:</th>
              <td class=" text-right">
                150 </td>
            </tr>
            <tr>
              <th>Product:</th>
              <td class=" text-right">
                Product 1 </td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>
    <div class="col-6">
      <div class="pr-1">
        <h2>Earning</h2>
        <table class="table stats">
          <tbody>
          </tbody>
          <thead>
            <tr>
              <th>Period</th>
              <th class="text-right">Rev</th>
              <th class="text-right">Cost</th>
              <th class="text-right">Profit</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>Hour</td>
              <td class="text-right text-info">$
                <span id="rev-hour">
                            0.022                        </span>
              </td>
              <td class="text-right text-danger">$
                <span id="cost-hour">
                            0.006                        </span>
              </td>
              <td class="text-right text-success">$
                <span id="earning-hour">
                            0.016                        </span>
              </td>
            </tr>
            <tr>
              <td>Day</td>
              <td class="text-right text-info">$
                <span id="rev-day">
                            1.34                        </span>
              </td>
              <td class="text-right text-danger">$
                <span id="cost-day">
                            0.36                        </span>
              </td>
              <td class="text-right text-success">$
                <span id="earning-day">
                            0.98                        </span>
              </td>
            </tr>
            <tr>
              <td>Week</td>
              <td class="text-right text-info">$
                <span id="rev-week">
                            9.37                        </span>
              </td>
              <td class="text-right text-danger">$
                <span id="cost-week">
                            2.52                        </span>
              </td>
              <td class="text-right text-success">$
                <span id="earning-week">
                            6.85                        </span>
              </td>
            </tr>
            <tr>
              <td>Month</td>
              <td class="text-right text-info">$
                <span id="rev-month">
                            37.48                        </span>
              </td>
              <td class="text-right text-danger">$
                <span id="cost-month">
                            10.08                        </span>
              </td>
              <td class="text-right text-success">$
                <span id="earning-month">
                            27.40                        </span>
              </td>
            </tr>
            <tr>
              <td>Year</td>
              <td class="text-right text-info">$
                <span id="rev-year">
                            449.77                        </span>
              </td>
              <td class="text-right text-danger">$
                <span id="cost-year">
                            120.96                        </span>
              </td>
              <td class="text-right text-success">$
                <span id="earning-year">
                            328.81                        </span>
              </td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</div>

When viewed on a mobile device these two tables stay on their position and do not align below each other.

Any suggestions how to make these two tables responsive?

I appreciate your replies!

Upvotes: 1

Views: 1981

Answers (1)

SeppeDev
SeppeDev

Reputation: 2302

As already stated in the comments, you need to include responsive breakpoints. Instead of using col-6, you should use col-md-6 col-sm-12. If you add this to your code it will use a column size of 12 on small devices and a size of 6 on devices with a medium size or higher.

I added the breakpoints in the snippet below.

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" />
    <div class="container-fluid">
  <div class="row">
    <div class="col-md-6 col-sm-12">
      <div class="pr-1">
        <h2>Specifications</h2>
        <table class="table stats">
          <tbody>
            <tr>
              <th>Price :</th>
              <td class=" text-right">
                100 </td>
            </tr>
            <tr>
              <th>Manufacturer:</th>
              <td class=" text-right">
                Gigabyte </td>
            </tr>
            <tr>
              <th>Wattage:</th>
              <td class=" text-right">
                150 </td>
            </tr>
            <tr>
              <th>Product:</th>
              <td class=" text-right">
                Product 1 </td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>
    <div class="col-md-6 col-sm-12">
      <div class="pr-1">
        <h2>Earning</h2>
        <table class="table stats">
          <tbody>
          </tbody>
          <thead>
            <tr>
              <th>Period</th>
              <th class="text-right">Rev</th>
              <th class="text-right">Cost</th>
              <th class="text-right">Profit</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>Hour</td>
              <td class="text-right text-info">$
                <span id="rev-hour">
                            0.022                        </span>
              </td>
              <td class="text-right text-danger">$
                <span id="cost-hour">
                            0.006                        </span>
              </td>
              <td class="text-right text-success">$
                <span id="earning-hour">
                            0.016                        </span>
              </td>
            </tr>
            <tr>
              <td>Day</td>
              <td class="text-right text-info">$
                <span id="rev-day">
                            1.34                        </span>
              </td>
              <td class="text-right text-danger">$
                <span id="cost-day">
                            0.36                        </span>
              </td>
              <td class="text-right text-success">$
                <span id="earning-day">
                            0.98                        </span>
              </td>
            </tr>
            <tr>
              <td>Week</td>
              <td class="text-right text-info">$
                <span id="rev-week">
                            9.37                        </span>
              </td>
              <td class="text-right text-danger">$
                <span id="cost-week">
                            2.52                        </span>
              </td>
              <td class="text-right text-success">$
                <span id="earning-week">
                            6.85                        </span>
              </td>
            </tr>
            <tr>
              <td>Month</td>
              <td class="text-right text-info">$
                <span id="rev-month">
                            37.48                        </span>
              </td>
              <td class="text-right text-danger">$
                <span id="cost-month">
                            10.08                        </span>
              </td>
              <td class="text-right text-success">$
                <span id="earning-month">
                            27.40                        </span>
              </td>
            </tr>
            <tr>
              <td>Year</td>
              <td class="text-right text-info">$
                <span id="rev-year">
                            449.77                        </span>
              </td>
              <td class="text-right text-danger">$
                <span id="cost-year">
                            120.96                        </span>
              </td>
              <td class="text-right text-success">$
                <span id="earning-year">
                            328.81                        </span>
              </td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</div>

Upvotes: 1

Related Questions