Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Bootstrap Table

Tags:

html

css

How to restrict width to each column of bootstrap table? I tried to play around but I cant seem to shrink the column.

https://plnkr.co/edit/XhwIUynVgAxMBpJo8x8N?p=preview

<!DOCTYPE html>
<html>

  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link data-require="[email protected]" data-semver="3.2.0" rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" />
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
  </head>

  <body>
    <div class="container">
      <h2>Table</h2>
      <p>The .table class adds basic styling (light padding and only horizontal dividers) to a table:</p>
      <table class="table">
        <thead>
          <tr>
            <th>#</th>
            <th style="width:20px">Firstname</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>1</td>
            <td>Anna</td>
          </tr>
          <tr>
            <td>2</td>
            <td>Debbie</td>
          </tr>
          <tr>
            <td>3</td>
            <td>John</td>
          </tr>
        </tbody>
      </table>
    </div>
  </body>

</html>
like image 352
Zanko Avatar asked Mar 05 '26 06:03

Zanko


1 Answers

You can set table-layout: fixed and have a static width of your columns.

.table {
  table-layout:fixed;
}

td, th {
  width: 90px;
}

Demo

.table {
  table-layout:fixed;
}

td, th {
  width: 90px;
  border: 1px solid black;
  text-align: left;
}
<table class="table">
        <thead>
          <tr>
            <th>#</th>
            <th>Firstname</th>
            <th>Lastname</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>1</td>
            <td>Anna</td>
            <td>Lastname</td>
          </tr>
          <tr>
            <td>2</td>
            <td>Debbie</td>
            <td>Lastname</td>
          </tr>
          <tr>
            <td>3</td>
            <td>John</td>
            <td>Lastname</td>
          </tr>
        </tbody>
      </table>
like image 68
Chris Avatar answered Mar 06 '26 20:03

Chris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!