Last active
January 10, 2021 14:32
-
-
Save punkrats/adb3bbbac5afba0ac6087f3b0e518d4a to your computer and use it in GitHub Desktop.
Table with vertical column titles
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <title>Table with vertical column titles</title> | |
| <style> | |
| body { | |
| font-family: Arial; | |
| } | |
| .features { | |
| display: table; | |
| width: 100%; | |
| position: relative; | |
| } | |
| #table-title>*{ | |
| margin: 0; | |
| } | |
| .features-row { | |
| width: 100%; | |
| display: table-row; | |
| } | |
| .features-row>* { | |
| display: table-cell; | |
| padding: 10px; | |
| box-sizing: border-box; | |
| } | |
| .features-body.features-row>* { | |
| border-left: 1px solid gray; | |
| border-top: 1px solid gray; | |
| } | |
| .feature-label { | |
| border-left: 0 !important; | |
| } | |
| .column-title { | |
| overflow: hidden; | |
| vertical-align: bottom; | |
| border: 0; | |
| padding: 10px; | |
| border-top: 1px solid gray; | |
| transform-origin: top left; | |
| transform: rotate(-90deg) translate(-100%); | |
| white-space: nowrap; | |
| } | |
| .rotate-outer { | |
| position: relative; | |
| } | |
| .feature-value { | |
| padding: 10px; | |
| width: 40px; | |
| text-align: center; | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <div class="features"> | |
| <div class="features-head features-row"> | |
| <div id="table-title"> | |
| <h1>This is a very, very long table title</h1> | |
| </div> | |
| <div class="column-title"> | |
| Some title | |
| </div> | |
| <div class="column-title"> | |
| Another | |
| </div> | |
| <div class="column-title"> | |
| A third one | |
| </div> | |
| </div> | |
| <div class="features-body features-row"> | |
| <div class="feature-label">Something</div> | |
| <div class="feature-value">0</div> | |
| <div class="feature-value">1</div> | |
| <div class="feature-value">1</div> | |
| </div> | |
| <div class="features-body features-row"> | |
| <div class="feature-label">Something else with a very long description</div> | |
| <div class="feature-value">0</div> | |
| <div class="feature-value">1</div> | |
| <div class="feature-value">1</div> | |
| </div> | |
| <div class="features-body features-row"> | |
| <div class="feature-label">Another thing</div> | |
| <div class="feature-value">0</div> | |
| <div class="feature-value">0</div> | |
| <div class="feature-value">1</div> | |
| </div> | |
| </div> | |
| <script> | |
| const table = document.getElementsByClassName('features')[0] | |
| const titles = document.getElementsByClassName('column-title') | |
| // Detect max width of plan labels to apply it as height. | |
| let maxWidth = 0 | |
| for (const el of titles) { | |
| let width = el.offsetWidth | |
| el.style.position = 'absolute' // remove them out of scope now | |
| if (width > maxWidth) { | |
| maxWidth = width | |
| } | |
| } | |
| const labelWidth = document.getElementsByClassName('feature-label')[0].offsetWidth | |
| const valueWidth = document.getElementsByClassName('feature-value')[0].offsetWidth | |
| // Position table title at the top left corner. | |
| const title = document.getElementById('table-title') | |
| title.style.position = 'absolute' | |
| title.style.top = 0 | |
| title.style.width = labelWidth + 'px' | |
| // Extend maxWidth if the height of the title exceeds it. | |
| let titleHeight = title.offsetHeight | |
| if (titleHeight > maxWidth) { | |
| maxWidth = titleHeight | |
| } | |
| // Apply maxWidth as padding to the table to make space for vertical titles. | |
| table.style.paddingTop = maxWidth + 'px' | |
| // Position vertical titles in each respective column. | |
| let count = titles.length | |
| for (const el of titles) { | |
| el.style.width = maxWidth + 'px' | |
| el.style.right = (valueWidth*count - maxWidth) + 'px' | |
| el.style.top = 0 | |
| count -= 1 | |
| } | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment