3

here is example of my code:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

<div class="row">
  <div class="col-6">
    <div class="card mt-4">
      <div class="card-header">
        <h5 class="text-center">Comments</h5>
      </div>
      <div class="card-body">
        <table class="table table-primary">
          <thead>
            <tr>
              <th colspan="6">
                <div class="pull-left">
                  <button type="button" class="btn btn-outline-secondary btn-sm"><i class="fa fa-plus-circle" aria-hidden="true"></i> Add New</button>
                </div>
                <div class="pull-right">
                  <button type="button" class="btn btn-outline-secondary btn-sm"><i class="fa fa-list" aria-hidden="true"></i> View All</button>
                </div>
              </th>
            </tr>
            <tr>
              <th>Subject</th>
              <th>Author</th>
              <th>Date</th>
              <th>Year</th>
              <th>Edit</th>
              <th>Delete</th>
            </tr>
          </thead>
          <tbody>
            <tr>

              <td>This is a test comment</td>
              <td>John Wick</td>
              <td>06/28/2019</td>
              <td>2019</td>
              <td class="text-center">
                <button type="button" class="btn btn-outline-secondary btn-sm comment-edit">Edit</button>
              </td>
              <td class="text-center">
                <button type="button" class="btn btn-outline-secondary btn-sm comment-delete">Delete</button>
              </td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</div>

As you can see in my example table overflows the card body. Is there a way to fit the table inside of the card body? Thank you.

espresso_coffee
  • 5,980
  • 11
  • 83
  • 193

3 Answers3

4

Bootstrap has a class .table-responsive that you can use to have a responsive table. It will add a horizontal scroll if needed.

<div class="table-responsive">
    <table class="table">
    ...
    </table>
</div>

Read more: DOCUMENTATION


Sidenote - use the latest release of bootstrap and jQuery because older versions may have bugs.

Jakub Muda
  • 6,008
  • 10
  • 37
  • 56
4

Try this solution and you can edit accordingly,

   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
    
    <div style="max-width:400px">
    <div class="row">
      <div class="col-12">
        <div class="card mt-4">
          <div class="card-header">
            <h5 class="text-center">Comments</h5>
          </div>
          <div class="card-body">
            <table class="table table-primary table-responsive">
              <thead>
                <tr>
                  <th colspan="6">
                    <div class="pull-left">
                      <button type="button" class="btn btn-outline-secondary btn-sm"><i class="fa fa-plus-circle" aria-hidden="true"></i> Add New</button>
                    </div>
                    <div class="pull-right">
                      <button type="button" class="btn btn-outline-secondary btn-sm"><i class="fa fa-list" aria-hidden="true"></i> View All</button>
                    </div>
                  </th>
                </tr>
                <tr>
                  <th>Subject</th>
                  <th>Author</th>
                  <th>Date</th>
                  <th>Year</th>
                  <th>Edit</th>
                  <th>Delete</th>
                </tr>
              </thead>
              <tbody>
                <tr>
    
                  <td>This is a test comment</td>
                  <td>John Wick</td>
                  <td>06/28/2019</td>
                  <td>2019</td>
                  <td class="text-center">
                    <button type="button" class="btn btn-outline-secondary btn-sm comment-edit">Edit</button>
                  </td>
                  <td class="text-center">
                    <button type="button" class="btn btn-outline-secondary btn-sm comment-delete">Delete</button>
                  </td>
                </tr>
              </tbody>
            </table>
          </div>
        </div>
      </div>
    </div>
      </div>
Charlene Vas
  • 723
  • 1
  • 9
  • 21
Navneet Kumar
  • 623
  • 1
  • 6
  • 16
3

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
<body>

<div class="row">
  <div class="col-6">
    <div class="card mt-4">
      <div class="card-header">
        <h5 class="text-center">Comments</h5>
      </div>
      <div class="card-body">
        <table class="table-responsive table-primary">
          <thead>
            <tr>
              <th colspan="6">
                <div class="pull-left">
                  <button type="button" class="btn btn-outline-secondary btn-sm"><i class="fa fa-plus-circle" aria-hidden="true"></i> Add New</button>
                </div>
                <div class="pull-right">
                  <button type="button" class="btn btn-outline-secondary btn-sm"><i class="fa fa-list" aria-hidden="true"></i> View All</button>
                </div>
              </th>
            </tr>
            <tr>
              <th>Subject</th>
              <th>Author</th>
              <th>Date</th>
              <th>Year</th>
              <th>Edit</th>
              <th>Delete</th>
            </tr>
          </thead>
          <tbody>
            <tr>

              <td>This is a test comment</td>
              <td>John Wick</td>
              <td>06/28/2019</td>
              <td>2019</td>
              <td class="text-center">
                <button type="button" class="btn btn-outline-secondary btn-sm comment-edit">Edit</button>
              </td>
              <td class="text-center">
                <button type="button" class="btn btn-outline-secondary btn-sm comment-delete">Delete</button>
              </td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</div>
</body>
</html>

You can use .table-responsive instead of using .table on your table

nazifa rashid
  • 1,469
  • 1
  • 9
  • 20
  • 1
    This is WRONG. You can't change `.table` to `.table-responsive`. You must add `.table-responsive` to the parent element. – Jakub Muda Sep 03 '19 at 18:43
  • 1
    This is not wrong .table has only the following properties which are not hard and fast to be used. .table { width: 100%; margin-bottom: 1rem; background-color: transparent; } This code will not make the major changes, btw thank you for the comment. – Navneet Kumar Sep 04 '19 at 04:51