Guide to implementing responsive table layout using CSS
Introduction:
With the popularity of mobile devices, modern web design has got rid of the limitations of fixed layout. Instead move to responsive layouts. Responsive layout allows web pages to automatically adapt to different devices and provide a better user experience. In this article, we will introduce how to use CSS to implement responsive table layout, with specific code examples.
.container { position: relative; } table { width: 100%; }
@media screen and (max-width: 600px) { table, thead, tbody, th, td, tr { display: block; } tr { margin-bottom: 10px; } th, td { display: inline-block; } th { font-weight: bold; } }
In the above code, we use @media query to set the style when the screen width is less than or equal to 600px. In this case, we set the display attribute of the table-related elements to block so that they are arranged vertically. At the same time, we set the display attribute of the cells in the table header and table body to inline-block so that they are arranged horizontally.
@media screen and (max-width: 600px) { /* Other styles */ th, td { font-size: 14px; line-height: 1.5; padding: 5px; } }
In the above code, we set the font size, line height and cell margins in the @media query when the screen width is less than or equal to 600px. You can adjust it according to your needs.
Conclusion:
With the above CSS code examples, we can easily implement responsive table layout. In this way, whether the user is using a mobile phone, tablet or computer, the form can automatically adapt to different devices and provide a better user experience. Hope this article can be helpful to you.
References:
The above is the detailed content of A guide to implementing responsive table layout using CSS. For more information, please follow other related articles on the PHP Chinese website!