Bold "<b>" does not apply in a range

Asked

Viewed 222 times

4

In a table, if I apply bold (<b>) out of cells (<td>), it doesn’t work:

<table>
  <tr>
    <b>
    <td>Cell A</td>
    <td>Cell B</td>
    <td>Cell C</td>
    <td>Cell D</td>
    </b>
  </tr>
</table>



If apply inside, (obvious) works, but I have to put in everyone who wants:

    <table>
      <tr>
        <td><b>Cell A</b></td>
        <td>Cell B</td>
        <td><b>Cell C</b></td>
        <td>Cell D</td>
      </tr>
    </table>


  • Why this occurs?
  • The only way would be manually place in each cell, or by CSS?
  • Works in range of other types of tags?
  • but if you want to use it so much <b> you can put it off the <table>, but CSS is more recommended and makes HTML cleaner

2 answers

5


According to the living standard of the element Table only the following elements may be used with 'children':

This is because the element aims to display tabular data, not formatting. To do this, use CSS:

td.bold {font-weight:bold;}
<table>
  <tr>
    <td class='bold'>Cell A</td>
    <td>Cell B</td>
    <td class='bold'>Cell C</td>
    <td>Cell D</td>
  </tr>
</table>

3

"Why does this happen?"

Because an element TD must be followed by an element TR, there is no room for elements between them.

"The only way would be to manually place in each cell, or by CSS?"

Yes, in each TD (horrible), or using CSS, dai can be a class or applying directly from TR also, that would work in your case

"Works in range of other types of tags?"

Yeah, it could be inside a DIV for example, and affect all elements then but the tag <B> is discontinued, is considered obsolete (read more here) and therefore recommended to use the CSS attribute for this, font-weight: bold for example:

tr {
  font-weight: bold
}

Here only one example, better apply a class otherwise all lines will be bold, or if it is a title use TH

Browser other questions tagged

You are not signed in. Login or sign up in order to post.