内容简介:Planning the responsive behavior of a component can be tricky, particularly when the component layout is highly affected by its content.For example, let's consider a table component. You may decide to use two different layouts: one optimized for smaller sc
Planning the responsive behavior of a component can be tricky, particularly when the component layout is highly affected by its content.
For example, let's consider a table component. You may decide to use two different layouts: one optimized for smaller screens ( state-a layout), the other for bigger screens ( state-b layout).
You would then need to decide the breakpoint for the change of layout and set it in CSS using a media query.
However, the same table component could have two columns or twenty.
If your table has a low number of columns, you may decide to change the layout at a small breakpoint:
For a table with more columns or richer content, instead, you may want to change it later to make sure it does not look too crowded on small screens:
Ideally, you should find a breakpoint that works for both of them (and all the other tables you have on your website). You could use a .table
class to define the style of the state-a layout, and use a media query to overwrite this style for the state-b layout:
<table> <!-- table content --> </table> <style> .table { /* state-a layout style */ @media (min-width: 600px) { /* state-b layout style */ } } </style>
This solution may not be ideal because the breakpoint you choose is a compromise: you could end up with some tables that look too crowded and others too sparse. Even if you find a solution that seems to work with today's tables, it could easily break with tomorrow's.
A possible alternative would be to define responsive class modifiers (classes that share the same style but target different breakpoints) to have the option of triggering the layout change at different breakpoints.
If we consider an example with two breakpoints (small and medium), you would have:
.table { /* state-a layout style */ } /* small breakpoint */ @media (min-width: 600px) { .table--state-b\@sm { /* state-b layout style */ } } /* medium breakpoint */ @media (min-width: 1000px) { .table--state-b\@md { /* state-b layout style */ } }
then you can apply those modifiers to different <table>
s based on their content:
<!-- :point_down: switch layout at a small breakpoint --> <table/cdn-cgi/l/email-protection" data-cfemail="c1b5a0a3ada4ececb2b5a0b5a4eca381b2ac">[email protected]</a>"></table> <!-- :point_down: switch layout at a medium breakpoint --> <table/cdn-cgi/l/email-protection" data-cfemail="433722212f266e6e30372237266e21032e27">[email protected]</a>"></table>
The code defined in the [email protected]
class is the same as the one in the [email protected]
. Remember, those two classes are used to create the same layout; it's only applied at different breakpoints.
This approach has two main disadvantages. The first one is the maintainability of your code: if you need to make changes to the state-b layout, you would need to update two different classes ( [email protected]
and [email protected]
). This is something you can solve using a CSS pre-processor (e.g., using SASS mixins).
The second issue is having the CSS code for state-b repeated multiple times in the final CSS (twice if you have two modifiers, but it could be more if you need additional variations!).
Repeat this for all the components and the different media queries, and this could cause a significant increase in your CSS file size.
While working on theTable category of the Components Library at CodyHouse, we ended up using a different approach; we defined a class for the state-b layout:
.table { /* state-a layout style */ } .table--state-b { /* state-b layout style */ }
We then defined a --table-layout
CSS custom property inside the .table
class and modified its value using class modifiers:
.table { --table-layout: state-a; } @media (min-width: 600px) { .table--state-b\@sm { --table-layout: state-b; } } @media (min-width: 1000px) { .table--state-b\@md { --table-layout: state-b; } }
Note that the class modifiers are now used to change the value of a CSS custom property; there's no repetition of layout style.
Using JavaScript, we can check whether to add or remove the .table--state-b
class based on the value of this CSS custom property. This will apply the proper layout style!
var layout = getComputedStyle(table).getPropertyValue('--table-layout'); table.classList.toggle('table--standard', layout == 'standard');
This technique allows us to use a single class ( .table--state-b
) for the layout style, regardless of the media query. Adding a new variation only requires setting the value of a single CSS custom property. No code repetition involved!
In this example, we've been working with tables, but you can apply this technique to any component whose responsiveness is highly affected by its content.
This approach requires JavaScript to work, but that should not be an issue: if JS is off, we serve the state-a version of the table, which is perfectly accessible.
What about CSS custom properties support? Using CSS variables is probably the cleanest and most self-explanatory way. But if you need to support older browsers (e.g., IE 11 and below) you can use a ::before
pseudo-element and change its content using the class modifiers:
.table::before { display: none; content: 'state-a'; } @media (min-width: 600px) { .table--state-b\@sm::before { content: 'state-b'; } } @media (min-width: 1000px) { .table--state-b\@md::before { content: 'state-b'; } }
In JS, you can check the value of the ::before
content rather than a custom property. Same result, different browser support! You can decide based on your needs.
That's it. I hope you find this approach useful. If you use a different technique, I would love to hear it!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
程序员代码面试指南:IT名企算法与数据结构题目最优解(第2版)
左程云 / 电子工业出版社 / 109.00元
《程序员代码面试指南:IT名企算法与数据结构题目最优解(第2版)》是一本程序员代码面试"神书”!书中对IT名企代码面试各类题目的最优解进行了总结,并提供了相关代码实现。针对当前程序员面试缺乏权威题目汇总这一痛点,本书选取将近300道真实出现过的经典代码面试题,帮助广大程序员的面试准备做到接近万无一失。"刷”完本书后,你就是"题王”!《程序员代码面试指南:IT名企算法与数据结构题目最优解(第2版)》......一起来看看 《程序员代码面试指南:IT名企算法与数据结构题目最优解(第2版)》 这本书的介绍吧!