Search code examples
python-sphinxrestructuredtext

Add CSS Class to Sphinx Table Cell


Is there a way to add a CSS class to a Sphinx-generated HTML table? For example, I want to modify the following RST table definition:

+-------+
| cell1 |
+-------+
| cell2 |
+-------+

To generate the following HTML (roughly):

<table>
    ...
    <tbody>
        <tr><td>cell1</td></tr>
        <tr><td class="someClass">cell2</td></tr>
    </tbody>
</table>

The following RST adds the specified class to a new paragraph element within the cell (instead of on the cell itself):

+--------------------------+
| cell1                    |
+--------------------------+
| .. rst-class:: someClass |
| cell2                    |
+--------------------------+

Is there any way to modify the original RST source to generate the specified HTML table?


Solution

  • As of December 2023, all major desktop and mobile browsers support the CSS has selector. This enables applying the style to the paragraph element, then writing CSS such as:

    td:has(>.someClass) {
        /* Some style to apply to the td here */
    }