通过@Media查询更新根字体大小(因此REM),响应敏感的尾风CSS?
我一直在我的 Vue 项目中使用 tailwind,总体来说它非常好,但是总是写 sm:w-10 lg:w-10 2xl:w-30
(等等)有点烦人对于我所有的课程。
所以我在想,由于宽度是基于 rems 的,那么在 lg xl 2xl
断点处更新根字体大小(决定 rem 值)而不是重置不是更有意义吗?每个标签上都有它们吗?
我认为我可以在根组件上通过类似的方法来实现这一点:
html { // changed from body to html
font-size: 16px;
@media screen and (max-width: 1024px) {
html {
font-size: 24px;
}
}
但我对这样做持怀疑态度,因为 Tailwind 文档根本没有提及它。谁能告诉我这是否/为什么是一个坏主意?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
只需将其添加到父类中,然后下面的所有内容都将应用相同的文本修饰符。
例如,随着所有子 div 的屏幕宽度增加,下面的文本大小也会增加
请参阅下面的链接以获取更多信息
字体大小
响应式设计
Just add it to the parent class then everything under applies the same text modifiers.
Eg below increases the size of text as the screen width goes up for all child divs
See below links for more info
Font Size
Responsive Design
我成功地将以下内容添加到
globals.css
中。I did this successfully by adding the following to
globals.css
.我认为这不是一个好主意,因为更改根字体大小会影响使用 rem 单位的 tailwind 的所有属性,不仅仅是宽度,还有填充、边距、字体大小、间隙等。只需一个断点更改就可能导致整个应用程序发生重大变化,从而使预测和控制布局的响应能力变得更加困难。
全球影响示例
考虑此组件旨在具有中等字体大小、中等填充和下边距:
断点之前:低于 1024 像素,
文本基
可能是 16pxp-4
导致所有边的内边距均为 16 像素mb-6
给出 24px 的下边距(假设默认 Tailwind 比例 mb-6 为 1.5rem)..断点之后:高于 1024px,根字体大小增加到 24px,
text-base
可能会意外缩放至 24pxp-4
将导致 24 像素内边距(而不是 16 像素)mb-6
给出的下边距为 36px (1.5rem * 24px)。后果
I don't think it's a good idea because changing the root font size affects all properties of tailwind that use
rem
units, not just widths but also padding, margins, font sizes, gaps, etc.. This can lead to big changes across your application with just a single break-point change, making it harder to predict and control the layout's responsiveness.Global Impact Example
Consider this component intended to have a medium font size, moderate padding, and a bottom margin:
Before the Breakpoint: Below 1024px,
text-base
might be 16pxp-4
results in 16px padding on all sidesmb-6
gives a bottom margin of 24px (assuming default Tailwind scale mb-6 is 1.5rem)..After the Breakpoint: Above 1024px, with the root font size increased to 24px,
text-base
could unexpectedly scale to 24pxp-4
would result in 24px padding (instead of 16px)mb-6
would give a bottom margin of 36px (1.5rem * 24px).Consequences
rem
units, leading to a lack of control over which elements resize at certain break points and by how much!如果将其设置为
html {
,则需要一个全新的间距量表。因为rem
是根据< html>
标签的大小设置计算的。将其设置为主体{
,就可以。If you set it at
html {
you need a whole new spacing scale. Becauserem
are calculated based on the size set for<html>
tag. Set it atbody {
and you are fine.对于您的问题而言,这可能为时已晚,但是在搜索了相同的问题之后,我认为最好的方法应该是评论。
对于不想带有链接的另一个领域的人:我们将一些规则添加到
tailwind.css
文件:tailwind出于某种原因而使用
rem
,而字体大小
我们在这里修改本质上是它。一些笔记您应该很明显,例如更改HTML字体大小可能会导致某些(意外?应该是预期的)行为,例如:当我们将字体大小更改为14px时,
w-4
将以w- [14px]
而不是往常而生效。另一个注意事项是您添加这些代码的CSS文件的名称将取决于您导入的尾风。我强调了这一点,因为我已经看到许多不同的路径名被提及了混乱的水平。
This might be too late for your problem but after a few searches for the same problem, I think the best approach should be this comment.
For who don't want to travel into another realm with the link: we add some rules to
tailwind.css
file:Tailwind uses
rem
for a reason, and thefont-size
we're modifying here is essentially it.A few notes is you should be awared that changing the html font-size might lead to some (unexpected? should be expected) behaviors, for example: when we change font-size to 14px,
w-4
will take effect asw-[14px]
instead of 16px as usual.Another note is the name of css file you add these code to will depend on where you import tailwind. I emphasize this since I've seen a lot of different pathname being mentioned to the level of confusion.