Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Style table as ASCII-art table using CSS

Using CSS, I want to style a table "ASCII art" like, such as this one:

+------+---------+----+
| Jill | Smith   | 50 |
+------+---------+----+
| Eve  | Jackson | 94 |
+------+---------+----+
| John | Doe     | 80 |
+------+---------+----+

<table>
 <tr>
    <td>Jill</td>
    <td>Smith</td> 
    <td>50</td>
  </tr>
  <tr>
    <td>Eve</td>
    <td>Jackson</td> 
    <td>94</td>
  </tr>
</table>

For more info on these tables, check out this table generator: Format Text As Table

If possible, it would be cool to just use CSS and not to hard code any of the border characters.


My attempt

I tried using border-image, but the results are not quite what I want:

My CSS:

* {
    font-family: "Ubuntu Mono";
    size: 10px;
    padding: 0;
    margin: 0;
}
table {
    border-spacing: 0;
    border-width: 8px;
    border-image: url("border.png") 16 8 round;
}

border.png:

border.png

Result:

Version 1

As you can see, the top and bottom border is not displayed. Also, no lines in between the cells are displayed.

Using border-width: 16px:

Version 2

Now, the top and bottom border is displayed, but the left and right border is streched.

Another thing I do not like about using my method is that the image does not properly respond to the font size.

like image 738
Frithjof Avatar asked Oct 13 '14 14:10

Frithjof


People also ask

Can you use CSS to style a table?

CSS provides a number of attributes for styling tables. These attributes allow you to—among other things—separate cells in a table, specify table borders, and specify the width and height of a table. This tutorial will discuss, with examples, how to style a table using CSS.


1 Answers

Here is a CSS solution that uses pseudo elements. It works as follows:

  • An extra element is required; so that we have enough pseudo elements for one-row tables.
  • An image is required for cell corners.
  • The image is positioned at top-left corner of all cells.
  • The image is positioned at bottom-right corner of right column and bottom row cells.
  • The image is positioned at top-right and bottom-left corner of the table.

Note: the results are off by 1px in FireFox.

.ascii-table {
    font: medium/1 monospace;
    margin: 1em;
    display: inline-block;
    position: relative;
}
.ascii-table table {
    border-collapse: collapse;
}
.ascii-table td {
    border: 1px dashed #000;
    padding: .5em;
    position: relative;
}
.ascii-table tr td:before {
    position: absolute;
    width: 15px;
    height: 15px;
    content: "";
    background-image: url(http://i.stack.imgur.com/2OGdZ.png);
    background-color: rgba(255, 127, 0, .4);
    top: -8px;
    left: -8px;
}
.ascii-table tr td:last-child:after, .ascii-table tr:last-child td:after {
    position: absolute;
    width: 15px;
    height: 15px;
    content: "";
    background-image: url(http://i.stack.imgur.com/2OGdZ.png);
    background-color: rgba(255, 63, 63, .4);
    bottom: -8px;
    right: -8px;
}
.ascii-table:before {
    position: absolute;
    width: 15px;
    height: 15px;
    content: "";
    background-image: url(http://i.stack.imgur.com/2OGdZ.png);
    background-color: rgba(255, 127, 0, .8);
    top: -7px;
    right: -7px;
}
.ascii-table:after {
    position: absolute;
    width: 15px;
    height: 15px;
    content: "";
    background-image: url(http://i.stack.imgur.com/2OGdZ.png);
    background-color: rgba(255, 63, 63, .8);
    bottom: -7px;
    left: -7px;
}
<div class="ascii-table">
    <table>
        <tr>
            <td>Jill</td><td>Smith</td><td>50</td>
        </tr>
    </table>
</div>
<div class="ascii-table">
    <table>
        <tr>
            <td>Jill</td><td>Smith</td><td>50</td>
        </tr>
        <tr>
            <td>Eve</td><td>Jackson</td><td>94</td>
        </tr>
    </table>
</div>
<div class="ascii-table">
    <table>
        <tr>
            <td>Jill</td><td>Smith</td><td>50</td>
        </tr>
        <tr>
            <td>Eve</td><td>Jackson</td><td>94</td>
        </tr>
        <tr>
            <td>John</td><td>Doe</td><td>75</td>
        </tr>
    </table>
</div>
like image 152
Salman A Avatar answered Sep 19 '22 13:09

Salman A