我正在尝试重新创建一个从 vanilla CSS 到 tailwindcss 的项目。但我尝试了很多选择,但都失败了。
这是 CSS 代码:
header {
background: linear-gradient(rgba(135, 80, 156, 0.9), rgba(135, 80, 156, 0.9)), url(img/hero-bg.jpg);
background-repeat: no-repeat;
background-size: cover;
background-position: center center;
background-attachment: fixed;
position: relative;
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以将此代码转换为等效的 tailwindcss 代码吗?
Cry*_*fel 10
您有几个选择:
最简单的一种是在 style 属性上设置图像,毕竟这是非常自定义的样式:
<header
class="relative bg-fixed bg-center bg-cover bg-no-repeat"
style="background-image:linear-gradient(rgba(135, 80, 156, 0.9), rgba(135, 80, 156, 0.9)), url(img/hero-bg.jpg)">
</header>
Run Code Online (Sandbox Code Playgroud)
第二个选项是继续使用现在的样式表,但仅适用于背景图像:
header {
background-image:linear-gradient(rgba(135, 80, 156, 0.9), rgba(135, 80, 156, 0.9)), url(img/hero-bg.jpg)
}
<header class="relative bg-fixed bg-center bg-cover bg-no-repeat">
</header>
Run Code Online (Sandbox Code Playgroud)
最后,您可以创建一个插件,您可以在其中动态发送颜色和图像作为参数,并且 tailwind 将为您生成这些类。这更复杂,但文档确实很有帮助:https ://tailwindcss.com/docs/plugins#app
如果你问我,我只会选择第一个选项
这是一个工作演示和教程:https://bleext.com/post/creating-a-hero-header-with-a-fixed-image