Reputation: 2264
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.
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
:
Result:
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
:
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.
Upvotes: 9
Views: 3039
Reputation: 272266
Here is a CSS solution that uses pseudo elements. It works as follows:
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(https://i.sstatic.net/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(https://i.sstatic.net/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(https://i.sstatic.net/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(https://i.sstatic.net/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>
Upvotes: 10