当前位置:网站首页>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 ?
边栏推荐
- 【题解】Educational Codeforces Round 82
- jmeter脚本参数化
- Solve the problem that El radio group cannot be edited after echo
- LeetCode 209. 长度最小的子数组
- qml 弹窗框架,可定制
- About text selection in web pages and counting the length of selected text
- Printf function and scanf function in C language
- LeetCode 209. Minimum length subarray
- info [email protected]: The platform “win32“ is incompatible with this module.
- AtCoder Beginner Contest 254
猜你喜欢
LeetCode 209. Minimum length subarray
【apipost】使用教程
微信小程序使用towxml显示公式
实用调试技巧
Actual combat sharing of shutter screen acquisition
obsidian安装第三方插件——无法加载插件
解决el-radio-group 回显后不能编辑问题
[noi simulation] Elis (greedy, simulation)
Thoroughly master prototype__ proto__、 Relationship before constructor (JS prototype, prototype chain)
IE 浏览器正式退休
随机推荐
Base64 编码原来还可以这么理解
实用调试技巧
mathjax 入门(web显示数学公式,矢量的)
OpenCV调用USB摄像头的点滴
AtCoder Beginner Contest 254
C语言中的算术运算及相关练习题
Arithmetic operations and related exercises in C language
Solve the problem that El radio group cannot be edited after echo
fatal: unsafe repository is owned by someone else 的解决方法
qml 弹窗框架,可定制
复用和分用
天猫商品详情接口(APP,H5端)
Makefile 分隔文件名与后缀
Socket and socket address
Slashgear shares 2021 life changing technology products, which are somewhat unexpected
【题解】Educational Codeforces Round 82
Xilinx Vivado set *.svh as SystemVerilog Header
Reuse and distribution
C# richTextBox控制显示最大行数
解决el-radio-group 回显后不能编辑问题