媒体查询的条件语句在Tailwind CSS中的表达方式:

huangapple go评论150阅读模式
英文:

conditional statement for media query on tailwindcss

问题

如何在Tailwind中仅针对中等或大屏幕设置条件语句?

英文:

How can i put this

${window.scrollY > 0 ? " bg-slate-50" : ""} 

Conditional statement only for medium or large screens on tailwind?

答案1

得分: 1

${window.scrollY > 0 ? "max-md:bg-slate-50" : ""}

你还可以在Tailwind的文档中查看更多信息:https://tailwindcss.com/docs/responsive-design#targeting-a-single-breakpoint

英文:

${window.scrollY > 0 ? "max-md:bg-slate-50" : ""}

you can also get the reference from the documentation of Tailwind for more info:
https://tailwindcss.com/docs/responsive-design#targeting-a-single-breakpoint

答案2

得分: 0

只需在您的代码中添加以下内容即可轻松完成:

className={md:${window.scrollY > 0 ? "bg-slate-50" : "bg-transparent"}}

这是Tailwind的方式来实现。否则,您始终可以编写自定义CSS。

请确保在您的 tailwind.config.js 中包含所需的变体,如果尚未启用:

module.exports = {
  variants: {
    backgroundColor: ['responsive', 'hover', 'focus'],
    // ... 其他配置
  },
}
英文:

You can easily do it by adding this in your code:

className={md:${window.scrollY > 0 ? "bg-slate-50" : "bg-transparent"}}

Thats tailwind way of doing it. otherwise you can always write the custom css.

Note Please make sure to include the required variant in your tailwind.config.js if it's not already enabled:

 module.exports = {
      variants: {
        backgroundColor: ['responsive', 'hover', 'focus'],
        // ... other configurations
      },
}

答案3

得分: 0

<div className={`md:${window.scrollY > 0 ? " bg-slate-50" : "bg-transparent"}`}>
...
</div>
英文:

Code:

&lt;div className={`md:${window.scrollY &gt; 0 ? &quot; bg-slate-50&quot; : &quot;bg-transparent&quot;}`}&gt;
...
&lt;/div&gt;

You can follow this documentation for more info:

https://tailwindcss.com/docs/responsive-design

huangapple
  • 本文由 发表于 2023年8月10日 13:17:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76872817.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定