当前位置:网站首页>Table responsive layout tips
Table responsive layout tips
2022-07-02 15:03:00 【zx_ twenty million two hundred and twenty thousand one hundred 】
today , I met a very interesting question , A group friend asked me , Only use CSS, Whether such a responsive layout effect can be achieved :

Simply analyze the effect :
When the screen viewport is wide , As a whole Table The style of
When the screen viewport width is small , primary Table Each row of data in the is split into one Table To display
An interesting responsive layout , Let the information get a good display under the small screen .
that , Only use CSS Words , Can such a layout be realized ? The answer is Tolerable .
First , It will definitely use Media query , It's not hard to see that . in addition , Let's look at each set of data after splitting :

There will be a group of the original whole Table Header information when , The main difficulty is here , How do we split it into sub - items one by one Table Display time , At the same time, these header information ?
Implementation of the basic structure
First , Let's first implement the... Under the conventional widescreen HTML And the corresponding CSS.
Relatively simple , There is nothing special here , Use <table> Label or use div、ul Wait for the tag to simulate a table .
<table> <caption>Lorem ipsum !</caption> <thead> <tr> <th>Account</th> <th>Due Date</th> <th>Amount</th> <th">Period</th> </tr> </thead> <tbody> <tr> <td data-label="Account">Visa - 3412</td> <td data-label="Due Date">04/01/2016</td> <td data-label="Amount">$1,190</td> <td data-label="Period">03/01/2016 - 03/31/2016</td> </tr> // ... Repeat groups </tbody> </table> Get such a simple Table:

Use media query to put a single Table Split into multiple
The next step is simple , Set the appropriate threshold ( It depends on the actual business situation ), Use media query to put a single Table Split into multiple children Table.
@media screen and (max-width: 600px) {
table {
border: 0; } table thead {
display: none; } table tr {
display: block; margin-bottom: 10px; } table td {
border-bottom: 1px solid #ddd; display: block; } } What is done here is also very simple :
Use the media to search , Set the screen width to be less than 600px The style of
Remove from the original table <thead> Header , Just hide it
Put the original line <tr>, Set to display: block, And set a bottom margin , Make each of them separate
In the original line <td>, Set to display: block, such , They will be arranged vertically , Make every one <tr> Form a new child table
good , such , Then the screen width is less than 600px when , We got this one Table:

With the help of pseudo elements and their properties , Display header information
Next step , That is the most critical step , How do we get there table Each line , That is to say <td> Inside , Show the original header information ?
It's actually very simple here , It simply uses pseudo elements , Extremely readable HTML Small feature implementation of tag attributes .
We just need to make a simple change to the code , For each <td> Of HTML, Bring the corresponding header column description information :
<table> // The information above is consistent <tbody> <tr> <td data-label="Account">Visa - 3412</td> <td data-label="Due Date">04/01/2016</td> <td data-label="Amount">$1,190</td> <td data-label="Period">03/01/2016 - 03/31/2016</td> </tr> <tr> <td scope="row" data-label="Account">Visa - 6076</td> <td data-label="Due Date">03/01/2016</td> <td data-label="Amount">$2,443</td> <td data-label="Period">02/01/2016 - 02/29/2016</td> </tr> // ... Every tr Do the same </tbody> </table> next , With the help of td The pseudo elements of , Display the header information :
@media screen and (max-width: 600px) {
// ... bring into correspondence with table td {
position: relative; display: block; text-align: right; } table td::before {
position: absolute; left: 10px; righht: 0; content: attr(data-label); } } here , Our core knowledge point is to use the pseudo element of the element to be able to content In the attribute , Read it HTML Attribute contents within the element , And show the knowledge points .
Suppose a HTML Labels are defined as :<div data-msg="ABC">
Then the div If the corresponding pseudo element is set content: attr(data-msg) , You can read data-msg Value , amount to content:"ABC"
such , We are on a small screen , You get such an effect :

Last
This feature of pseudo elements can be applied in many places in daily effects , A very small skill , Have you learned ?
边栏推荐
- STM32 standard firmware library function name memory (II)
- 【空间&单细胞组学】第1期:单细胞结合空间转录组研究PDAC肿瘤微环境
- MFC CString to char*
- 报错:npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
- 电脑怎么设置扬声器播放麦克风的声音
- LeetCode 2310. The number of digits is the sum of integers of K
- 【C语言】详解指针的初阶和进阶以及注意点(1)
- buuctf-pwn write-ups (7)
- Database connection pool and data source
- Large top heap, small top heap and heap sequencing
猜你喜欢

STM32 library function for GPIO initialization

Dragonfly low code security tool platform development path

CTO如何帮助业务?

CodeCraft-22 and Codeforces Round #795 (Div. 2)D,E

CodeCraft-22 and Codeforces Round #795 (Div. 2)D,E

Yolov6 training: various problems encountered in training your dataset

Introduction to C language -- array

forEach的错误用法,你都学废了吗

Why can't programmers who can only program become excellent developers?

MathML to latex
随机推荐
LeetCode - 搜索二维矩阵
taobao.trades.sold.get-查询卖家已卖出的交易数据(根据创建时间),淘宝店铺卖出订单查询API接口,淘宝R2接口,淘宝oAuth2.0交易接口代码分享
Error: NPM warn config global ` --global`, `--local` are deprecated Use `--location=global` instead.
C# 线程传参
Xilinx Vivado set *. svh as SystemVerilog Header
为什么只会编程的程序员无法成为优秀的开发者?
MFC CString 转 char*
mathML转latex
Li Chuang EDA learning notes 15: draw border or import border (DXF file)
[QNX Hypervisor 2.2用户手册]6.3 Guest与外部之间通信
Mfc a dialog calls B dialog function and passes parameters
Jenkins Pipeline 应用与实践
It's no exaggeration to say that this is the most user-friendly basic tutorial of pytest I've ever seen
Factal: Unsafe repository is owned by someone else Solution
数据库内容输出有问题怎么解决
tmall. product. schema. Get (product information acquisition schema acquisition), Taobao store upload commodity API interface, Taobao commodity publishing interface, Taobao commodity upload API interf
Yolov6 training: various problems encountered in training your dataset
微信小程序使用towxml显示公式
C thread transfer parameters
数据库连接池和数据源