1

Thanks for helping out. I've searched for the answer to this for a while because it seems pretty basic, but can't find anything so I'm posting here.

Why does column-span not seem to work in CSS for table cells? For example, specifying the style inline like this works just fine:

<table>
  <tbody>
    <tr>
      <td colspan="5"><i>Span!</i></td>
    </tr>
    <tr>
      <td>Cell 1</td>
      <td>Cell 2</td>
    </tr>
  </tbody>
</table>

But if I move the colspan to CSS:

.my-class td {
  column-span: all;
}
<table>
  <tbody>
    <tr class="my-class">
      <td class="my-class"><i>No span?</i></td>
    </tr>
    <tr>
      <td>Cell 1</td>
      <td>Cell 2</td>
    </tr>
  </tbody>
</table>

Then it does NOT work. Other style attributes (e.g. background color) DO work just fine. But column-span doesn't. What's going on here?

UPDATE: Someone suggested that it's the fact that I was using a number in my CSS (I used "column-span: 5"). But it also doesn't work if I use "all" instead, so I've update the snippet to reflect that.

SOLVED. Thanks for explaining! Didn't realize column-span CSS was not for tables.

3
  • 1
    Does this answer your question? HTML colspan in CSS Commented Oct 11, 2022 at 19:35
  • Close, but that question is talking about non-table elements. I'm trying to understand why the colspan attribute for a td works inline but not in CSS. Thanks, though. Commented Oct 11, 2022 at 19:37
  • I'm pretty sure the column-span CSS property is intended for multi column layouts, not for table cells as an alternative to the colspan HTML attribute. Commented Oct 11, 2022 at 20:15

3 Answers 3

2

The colspan attribute was created to implement the structure. Styles can't change html structure. You cannot use the column-span for table structures.

https://developer.mozilla.org/en-US/docs/Web/CSS/column-span

Sign up to request clarification or add additional context in comments.

Comments

1

column-span does not work for table layouts, only when using columns layout. You should use the colspan attribute.

Comments

1

Check out W3 School's CSS column-span Property description here. Suggests that CSS column span doesn't permit a number in this case. Though I may be misreading it.

1 Comment

Thanks, but "all" doesn't work, either. I just updated the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.