tables.md 14.6 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
description: Documentation and examples for opt-in styling of tables (given their prevelant use in JavaScript plugins) with Bootstrap.
Mark Otto's avatar
Mark Otto committed
5
group: content
Mark Otto's avatar
Mark Otto committed
6
toc: true
Mark Otto's avatar
Mark Otto committed
7
8
---

Mark Otto's avatar
Mark Otto committed
9
10
## Examples

Mark Otto's avatar
Mark Otto committed
11
12
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>`, then extend with custom styles or our various included modifier classes.

13
Using the most basic table markup, here's how `.table`-based tables look in Bootstrap. **All table styles are inherited in Bootstrap 4**, meaning any nested tables will be styled in the same manner as the parent.
Mark Otto's avatar
Mark Otto committed
14
15
16
17
18
19
20
21
22
23
24
25
26

{% 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
27
      <th scope="row">1</th>
Mark Otto's avatar
Mark Otto committed
28
29
30
31
32
      <td>Mark</td>
      <td>Otto</td>
      <td>@mdo</td>
    </tr>
    <tr>
Mark Otto's avatar
Mark Otto committed
33
      <th scope="row">2</th>
Mark Otto's avatar
Mark Otto committed
34
35
36
37
38
      <td>Jacob</td>
      <td>Thornton</td>
      <td>@fat</td>
    </tr>
    <tr>
Mark Otto's avatar
Mark Otto committed
39
      <th scope="row">3</th>
Mark Otto's avatar
Mark Otto committed
40
41
42
43
44
45
46
47
      <td>Larry</td>
      <td>the Bird</td>
      <td>@twitter</td>
    </tr>
  </tbody>
</table>
{% endexample %}

Mark Otto's avatar
Mark Otto committed
48
You can also invert the colors—with light text on dark backgrounds—with `.table-inverse`.
Mark Otto's avatar
Mark Otto committed
49
50
51
52
53
54
55
56
57
58
59
60
61

{% 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
62
      <th scope="row">1</th>
Mark Otto's avatar
Mark Otto committed
63
64
65
66
67
      <td>Mark</td>
      <td>Otto</td>
      <td>@mdo</td>
    </tr>
    <tr>
Mark Otto's avatar
Mark Otto committed
68
      <th scope="row">2</th>
Mark Otto's avatar
Mark Otto committed
69
70
71
72
73
      <td>Jacob</td>
      <td>Thornton</td>
      <td>@fat</td>
    </tr>
    <tr>
Mark Otto's avatar
Mark Otto committed
74
      <th scope="row">3</th>
Mark Otto's avatar
Mark Otto committed
75
76
77
78
79
80
81
82
      <td>Larry</td>
      <td>the Bird</td>
      <td>@twitter</td>
    </tr>
  </tbody>
</table>
{% endexample %}

Mark Otto's avatar
Mark Otto committed
83
84
## Table head options

Mark Otto's avatar
Mark Otto committed
85
Similar to default and inverse tables, use one of two modifier classes to make `<thead>`s appear light or dark gray.
Mark Otto's avatar
Mark Otto committed
86
87
88
89
90
91
92
93
94
95
96
97
98

{% 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
99
      <th scope="row">1</th>
Mark Otto's avatar
Mark Otto committed
100
101
102
103
104
      <td>Mark</td>
      <td>Otto</td>
      <td>@mdo</td>
    </tr>
    <tr>
Mark Otto's avatar
Mark Otto committed
105
      <th scope="row">2</th>
Mark Otto's avatar
Mark Otto committed
106
107
108
109
110
      <td>Jacob</td>
      <td>Thornton</td>
      <td>@fat</td>
    </tr>
    <tr>
Mark Otto's avatar
Mark Otto committed
111
      <th scope="row">3</th>
Mark Otto's avatar
Mark Otto committed
112
113
114
115
116
      <td>Larry</td>
      <td>the Bird</td>
      <td>@twitter</td>
    </tr>
  </tbody>
117
118
119
</table>

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

Mark Otto's avatar
Mark Otto committed
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
## 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
167
      <th scope="row">1</th>
Mark Otto's avatar
Mark Otto committed
168
169
170
171
172
      <td>Mark</td>
      <td>Otto</td>
      <td>@mdo</td>
    </tr>
    <tr>
Mark Otto's avatar
Mark Otto committed
173
      <th scope="row">2</th>
Mark Otto's avatar
Mark Otto committed
174
175
176
177
178
      <td>Jacob</td>
      <td>Thornton</td>
      <td>@fat</td>
    </tr>
    <tr>
Mark Otto's avatar
Mark Otto committed
179
      <th scope="row">3</th>
Mark Otto's avatar
Mark Otto committed
180
181
182
183
184
185
186
187
      <td>Larry</td>
      <td>the Bird</td>
      <td>@twitter</td>
    </tr>
  </tbody>
</table>
{% endexample %}

188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
{% example html %}
<table class="table table-striped table-inverse">
  <thead>
    <tr>
      <th>#</th>
      <th>First Name</th>
      <th>Last Name</th>
      <th>Username</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th scope="row">1</th>
      <td>Mark</td>
      <td>Otto</td>
      <td>@mdo</td>
    </tr>
    <tr>
      <th scope="row">2</th>
      <td>Jacob</td>
      <td>Thornton</td>
      <td>@fat</td>
    </tr>
    <tr>
      <th scope="row">3</th>
      <td>Larry</td>
      <td>the Bird</td>
      <td>@twitter</td>
    </tr>
  </tbody>
</table>
{% endexample %}

Mark Otto's avatar
Mark Otto committed
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
## 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>
237
      <th scope="row">1</th>
Mark Otto's avatar
Mark Otto committed
238
239
240
241
242
      <td>Mark</td>
      <td>Otto</td>
      <td>@mdo</td>
    </tr>
    <tr>
243
      <th scope="row">2</th>
Mark Otto's avatar
Mark Otto committed
244
245
246
247
248
      <td>Mark</td>
      <td>Otto</td>
      <td>@TwBootstrap</td>
    </tr>
    <tr>
Mark Otto's avatar
Mark Otto committed
249
      <th scope="row">3</th>
Mark Otto's avatar
Mark Otto committed
250
251
252
253
254
      <td>Jacob</td>
      <td>Thornton</td>
      <td>@fat</td>
    </tr>
    <tr>
Mark Otto's avatar
Mark Otto committed
255
      <th scope="row">4</th>
Mark Otto's avatar
Mark Otto committed
256
257
258
259
260
261
262
      <td colspan="2">Larry the Bird</td>
      <td>@twitter</td>
    </tr>
  </tbody>
</table>
{% endexample %}

263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
{% example html %}
<table class="table table-bordered table-inverse">
  <thead>
    <tr>
      <th>#</th>
      <th>First Name</th>
      <th>Last Name</th>
      <th>Username</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th scope="row">1</th>
      <td>Mark</td>
      <td>Otto</td>
      <td>@mdo</td>
    </tr>
    <tr>
      <th scope="row">2</th>
      <td>Mark</td>
      <td>Otto</td>
      <td>@TwBootstrap</td>
    </tr>
    <tr>
      <th scope="row">3</th>
      <td>Jacob</td>
      <td>Thornton</td>
      <td>@fat</td>
    </tr>
    <tr>
      <th scope="row">4</th>
      <td colspan="2">Larry the Bird</td>
      <td>@twitter</td>
    </tr>
  </tbody>
</table>
{% endexample %}

Mark Otto's avatar
Mark Otto committed
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
## 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
317
      <th scope="row">1</th>
Mark Otto's avatar
Mark Otto committed
318
319
320
321
322
      <td>Mark</td>
      <td>Otto</td>
      <td>@mdo</td>
    </tr>
    <tr>
Mark Otto's avatar
Mark Otto committed
323
      <th scope="row">2</th>
Mark Otto's avatar
Mark Otto committed
324
325
326
327
328
      <td>Jacob</td>
      <td>Thornton</td>
      <td>@fat</td>
    </tr>
    <tr>
Mark Otto's avatar
Mark Otto committed
329
      <th scope="row">3</th>
Mark Otto's avatar
Mark Otto committed
330
331
332
333
334
335
336
      <td colspan="2">Larry the Bird</td>
      <td>@twitter</td>
    </tr>
  </tbody>
</table>
{% endexample %}

337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
{% example html %}
<table class="table table-hover table-inverse">
  <thead>
    <tr>
      <th>#</th>
      <th>First Name</th>
      <th>Last Name</th>
      <th>Username</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th scope="row">1</th>
      <td>Mark</td>
      <td>Otto</td>
      <td>@mdo</td>
    </tr>
    <tr>
      <th scope="row">2</th>
      <td>Jacob</td>
      <td>Thornton</td>
      <td>@fat</td>
    </tr>
    <tr>
      <th scope="row">3</th>
      <td colspan="2">Larry the Bird</td>
      <td>@twitter</td>
    </tr>
  </tbody>
</table>
{% endexample %}

369
## Small table
Mark Otto's avatar
Mark Otto committed
370

371
Add `.table-sm` to make tables more compact by cutting cell padding in half.
Mark Otto's avatar
Mark Otto committed
372
373

{% example html %}
374
<table class="table table-sm">
Mark Otto's avatar
Mark Otto committed
375
376
377
378
379
380
381
382
383
384
  <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
385
      <th scope="row">1</th>
Mark Otto's avatar
Mark Otto committed
386
387
388
389
390
      <td>Mark</td>
      <td>Otto</td>
      <td>@mdo</td>
    </tr>
    <tr>
Mark Otto's avatar
Mark Otto committed
391
      <th scope="row">2</th>
Mark Otto's avatar
Mark Otto committed
392
393
394
      <td>Jacob</td>
      <td>Thornton</td>
      <td>@fat</td>
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
    </tr>
    <tr>
      <th scope="row">3</th>
      <td colspan="2">Larry the Bird</td>
      <td>@twitter</td>
    </tr>
  </tbody>
</table>
{% endexample %}

{% example html %}
<table class="table table-sm table-inverse">
  <thead>
    <tr>
      <th>#</th>
      <th>First Name</th>
      <th>Last Name</th>
      <th>Username</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th scope="row">1</th>
      <td>Mark</td>
      <td>Otto</td>
      <td>@mdo</td>
    </tr>
    <tr>
      <th scope="row">2</th>
      <td>Jacob</td>
      <td>Thornton</td>
      <td>@fat</td>
Mark Otto's avatar
Mark Otto committed
427
428
    </tr>
    <tr>
Mark Otto's avatar
Mark Otto committed
429
      <th scope="row">3</th>
Mark Otto's avatar
Mark Otto committed
430
431
432
433
434
435
436
437
438
439
440
      <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.

441
<div class="bd-example">
Mark Otto's avatar
Mark Otto committed
442
443
444
  <table class="table">
    <thead>
      <tr>
445
        <th>Type</th>
Mark Otto's avatar
Mark Otto committed
446
447
448
449
450
451
        <th>Column heading</th>
        <th>Column heading</th>
        <th>Column heading</th>
      </tr>
    </thead>
    <tbody>
452
      <tr class="table-active">
453
        <th scope="row">Active</th>
Mark Otto's avatar
Mark Otto committed
454
455
456
457
458
        <td>Column content</td>
        <td>Column content</td>
        <td>Column content</td>
      </tr>
      <tr>
459
        <th scope="row">Default</th>
Mark Otto's avatar
Mark Otto committed
460
461
462
463
        <td>Column content</td>
        <td>Column content</td>
        <td>Column content</td>
      </tr>
464
465
466
467

      {% for color in site.data.theme-colors %}
      <tr class="table-{{ color.name }}">
        <th scope="row">{{ color.name | capitalize }}</th>
Mark Otto's avatar
Mark Otto committed
468
469
470
        <td>Column content</td>
        <td>Column content</td>
        <td>Column content</td>
471
      </tr>{% endfor %}
Mark Otto's avatar
Mark Otto committed
472
473
474
475
476
477
    </tbody>
  </table>
</div>

{% highlight html %}
<!-- On rows -->
478
<tr class="table-active">...</tr>
479
480
{% for color in site.data.theme-colors %}
<tr class="table-{{ color.name }}">...</tr>{% endfor %}
Mark Otto's avatar
Mark Otto committed
481
482
483

<!-- On cells (`td` or `th`) -->
<tr>
484
  <td class="table-active">...</td>
485
486
  {% for color in site.data.theme-colors %}
  <td class="table-{{ color.name }}">...</td>{% endfor %}
Mark Otto's avatar
Mark Otto committed
487
488
489
</tr>
{% endhighlight %}

Mark Otto's avatar
Mark Otto committed
490
Regular table background variants are not available with the inverse table, however, you may use [text or background utilities]({{ site.baseurl }}/docs/{{ site.docs_version }}/utilities/colors/) to achieve similar styles.
Mark Otto's avatar
Mark Otto committed
491
492
493
494
495
496
497
498
499
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560

<div class="bd-example">
  <table class="table table-inverse">
    <thead>
      <tr>
        <th>#</th>
        <th>Column heading</th>
        <th>Column heading</th>
        <th>Column heading</th>
      </tr>
    </thead>
    <tbody>
      <tr class="bg-primary">
        <th scope="row">1</th>
        <td>Column content</td>
        <td>Column content</td>
        <td>Column content</td>
      </tr>
      <tr>
        <th scope="row">2</th>
        <td>Column content</td>
        <td>Column content</td>
        <td>Column content</td>
      </tr>
      <tr class="bg-success">
        <th scope="row">3</th>
        <td>Column content</td>
        <td>Column content</td>
        <td>Column content</td>
      </tr>
      <tr>
        <th scope="row">4</th>
        <td>Column content</td>
        <td>Column content</td>
        <td>Column content</td>
      </tr>
      <tr class="bg-info">
        <th scope="row">5</th>
        <td>Column content</td>
        <td>Column content</td>
        <td>Column content</td>
      </tr>
      <tr>
        <th scope="row">6</th>
        <td>Column content</td>
        <td>Column content</td>
        <td>Column content</td>
      </tr>
      <tr class="bg-warning">
        <th scope="row">7</th>
        <td>Column content</td>
        <td>Column content</td>
        <td>Column content</td>
      </tr>
      <tr>
        <th scope="row">8</th>
        <td>Column content</td>
        <td>Column content</td>
        <td>Column content</td>
      </tr>
      <tr class="bg-danger">
        <th scope="row">9</th>
        <td>Column content</td>
        <td>Column content</td>
        <td>Column content</td>
      </tr>
    </tbody>
  </table>
</div>

Mark Otto's avatar
Mark Otto committed
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
{% highlight html %}
<!-- On rows -->
<tr class="bg-primary">...</tr>
<tr class="bg-success">...</tr>
<tr class="bg-warning">...</tr>
<tr class="bg-danger">...</tr>
<tr class="bg-info">...</tr>

<!-- On cells (`td` or `th`) -->
<tr>
  <td class="bg-primary">...</td>
  <td class="bg-success">...</td>
  <td class="bg-warning">...</td>
  <td class="bg-danger">...</td>
  <td class="bg-info">...</td>
</tr>
{% endhighlight %}

Mark Otto's avatar
Mark Otto committed
579
580
581
{% capture callout-include %}{% include callout-warning-color-assistive-technologies.md %}{% endcapture %}
{{ callout-include | markdownify }}

Mark Otto's avatar
Mark Otto committed
582
583
## Responsive tables

584
Create responsive tables by adding `.table-responsive` to any `.table` 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.
Mark Otto's avatar
Mark Otto committed
585

586
587
588
589
590
591
{% 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 %}

592
<div class="bd-example">
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
  <table class="table table-responsive">
    <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>
        <th scope="row">1</th>
        <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>
        <th scope="row">2</th>
        <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>
        <th scope="row">3</th>
        <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>
Mark Otto's avatar
Mark Otto committed
635

636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
  <table class="table table-bordered table-responsive">
    <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>
        <th scope="row">1</th>
        <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>
        <th scope="row">2</th>
        <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>
        <th scope="row">3</th>
        <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>
Mark Otto's avatar
Mark Otto committed
677
678
  </table>
</div>
679
680

{% highlight html %}
681
<table class="table table-responsive">
682
683
  ...
</table>
Mark Otto's avatar
Mark Otto committed
684
{% endhighlight %}