A practical way to make the form beautifully displayed on the mobile phone
A few days ago, I received a warningGoogle Search Console. It detects mobile usability issues on a page where I have a large table.
This is the table that gave me the problem:
On mobile devices, it renders poorly:
Bad user experience, and errors in Google Search Console. If there is something I don't want, it is an error/warning. It won't work when I can solve it.
Hugo, the static website generator I use, just add one<style>
Mark it in the markdown file. convenient.
Therefore, I started looking for a good way to make the watch responsive. I saw this very good article on CSS Tricks:Response data sheet. Since 2011, it can still be used normally!
The trick is this: we want the table to be displayed as a block element, not a table in the traditional CSS sense. We hide all table headers by moving all table headers out of view, and insert a new block in the table, each row will have its own set of table headers, as shown below:
This is the code that implements the above design:
@media
only screen and (max-width: 1500px) {
table, thead, tbody, th, td, tr {
display: block;
}
thead tr {
position: absolute;
top: -9999px;
left: -9999px;
}
tr { border: 1px solid #ccc; }
td {
border: none;
border-bottom: 1px solid #eee;
position: relative;
padding-left: 200px;
margin-left: 150px;
}
td:before {
position: absolute;
top: 12px;
left: 6px;
width: 200px;
padding-right: 40px;
white-space: nowrap;
margin-left: -150px;
}
td:nth-of-type(1):before { content: "Option"; }
td:nth-of-type(2):before { content: "Description"; }
td:nth-of-type(3):before { content: "Type"; }
td:nth-of-type(4):before { content: "Default";}
}
The important thing you need to customize to make your own table responsive is the last 4 rows-you need to enter the title of each "column", and if you have more columns, you need to add more. Or, if the number is small, delete it.
The other thing is that the new "title" will take up space in each row. I added one150px
Margin, you need to quote 2 times: once formargin-left: 150px
insidetd
And asmargin-left: -150px
insidetd: before
.
Finally, you need to determine when to start the new layout. When the page is smaller than 1500px, I activate it because the table is large. I can also set it as the default behavior instead of displaying a normal table on a big screen, but so far, I think the problem has been solved.
I hope this helps.
Download mine for freeCSS Manual
More CSS tutorials:
- Flexbox guide
- CSS grid tutorial
- CSS variables (custom attributes)
- Introduction to PostCSS
- CSS guarantees metallicity
- How to center an element using CSS
- CSS system fonts
- How to print HTML with styles
- Getting started with CSS transition
- CSS animation tutorial
- Introduction to CSS
- CSS guide
- How to set up Tailwind with PurgeCSS and PostCSS
- Tail wind cheat sheet
- How to continuously rotate an image using CSS
- Use CSS to make the table responsive
- How to debug CSS by halving
- CSS selector
- CSS cascade
- CSS specificity
- CSS attribute selector
- CSS colors
- CSS Unit
- CSS url()
- CSS typography
- CSS Box model
- CSS position properties
- CSS media queries and responsive design
- CSS function query
- CSS conversion
- How to style a list with CSS
- CSS vendor prefix
- CSS inheritance
- CSS pseudo-classes
- CSS pseudo elements
- Style HTML tables with CSS
- CSS Display property
- CSS calc() function
- CSS border
- Use @import to import CSS files
- CSS error handling
- CSS filter
- CSS Box size
- CSS background
- CSS comments
- CSS fonts
- CSS padding
- CSS float property and clear
- CSS normalization
- CSS z-index property
- How to disable text selection using CSS
- How to use CSS to place items at the bottom of their container
- How to invert colors using CSS
- Responsive front tags in CSS
- Responsive YouTube video embedding
- What is a good CSS breakpoint value for responsive design?
- How to align the center in Flexbox