+++ title = "Table Styling with ATTR_HTML and ATTR_CSS" tags = ["table", "attr_html", "attr_css", "css"] categories = ["upstream"] draft = false +++ This _table styling_ feature overcomes a known limitation in Hugo/Blackfriday (_Blackfriday_ Issue #[404](https://github.com/russross/blackfriday/issues/404)) that doesn't allow wrapping Markdown elements with `
` tags (so that a user can assign classes, etc. to those `div` blocks). So this feature had to be implemented at the expense of a nasty hack demonstrated [here](https://github.com/kaushalmodi/ox-hugo/issues/93#issue-270108567). --- HTML5 does not allow `align`, `width`, etc. attributes **within** the `table` tag [ [Ref](https://www.w3.org/TR/2011/WD-html-markup-20110113/table.html#table-constraints) ]. So instead `ox-hugo` simply wraps the table with a `div` with a `class`. The table can then be customized using CSS, either via the `#+attr_css` attribute above the tables, or by putting verbatim CSS in `#+begin_export html` blocks. See below examples. ## Table with only the class specified {#table-with-only-the-class-specified}
| h1 | h2 | h3 | |-----|-----|-----| | abc | def | ghi |
Above table is wrapped in a `my-table` class. Just doing that won't bring any presentation changes to the table.. you'd need to add some CSS that customizes `.my-table table`. ## Table with only a CSS attribute specified {#table-with-only-a-css-attribute-specified}
| h1 | h2 | h3 | |-----|-----|-----| | abc | def | ghi |
Above table get wrapped in the auto-generated class `table-nocaption`. The specified CSS attribute is auto-set for `.table-nocaption table`. ## Table with both class and CSS attribute specified {#table-with-both-class-and-css-attribute-specified}
| h1 | h2 | h3 | |-----|-----|-----| | abc | def | ghi |
Above table get wrapped in the specified class `my-table-2`. The specified CSS attribute is auto-set for `.my-table-2 table`. ## Table with caption, and CSS attributes specified {#table-with-caption-and-css-attributes-specified}
Table 1: Table with caption, with left-aligned text
| h1 | h2 | h3 | |----------|----------|----------| | abcdefgh | ijklmnop | qrstuvwx |
Above table get wrapped in the auto-generated class `table-1` (_"1", because this is the first table with a caption on this page._). The specified CSS attribute is auto-set for `.table-1 table`.
Table 2: Table with caption, with right-aligned text
| h1 | h2 | h3 | |----------|----------|----------| | abcdefgh | ijklmnop | qrstuvwx |
Table 3: Table with caption, with center-aligned text
| h1 | h2 | h3 | |----------|----------|----------| | abcdefgh | ijklmnop | qrstuvwx |
## Table with caption, and both class and CSS attributes specified {#table-with-caption-and-both-class-and-css-attributes-specified}
Table 4: Table with caption, class and CSS attributes specified
| h1 | h2 | h3 | |-----|-----|-----| | abc | def | ghi |
Above table get wrapped in the specified class `my-red-bordered-table`. The specified CSS attribute is auto-set for `.my-red-bordered-table table`. Here's another table with the exact same class as that of the above table. So the CSS properties do not need to be specified again. Below table will also show up with a red border.
| h1 | h2 | h3 | |-----|-----|-----| | jkl | kmo | pqr |
## Table with verbatim CSS {#table-with-verbatim-css} There could be times when the CSS couldn't be simply specified using the `#+attr_css` attribute. In those cases, simply set the table class using `#+attr_html`, and put the CSS in the `#+begin_export html` block.
Table 5: Table with verbatim CSS
| h1 | h2 | h3 | |-----|-----|-----| | abc | def | ghi |
## More table styling examples {#more-table-styling-examples} [Source: _css-tricks.com_](https://css-tricks.com/complete-guide-table-element/) ### Basic styling {#basic-styling} #### Uncollapsed borders {#uncollapsed-borders}
Table 6: Table with uncollapsed borders
| Name | ID | Favorite Color | |------|-------|----------------| | Jim | 00001 | Blue | | Sue | 00002 | Red | | Barb | 00003 | Green |
#### Collapsed borders {#collapsed-borders} ### Two Axis Tables {#two-axis-tables}
Table 8: Table with 1st row and 1st column highlighted
| 1 | 2 | 3 | 4 | 5 | |---|----|----|----|----| | 2 | 4 | 6 | 8 | 10 | | 3 | 6 | 9 | 12 | 15 | | 4 | 8 | 12 | 16 | 20 | | 5 | 10 | 15 | 20 | 25 |
### Sane Table {#sane-table} [Ref](https://css-tricks.com/complete-guide-table-element/#article-header-id-17)
Table 9: Sane Table — with minimal styling
| Name | ID | Favorite Color | |------|-------|----------------| | Jim | 00001 | Blue | | Sue | 00002 | Red | | Barb | 00003 | Green |
### Zebra Striping {#zebra-striping}
Table 10: Table with zebra striping
| Last Name | First Name | Email | Due | Web Site | |-----------|------------|-----------------------|---------|-------------------------| | Smith | John | jsmith@gmail.com | $50.00 | | | Bach | Frank | fbach@yahoo.com | $50.00 | | | Doe | Jason | jdoe@hotmail.com | $100.00 | | | Conway | Tim | tconway@earthlink.net | $50.00 | |
### Highlighting on Hover {#highlighting-on-hover} #### Highlight Cell {#highlight-cell}
Table 11: Table where the hovered-upon cell highlights
| Last Name | First Name | Email | Due | Web Site | |-----------|------------|-----------------------|---------|-------------------------| | Smith | John | jsmith@gmail.com | $50.00 | | | Bach | Frank | fbach@yahoo.com | $50.00 | | | Doe | Jason | jdoe@hotmail.com | $100.00 | | | Conway | Tim | tconway@earthlink.net | $50.00 | |
#### Highlight Row {#highlight-row}
Table 12: Table where the hovered-upon row highlights
| Last Name | First Name | Email | Due | Web Site | |-----------|------------|-----------------------|---------|-------------------------| | Smith | John | jsmith@gmail.com | $50.00 | | | Bach | Frank | fbach@yahoo.com | $50.00 | | | Doe | Jason | jdoe@hotmail.com | $100.00 | | | Conway | Tim | tconway@earthlink.net | $50.00 | |
### Blur non-hovered Rows {#blur-non-hovered-rows} _Let's-go-ballistic-with-CSS_ Edition :smile:
Table 13: Table where rows except the hovered-upon get blurred
| Last Name | First Name | Email | Due | Web Site | |-----------|------------|-----------------------|---------|-------------------------| | Smith | John | jsmith@gmail.com | $50.00 | | | Bach | Frank | fbach@yahoo.com | $50.00 | | | Doe | Jason | jdoe@hotmail.com | $100.00 | | | Conway | Tim | tconway@earthlink.net | $50.00 | |