Preface
One issue with
Hopefully one solution is moving from
The Solution
My honor to share my idea and solution with you great developers, it's easy and simple.
Idea is simple, first we need to have a
- Manf. Name
- Awesomeness
- Models
- Origin
For having a css to serve 4 element inside horizontally(columns), we would use
Columns Turns To Rows
When screen goes small, so your table/grid may need more data to render the data, and when they don't get enough space, you get broken layout.
Simply we would show the columns of the a row in each line, so it won't cause any issue as row will need less
Better we say, we turn out the table into 1 column table for mobile.
Desktop View
First let's have desktop view working, becasue it's the easiest part.
So we want a layout as table row to host four cells, easy, sounds like a easy job with
Considering have a CSS rule for row, to display as grid, and comes as 4 elements as column(cell), this could be something liek following.
.row_grid{
display:grid;
grid-template-columns: 0.25fr 0.25fr 0.25fr 0.25fr;
}
Just put some data on this grid element, and it will place them by 4 equal portion(cell), easy.
Mobile View
For mobile view, you need to turn that 4 column grid into 1 column grid, and simply a 1 column row is just a simple block.
So actually there is no any specific CSS for mobile part, but for mobile you should turn it into 4-column element.
Easy! just need one media query for desktop(or any size it looks good) and let it goes as 4-column row.
@media (min-width: 5.1in) {
.row_grid{
display:grid;
grid-template-columns: 0.25fr 0.25fr 0.25fr 0.25fr;
}
}
Table Head
The first row of each table is usually about the column names(just like the list mentioned above). But when columns turn into rows, that top row about the column names became a little nonsense and redundant. So what's idea?
My idea is about hiding the top row about the column names when in mobile. You can hide an element very easy using
NOTE: Hiding an elements using
Alright, so let's have a good table header row where it's there for desktop view, and it's gon for mobile view. Again we get some hand of media query for this.
.row_head{
display:none;
font-width:bold;
font-size:1.1em;
}
@media (min-width: 5.1in) {
.row_head,.row_grid{
display:grid;
grid-template-columns: 0.25fr 0.25fr 0.25fr 0.25fr;
}
}
Cell/Data Legend
In mobile ,now there is no a row to specifies the column names, we would go for a very tricky way to show a very small legend that explain about the cell/data.
Just like we hide the table header row in mobile, we would show the small legend next to the cell/data.
The CSS part is easy, using the media query to show/hide the element in different views, considering.
.inline_hn{
font-size: 0.9em;
border: 1px dashed #00ce00;
background-color: #00ff004d;
border-radius: 0.3em;
padding: 0.1em 0.3em;
color: gray;
text-align:right;
font-family:monospace;
margin-right:0.5em;
}
@media (min-width: 5.1in) {
.inline_hn{
display:none;
}
}
Row Border Splitter
Indeed having a line after each border to work as border between top and bottom row make the table more easier to read.
For both desktop and mobile, this could be great, all we need is a simple border bottom.
Please note the last row may not need that border bottom since it may overflow on table border and make the work a little not pro. The tip is using
.row_grid{
border-bottom:1px solid lime;
}
.row_grid:last-child{
border-bottom-width:0;
}
Looks very easy, all rows come with border, except the last one. Cool.
Cell Border Splitter
Just like we give each row border bottom, we would give the cells right border too. Why not.
Exactly the same way we did for rows, it works for cell too. but remember for mobile there is no cell! so there is no border needed at right or left.
@media (min-width: 5.1in) {
.cell:nth-child(1n+2){
border-left:1px solid lime;
}
}
Odd/Even Cell Highlighting
To make the table even more easier to read is highlight the odd/even cells with another colour. So easy, we would get some hand of
.row_grid:nth-child(even){
background-color:#0f03;
}
Putting All Together
Now the final part, just add some more fancy CSS for your table, cell ,row and putting all together, considering.
See the Pen Responsive table idea by NULL_dev (@NULL_dev11) on CodePen.
TL;DR Version
Open the codepen page and resize the page to see how does it work.
That's it, programming is easy, happy coding.