6

In css tables, I am noticing that if I add a border to display: table; element, it only draws border around all display: table-row; nodes, but not around display: table-caption; element, even if the table-caption node is a child of the display: table;. Why does it do so?

How do I get it to draw border around whole table (i.e. include table-caption inside the border)?

I have created a fiddle to demonstrate the point: https://jsfiddle.net/9028hswc/

6
  • because caption is not meant to be part of the data, i'd say ...
    – G-Cyrillus
    Commented Mar 9, 2016 at 2:30
  • table caption is not part of the table itself: stackoverflow.com/a/35543537/3597276 Commented Mar 9, 2016 at 2:34
  • mind also the caption-side CSS rule :) jsfiddle.net/9028hswc/1 (where it works ) so the borders ?
    – G-Cyrillus
    Commented Mar 9, 2016 at 2:38
  • 1
    @Michael_B: You should post an answer.
    – BoltClock
    Commented Mar 9, 2016 at 3:06
  • Thanks @Michael_B. Thats useful info.
    – codneto
    Commented Mar 9, 2016 at 5:03

1 Answer 1

4

Any element with the CSS property display: table-caption behaves like a <caption> element.

From MDN:

The HTML <caption> Element (or HTML Table Caption Element) represents the title of a table. Though it is always the first descendant of a , its styling, using CSS, may place it elsewhere, relative to the table.

So, technically, it's not part of the table and that's why it is not inside the border.

As 2-cents mentions in the comment, here is how you get the border around all of the content: https://jsfiddle.net/25zwopqr/

Changes:

HTML

<caption class='table-caption-div'>People Names</caption>

CSS

.table-caption-div {
  display: inline-block;
  background-color: #cf0;
}
2
  • 2
    caption { display: inline-block; } will place it inside the border, if that's what he wants. Commented Mar 9, 2016 at 2:43
  • caption { display: inline-block; } will place it below <thead>.
    – Julien
    Commented Jun 23, 2021 at 8:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.