首页 > 解决方案 > 在tailwindcss中具有不透明度的背景图像

问题描述

我正在尝试从 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;
}

任何人都可以将此代码转换为等效的 tailwindcss 代码吗?

标签: csstailwind-css

解决方案


你有几个选择:

最简单的一种是在 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>

第二种选择是继续使用现在的样式表,但仅限于背景图像:

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>

最后,您可以创建一个插件,您可以在其中动态发送颜色和图像作为参数,tailwind 将为您生成这些类。这更复杂,但文档真的很有帮助:https ://tailwindcss.com/docs/plugins#app

如果你问我,我会选择第一个选项

这是一个工作演示和教程:https ://bleext.com/post/creating-a-hero-header-with-a-fixed-image


推荐阅读