tables.md 13 KB
Newer Older
Mark Otto's avatar
Mark Otto committed
1
---
Mark Otto's avatar
Mark Otto committed
2
layout: docs
Mark Otto's avatar
Mark Otto committed
3
title: Tables
Mark Otto's avatar
Mark Otto committed
4
group: content
Mark Otto's avatar
Mark Otto committed
5
6
---

7
Due to the widespread use of tables across third-party widgets like calendars and date pickers, we've designed our tables to be **opt-in**. Just add the base class `.table` to any `<table>`.
Mark Otto's avatar
Mark Otto committed
8

Mark Otto's avatar
Mark Otto committed
9
10
11
12
13
## Contents

* Will be replaced with the ToC, excluding the "Contents" header
{:toc}

Mark Otto's avatar
Mark Otto committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
## Basic example

{% example html %}
<table class="table">
  <thead>
    <tr>
      <th>#</th>
      <th>First Name</th>
      <th>Last Name</th>
      <th>Username</th>
    </tr>
  </thead>
  <tbody>
    <tr>
Mark Otto's avatar
Mark Otto committed
28
      <th scope="row">1</th>
Mark Otto's avatar
Mark Otto committed
29
30
31
32
33
      <td>Mark</td>
      <td>Otto</td>
      <td>@mdo</td>
    </tr>
    <tr>
Mark Otto's avatar
Mark Otto committed
34
      <th scope="row">2</th>
Mark Otto's avatar
Mark Otto committed
35
36
37
38
39
      <td>Jacob</td>
      <td>Thornton</td>
      <td>@fat</td>
    </tr>
    <tr>
Mark Otto's avatar
Mark Otto committed
40
      <th scope="row">3</th>
Mark Otto's avatar
Mark Otto committed
41
42
43
44
45
46
47
48
      <td>Larry</td>
      <td>the Bird</td>
      <td>@twitter</td>
    </tr>
  </tbody>
</table>
{% endexample %}

Mark Otto's avatar
Mark Otto committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
## Inverse table

{% example html %}
<table class="table table-inverse">
  <thead>
    <tr>
      <th>#</th>
      <th>First Name</th>
      <th>Last Name</th>
      <th>Username</th>
    </tr>
  </thead>
  <tbody>
    <tr>
Mark Otto's avatar
Mark Otto committed
63
      <th scope="row">1</th>
Mark Otto's avatar
Mark Otto committed
64
65
66
67
68
      <td>Mark</td>
      <td>Otto</td>
      <td>@mdo</td>
    </tr>
    <tr>
Mark Otto's avatar
Mark Otto committed
69
      <th scope="row">2</th>
Mark Otto's avatar
Mark Otto committed
70
71
72
73
74
      <td>Jacob</td>
      <td>Thornton</td>
      <td>@fat</td>
    </tr>
    <tr>
Mark Otto's avatar
Mark Otto committed
75
      <th scope="row">3</th>
Mark Otto's avatar
Mark Otto committed
76
77
78
79
80
81
82
83
      <td>Larry</td>
      <td>the Bird</td>
      <td>@twitter</td>
    </tr>
  </tbody>
</table>
{% endexample %}

Mark Otto's avatar
Mark Otto committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
## Table head options

Use one of two modifier classes to make `<thead>`s appear light or dark gray.

{% example html %}
<table class="table">
  <thead class="thead-inverse">
    <tr>
      <th>#</th>
      <th>First Name</th>
      <th>Last Name</th>
      <th>Username</th>
    </tr>
  </thead>
  <tbody>
    <tr>
Mark Otto's avatar
Mark Otto committed
100
      <th scope="row">1</th>
Mark Otto's avatar
Mark Otto committed
101
102
103
104
105
      <td>Mark</td>
      <td>Otto</td>
      <td>@mdo</td>
    </tr>
    <tr>
Mark Otto's avatar
Mark Otto committed
106
      <th scope="row">2</th>
Mark Otto's avatar
Mark Otto committed
107
108
109
110
111
      <td>Jacob</td>
      <td>Thornton</td>
      <td>@fat</td>
    </tr>
    <tr>
Mark Otto's avatar
Mark Otto committed
112
      <th scope="row">3</th>
Mark Otto's avatar
Mark Otto committed
113
114
115
116
117
      <td>Larry</td>
      <td>the Bird</td>
      <td>@twitter</td>
    </tr>
  </tbody>
118
119
120
</table>

<table class="table">
Mark Otto's avatar
Mark Otto committed
121
122
123
124
125
126
127
128
129
130
  <thead class="thead-default">
    <tr>
      <th>#</th>
      <th>First Name</th>
      <th>Last Name</th>
      <th>Username</th>
    </tr>
  </thead>
  <tbody>
    <tr>
Mark Otto's avatar
Mark Otto committed
131
      <th scope="row">1</th>
Mark Otto's avatar
Mark Otto committed
132
133
134
135
136
      <td>Mark</td>
      <td>Otto</td>
      <td>@mdo</td>
    </tr>
    <tr>
Mark Otto's avatar
Mark Otto committed
137
      <th scope="row">2</th>
Mark Otto's avatar
Mark Otto committed
138
139
140
141
142
      <td>Jacob</td>
      <td>Thornton</td>
      <td>@fat</td>
    </tr>
    <tr>
Mark Otto's avatar
Mark Otto committed
143
      <th scope="row">3</th>
Mark Otto's avatar
Mark Otto committed
144
145
146
147
148
149
150
151
      <td>Larry</td>
      <td>the Bird</td>
      <td>@twitter</td>
    </tr>
  </tbody>
</table>
{% endexample %}

Mark Otto's avatar
Mark Otto committed
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
## Striped rows

Use `.table-striped` to add zebra-striping to any table row within the `<tbody>`.

{% example html %}
<table class="table table-striped">
  <thead>
    <tr>
      <th>#</th>
      <th>First Name</th>
      <th>Last Name</th>
      <th>Username</th>
    </tr>
  </thead>
  <tbody>
    <tr>
Mark Otto's avatar
Mark Otto committed
168
      <th scope="row">1</th>
Mark Otto's avatar
Mark Otto committed
169
170
171
172
173
      <td>Mark</td>
      <td>Otto</td>
      <td>@mdo</td>
    </tr>
    <tr>
Mark Otto's avatar
Mark Otto committed
174
      <th scope="row">2</th>
Mark Otto's avatar
Mark Otto committed
175
176
177
178
179
      <td>Jacob</td>
      <td>Thornton</td>
      <td>@fat</td>
    </tr>
    <tr>
Mark Otto's avatar
Mark Otto committed
180
      <th scope="row">3</th>
Mark Otto's avatar
Mark Otto committed
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
      <td>Larry</td>
      <td>the Bird</td>
      <td>@twitter</td>
    </tr>
  </tbody>
</table>
{% endexample %}

## Bordered table

Add `.table-bordered` for borders on all sides of the table and cells.

{% example html %}
<table class="table table-bordered">
  <thead>
    <tr>
      <th>#</th>
      <th>First Name</th>
      <th>Last Name</th>
      <th>Username</th>
    </tr>
  </thead>
  <tbody>
    <tr>
205
      <th scope="row">1</th>
Mark Otto's avatar
Mark Otto committed
206
207
208
209
210
      <td>Mark</td>
      <td>Otto</td>
      <td>@mdo</td>
    </tr>
    <tr>
211
      <th scope="row">2</th>
Mark Otto's avatar
Mark Otto committed
212
213
214
215
216
      <td>Mark</td>
      <td>Otto</td>
      <td>@TwBootstrap</td>
    </tr>
    <tr>
Mark Otto's avatar
Mark Otto committed
217
      <th scope="row">3</th>
Mark Otto's avatar
Mark Otto committed
218
219
220
221
222
      <td>Jacob</td>
      <td>Thornton</td>
      <td>@fat</td>
    </tr>
    <tr>
Mark Otto's avatar
Mark Otto committed
223
      <th scope="row">4</th>
Mark Otto's avatar
Mark Otto committed
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
      <td colspan="2">Larry the Bird</td>
      <td>@twitter</td>
    </tr>
  </tbody>
</table>
{% endexample %}

## Hoverable rows

Add `.table-hover` to enable a hover state on table rows within a `<tbody>`.

{% example html %}
<table class="table table-hover">
  <thead>
    <tr>
      <th>#</th>
      <th>First Name</th>
      <th>Last Name</th>
      <th>Username</th>
    </tr>
  </thead>
  <tbody>
    <tr>
Mark Otto's avatar
Mark Otto committed
247
      <th scope="row">1</th>
Mark Otto's avatar
Mark Otto committed
248
249
250
251
252
      <td>Mark</td>
      <td>Otto</td>
      <td>@mdo</td>
    </tr>
    <tr>
Mark Otto's avatar
Mark Otto committed
253
      <th scope="row">2</th>
Mark Otto's avatar
Mark Otto committed
254
255
256
257
258
      <td>Jacob</td>
      <td>Thornton</td>
      <td>@fat</td>
    </tr>
    <tr>
Mark Otto's avatar
Mark Otto committed
259
      <th scope="row">3</th>
Mark Otto's avatar
Mark Otto committed
260
261
262
263
264
265
266
      <td colspan="2">Larry the Bird</td>
      <td>@twitter</td>
    </tr>
  </tbody>
</table>
{% endexample %}

267
## Small table
Mark Otto's avatar
Mark Otto committed
268

269
Add `.table-sm` to make tables more compact by cutting cell padding in half.
Mark Otto's avatar
Mark Otto committed
270
271

{% example html %}
272
<table class="table table-sm">
Mark Otto's avatar
Mark Otto committed
273
274
275
276
277
278
279
280
281
282
  <thead>
    <tr>
      <th>#</th>
      <th>First Name</th>
      <th>Last Name</th>
      <th>Username</th>
    </tr>
  </thead>
  <tbody>
    <tr>
Mark Otto's avatar
Mark Otto committed
283
      <th scope="row">1</th>
Mark Otto's avatar
Mark Otto committed
284
285
286
287
288
      <td>Mark</td>
      <td>Otto</td>
      <td>@mdo</td>
    </tr>
    <tr>
Mark Otto's avatar
Mark Otto committed
289
      <th scope="row">2</th>
Mark Otto's avatar
Mark Otto committed
290
291
292
293
294
      <td>Jacob</td>
      <td>Thornton</td>
      <td>@fat</td>
    </tr>
    <tr>
Mark Otto's avatar
Mark Otto committed
295
      <th scope="row">3</th>
Mark Otto's avatar
Mark Otto committed
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
      <td colspan="2">Larry the Bird</td>
      <td>@twitter</td>
    </tr>
  </tbody>
</table>
{% endexample %}

## Contextual classes

Use contextual classes to color table rows or individual cells.

<div class="table-responsive">
  <table class="table table-bordered table-striped">
    <colgroup>
      <col class="col-xs-1">
      <col class="col-xs-7">
    </colgroup>
    <thead>
      <tr>
        <th>Class</th>
        <th>Description</th>
      </tr>
    </thead>
    <tbody>
      <tr>
Mark Otto's avatar
Mark Otto committed
321
        <th scope="row">
322
          <code>.table-active</code>
Mark Otto's avatar
Mark Otto committed
323
        </th>
Mark Otto's avatar
Mark Otto committed
324
325
326
        <td>Applies the hover color to a particular row or cell</td>
      </tr>
      <tr>
Mark Otto's avatar
Mark Otto committed
327
        <th scope="row">
328
          <code>.table-success</code>
Mark Otto's avatar
Mark Otto committed
329
        </th>
Mark Otto's avatar
Mark Otto committed
330
331
332
        <td>Indicates a successful or positive action</td>
      </tr>
      <tr>
Mark Otto's avatar
Mark Otto committed
333
        <th scope="row">
334
          <code>.table-info</code>
Mark Otto's avatar
Mark Otto committed
335
        </th>
Mark Otto's avatar
Mark Otto committed
336
337
338
        <td>Indicates a neutral informative change or action</td>
      </tr>
      <tr>
Mark Otto's avatar
Mark Otto committed
339
        <th scope="row">
340
          <code>.table-warning</code>
Mark Otto's avatar
Mark Otto committed
341
        </th>
Mark Otto's avatar
Mark Otto committed
342
343
344
        <td>Indicates a warning that might need attention</td>
      </tr>
      <tr>
Mark Otto's avatar
Mark Otto committed
345
        <th scope="row">
346
          <code>.table-danger</code>
Mark Otto's avatar
Mark Otto committed
347
        </th>
Mark Otto's avatar
Mark Otto committed
348
349
350
351
352
353
        <td>Indicates a dangerous or potentially negative action</td>
      </tr>
    </tbody>
  </table>
</div>

354
<div class="bd-example">
Mark Otto's avatar
Mark Otto committed
355
356
357
358
359
360
361
362
363
364
  <table class="table">
    <thead>
      <tr>
        <th>#</th>
        <th>Column heading</th>
        <th>Column heading</th>
        <th>Column heading</th>
      </tr>
    </thead>
    <tbody>
365
      <tr class="table-active">
Mark Otto's avatar
Mark Otto committed
366
        <th scope="row">1</th>
Mark Otto's avatar
Mark Otto committed
367
368
369
370
371
        <td>Column content</td>
        <td>Column content</td>
        <td>Column content</td>
      </tr>
      <tr>
Mark Otto's avatar
Mark Otto committed
372
        <th scope="row">2</th>
Mark Otto's avatar
Mark Otto committed
373
374
375
376
        <td>Column content</td>
        <td>Column content</td>
        <td>Column content</td>
      </tr>
377
      <tr class="table-success">
Mark Otto's avatar
Mark Otto committed
378
        <th scope="row">3</th>
Mark Otto's avatar
Mark Otto committed
379
380
381
382
383
        <td>Column content</td>
        <td>Column content</td>
        <td>Column content</td>
      </tr>
      <tr>
Mark Otto's avatar
Mark Otto committed
384
        <th scope="row">4</th>
Mark Otto's avatar
Mark Otto committed
385
386
387
388
        <td>Column content</td>
        <td>Column content</td>
        <td>Column content</td>
      </tr>
389
      <tr class="table-info">
Mark Otto's avatar
Mark Otto committed
390
        <th scope="row">5</th>
Mark Otto's avatar
Mark Otto committed
391
392
393
394
395
        <td>Column content</td>
        <td>Column content</td>
        <td>Column content</td>
      </tr>
      <tr>
Mark Otto's avatar
Mark Otto committed
396
        <th scope="row">6</th>
Mark Otto's avatar
Mark Otto committed
397
398
399
400
        <td>Column content</td>
        <td>Column content</td>
        <td>Column content</td>
      </tr>
401
      <tr class="table-warning">
Mark Otto's avatar
Mark Otto committed
402
        <th scope="row">7</th>
Mark Otto's avatar
Mark Otto committed
403
404
405
406
407
        <td>Column content</td>
        <td>Column content</td>
        <td>Column content</td>
      </tr>
      <tr>
Mark Otto's avatar
Mark Otto committed
408
        <th scope="row">8</th>
Mark Otto's avatar
Mark Otto committed
409
410
411
412
        <td>Column content</td>
        <td>Column content</td>
        <td>Column content</td>
      </tr>
413
      <tr class="table-danger">
Mark Otto's avatar
Mark Otto committed
414
        <th scope="row">9</th>
Mark Otto's avatar
Mark Otto committed
415
416
417
418
419
420
421
422
423
424
        <td>Column content</td>
        <td>Column content</td>
        <td>Column content</td>
      </tr>
    </tbody>
  </table>
</div>

{% highlight html %}
<!-- On rows -->
425
426
427
428
429
<tr class="table-active">...</tr>
<tr class="table-success">...</tr>
<tr class="table-warning">...</tr>
<tr class="table-danger">...</tr>
<tr class="table-info">...</tr>
Mark Otto's avatar
Mark Otto committed
430
431
432

<!-- On cells (`td` or `th`) -->
<tr>
433
434
435
436
437
  <td class="table-active">...</td>
  <td class="table-success">...</td>
  <td class="table-warning">...</td>
  <td class="table-danger">...</td>
  <td class="table-info">...</td>
Mark Otto's avatar
Mark Otto committed
438
439
440
441
442
443
444
</tr>
{% endhighlight %}

## Responsive tables

Create responsive tables by wrapping any `.table` in `.table-responsive` to make them scroll horizontally on small devices (under 768px). When viewing on anything larger than 768px wide, you will not see any difference in these tables.

445
446
447
448
449
450
451
452
453
454
{% callout warning %}
#### Vertical clipping/truncation

Responsive tables make use of `overflow-y: hidden`, which clips off any content that goes beyond the bottom or top edges of the table. In particular, this can clip off dropdown menus and other third-party widgets.
{% endcallout %}

{% callout warning %}
#### Firefox and fieldsets

Firefox has some awkward fieldset styling involving `width` that interferes with the responsive table. This cannot be overriden without a Firefox-specific hack that we **don't** provide in Bootstrap:
Mark Otto's avatar
Mark Otto committed
455

Mark Otto's avatar
Mark Otto committed
456
457
458
459
460
{% highlight css %}
@-moz-document url-prefix() {
  fieldset { display: table-cell; }
}
{% endhighlight %}
461

Mark Otto's avatar
Mark Otto committed
462
For more information, read [this Stack Overflow answer](https://stackoverflow.com/questions/17408815/fieldset-resizes-wrong-appears-to-have-unremovable-min-width-min-content/17863685#17863685).
463
{% endcallout %}
Mark Otto's avatar
Mark Otto committed
464

465
<div class="bd-example">
Mark Otto's avatar
Mark Otto committed
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
  <div class="table-responsive">
    <table class="table">
      <thead>
        <tr>
          <th>#</th>
          <th>Table heading</th>
          <th>Table heading</th>
          <th>Table heading</th>
          <th>Table heading</th>
          <th>Table heading</th>
          <th>Table heading</th>
        </tr>
      </thead>
      <tbody>
        <tr>
Mark Otto's avatar
Mark Otto committed
481
          <th scope="row">1</th>
Mark Otto's avatar
Mark Otto committed
482
483
484
485
486
487
488
489
          <td>Table cell</td>
          <td>Table cell</td>
          <td>Table cell</td>
          <td>Table cell</td>
          <td>Table cell</td>
          <td>Table cell</td>
        </tr>
        <tr>
Mark Otto's avatar
Mark Otto committed
490
          <th scope="row">2</th>
Mark Otto's avatar
Mark Otto committed
491
492
493
494
495
496
497
498
          <td>Table cell</td>
          <td>Table cell</td>
          <td>Table cell</td>
          <td>Table cell</td>
          <td>Table cell</td>
          <td>Table cell</td>
        </tr>
        <tr>
Mark Otto's avatar
Mark Otto committed
499
          <th scope="row">3</th>
Mark Otto's avatar
Mark Otto committed
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
          <td>Table cell</td>
          <td>Table cell</td>
          <td>Table cell</td>
          <td>Table cell</td>
          <td>Table cell</td>
          <td>Table cell</td>
        </tr>
      </tbody>
    </table>
  </div>

  <div class="table-responsive">
    <table class="table table-bordered">
      <thead>
        <tr>
          <th>#</th>
          <th>Table heading</th>
          <th>Table heading</th>
          <th>Table heading</th>
          <th>Table heading</th>
          <th>Table heading</th>
          <th>Table heading</th>
        </tr>
      </thead>
      <tbody>
        <tr>
Mark Otto's avatar
Mark Otto committed
526
          <th scope="row">1</th>
Mark Otto's avatar
Mark Otto committed
527
528
529
530
531
532
533
534
          <td>Table cell</td>
          <td>Table cell</td>
          <td>Table cell</td>
          <td>Table cell</td>
          <td>Table cell</td>
          <td>Table cell</td>
        </tr>
        <tr>
Mark Otto's avatar
Mark Otto committed
535
          <th scope="row">2</th>
Mark Otto's avatar
Mark Otto committed
536
537
538
539
540
541
542
543
          <td>Table cell</td>
          <td>Table cell</td>
          <td>Table cell</td>
          <td>Table cell</td>
          <td>Table cell</td>
          <td>Table cell</td>
        </tr>
        <tr>
Mark Otto's avatar
Mark Otto committed
544
          <th scope="row">3</th>
Mark Otto's avatar
Mark Otto committed
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
          <td>Table cell</td>
          <td>Table cell</td>
          <td>Table cell</td>
          <td>Table cell</td>
          <td>Table cell</td>
          <td>Table cell</td>
        </tr>
      </tbody>
    </table>
  </div>
</div>

{% highlight html %}
<div class="table-responsive">
  <table class="table">
    ...
  </table>
</div>
{% endhighlight %}
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582


### Reflow

{% example html %}
<table class="table table-reflow">
  <thead>
    <tr>
      <th>#</th>
      <th>Table heading</th>
      <th>Table heading</th>
      <th>Table heading</th>
      <th>Table heading</th>
      <th>Table heading</th>
      <th>Table heading</th>
    </tr>
  </thead>
  <tbody>
    <tr>
Mark Otto's avatar
Mark Otto committed
583
      <th scope="row">1</th>
584
585
586
587
588
589
590
591
      <td>Table cell</td>
      <td>Table cell</td>
      <td>Table cell</td>
      <td>Table cell</td>
      <td>Table cell</td>
      <td>Table cell</td>
    </tr>
    <tr>
Mark Otto's avatar
Mark Otto committed
592
      <th scope="row">2</th>
593
594
595
596
597
598
599
600
      <td>Table cell</td>
      <td>Table cell</td>
      <td>Table cell</td>
      <td>Table cell</td>
      <td>Table cell</td>
      <td>Table cell</td>
    </tr>
    <tr>
Mark Otto's avatar
Mark Otto committed
601
      <th scope="row">3</th>
602
603
604
605
606
607
608
609
610
611
      <td>Table cell</td>
      <td>Table cell</td>
      <td>Table cell</td>
      <td>Table cell</td>
      <td>Table cell</td>
      <td>Table cell</td>
    </tr>
  </tbody>
</table>
{% endexample %}