I am writing a Hugo theme and have difficulty creating horizontal scrollbar for table.
Problem description and reproducible steps:
opened 11:23PM - 15 Jul 24 UTC
## What I expect
- https://example.docsy.dev/docs/tasks/task/
- https://demo… .stack.jimmycai.com/p/markdown-syntax-guide/
The tables on these pages scroll horizontally when device's width is low (e.g. smart phone). I want to create
horizontal scrollbar like those.
## What happened
I am creating responsive horizontal scroll bar for `<table>`.
```
table {
display: block;
// width: 100%; // This doesn't work. Tables extend beyond <body>
width: 300px; // This works but readers use different devices with different width
overflow-x: auto;
word-break: normal;
}
```
This code works but fixed width is not ideal since readers use different devices.
<details>
<summary>Fennec browser (Android) screenshot</summary>

</details>
<details>
<summary>Chrome (Linux) screenshot</summary>

</details>
---
When I use `width: 100%`, table overflows and layout is broken.
<details>
<summary>Fennec browser (Android) screenshot</summary>

</details>
## Reproduce
```
git clone https://github.com/CyrusYip/hugo-theme-yue
cd hugo-theme-yue
git switch table-bug
git reset --hard eb4863
# use hugo 0.128.1
hugo server --navigateToChanged --bind 0.0.0.0
# if you have npm and nodejs installed, run this
npm i
npx hugo server --navigateToChanged --bind 0.0.0.0
```
Visit http://localhost:1313/en/posts/post-5/ on mobile and desktop.
Modify `/assets/sass/_style.scss`, uncomment `width: 100%` and comment `width: 300px`, now you will see the problem. All style files are in `/assets/sass`.
Arif
July 16, 2024, 12:06am
2
You need to wrap the table in a <div>
and style that with a class and CSS to make the table scrollable.
<div class="table-wrapper">
<table>
<!-- other table stuff here -->
</table>
</div>
All this depends on how you want the tables to be presented (markdown, shortcode or using replaceRE , the latter which is applicable for wrapping all tables).
.table-wrapper {
overflow-x: auto;
display: block;
}
1 Like
I found the culprit. I used grid layout to keep footer at the bottom, which caused the problem. This was totally unexpected.
// Stretch <main> to ensure <footer> stays at bottom
.body {
display: grid;
grid-template-rows: auto 1fr auto; // <header> <main> <footer>
min-height: 100vh; // fallback value if dvh is not supported
min-height: 100dvh; // Avoid scrollbar on mobile
}
After removing this code, both wrapper solution and non-wrapper solution work for my theme.
wrapper solution:
.table-wrapper {
overflow-x: auto;
display: block;
}
table {
word-break: normal;
}
Non-wrapper solution:
table {
display: block;
width: 100%;
overflow-x: auto;
word-break: normal;
}
@Arif Thank you for sharing wrapper solution.
system
Closed
July 18, 2024, 12:53am
4
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.