Search code examples
cssmarkdownjekyll

How to change padding of a table within markdown?


I'm building tables using Jekyll Spaceship and am not happy with how much space a single row takes up. My tables are quite simple which I have in a markdown file.

|              Stage | Direct Products | ATP Yields |
| -----------------: | --------------: | ---------: |
|         Glycolysis |          2 ATP              ||
| Pyruvaye oxidation |          2 NADH |      5 ATP |
|  Citric acid cycle |          2 ATP              ||
|                               30--32 ATP        |||

My problem is that the text takes up not even half the cell space with padding above and below the text.

How would I decrease the padding in a markdown file for rows in a table?

I've also tried adding the following ...

<style>
table, th, td {
    padding: 1px;
}
</style>

<div class="bettertd">

| .... placed markdown tables here ... |

</div> 

And this put this in CSS ...

.features table {
    styles="padding:1px";
}

But didn't work.


Solution

  • At First, you can specific a class for the table as below:

    |              Stage | Direct Products | ATP Yields |
    | -----------------: | --------------: | ---------: |
    |         Glycolysis |          2 ATP              ||
    | Pyruvaye oxidation |          2 NADH |      5 ATP |
    |  Citric acid cycle |          2 ATP              ||
    |                               30--32 ATP        |||
    {:.custom-table}
    

    And Second step, you need to add your custom css styles to the your jekyll theme css file (usually under the _sass folder and named *.sass).

    //...
    
    .custom-table th, .custom-table td {
       padding: 1px;
       // any style you want ...
    }
    

    Or add to the head of your theme post layout (usually under the _layout folder and called *.html).

    <html>
      <head>
        <!-- ... -->
        <style>
          .custom-table th, .custom-table td {
            padding: 1px;
            // any style you want ...
          }
        </style>
      </head>
      <body>
        ...
      </body>
    <html>
    

    Or even you using the element-processor of jekyll-spaceship to modify the element style (Click for more usage details), adding the config to your _config.yml as below:

    jekyll-spaceship:
      element-processor:
        css:
          - [.custom-table th, .custom-table td]:  # Select the th, td nodes of custom table
              props:                               # Replace element properties
                style:                             # Add style attributes (Hash Style)
                  padding: 1px                     # Add padding 
                  # font-size: '1.2em'             # Add any style that you want