htmlcssfonts

html table with column in monospace font


I am trying to create an html table using css. The first column is to be in monospace font, as it contains computer code.

The font-family attribute does not seem to work, however. This is the html file:

<html>    
<head>
<link rel="stylesheet" href="styles.css">
</head>    
<body>    
<table cellspacing="0" class="software">
<COLGROUP class="command">
<COLGROUP>
<tr>
  <td>command 1</td>
  <td>description</td>
</tr>
<tr>
  <td>command 2</td>
  <td>description</td>
</tr>
</table>    
</body>
</html>

The corresponding css file contains the following:

table.software{
    font-family: Arial;
    font-size: 13pt;
    border-width: 1px 1px 1px 1px;
    border-style: solid;
}    
table.software td{
    text-align: left;
    border-width: 1px 1px 1px 1px;
    border-style: solid;
    padding: 5px;
}    
.command{
    font-family: monospace;
    font-size: 10pt;
    width: 300;
}

I would be most grateful for a pointer of how to achieve the change of fonts.


Solution

  • COL and COLGROUP support a limited number of CSS styles.

    You can use the nth-child(x) CSS3 selector instead, where x is the number of the column (1 for the first column) you want to style:

    table.software{
        font-family: Arial;
        font-size: 13pt;
        border-width: 1px 1px 1px 1px;
        border-style: solid;
    }    
    
    table.software td {
        text-align: left;
        border-width: 1px 1px 1px 1px;
        border-style: solid;
        padding: 5px;
    }    
    
    table.software td:nth-child(1) {
        font-family: monospace;
        font-size: 10pt;
        width: 300;
    }
    <table cellspacing="0" class="software">
    <tr>
      <td>command 1</td>
      <td>description</td>
    </tr>
    <tr>
      <td>command 2</td>
      <td>description</td>
    </tr>
    </table>