Search code examples
javascriptcsshtml-tablejavascript-marked

markedjs table has no styles


I am using the markedjs markdown parser on my website, but the table syntax didn't style the right way (no borders and stripes). My code:

<!doctype html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>Marked in the browser</title>
    <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
</head>
<body>
<div id="content"></div>
<script>

    document.getElementById('content').innerHTML =
        marked(`
First Header | Second Header
------------ | -------------
Content Cell | Content Cell
Content Cell | Content Cell
        `);
</script>
</body>
</html>


Solution

  • This is not a marked issue, but a CSS issue. Since you are not specifying any styling for your table, you are not getting any :) .

    Edit Here is an example that uses Bootstrap with markedjs.

    Non-Bootstrap example:

    <!doctype html>
    <html>
    <head>
        <meta charset="utf-8"/>
        <title>Marked in the browser</title>
        <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
        <style>
            table { border-collapse: collapse; }
            tr { border-bottom: solid 1px black; }
            tr:nth-child(even) {background-color: #f2f2f2;}
        </style>
    </head>
    <body>
    <div id="content"></div>
    <script>
    
        document.getElementById('content').innerHTML =
            marked(`
    First Header | Second Header
    ------------ | -------------
    Content Cell | Content Cell
    Content Cell | Content Cell
            `);
    </script>
    </body>
    </html>

    • table { border-collapse: collapse; } enables per-tr borders, per this answer.
    • tr { ... } gives you the lines between rows.
    • tr:nth-child(even) { ... } gives you the shading.