在Tailwind CSS的部分中添加背景图片。

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

Add a background image to a tailwinds css section

问题

我正在尝试在Tailwind CSS模板部分中添加背景图片。

Tailwind文档说要扩展主题,并定义一个图片,我已经在我的tailwind.config.js中这样做了:

extend: {
      backgroundImage: {
        'ttuPattern': 'url("/images/ttu.png")'
      },

我想将该图片用作部分的背景。

<section
      id="ttu"
      aria-label="Features for investing all your money"
      className="bg-gray-900 py-20 sm:py-32 ALSO TRIED IT HERE bg-cover"
      style="background-image: url('ttuPattern'); height: 100%;"
>
      <Container>
        <div className="mx-auto max-w-2xl lg:mx-0 lg:max-w-3xl bg-cover" style="background-image: url('ttuPattern'); height: 100%;">

我的部分中没有显示图片。

如何在Tailwind CSS中使用背景图片?

英文:

I am trying to figure out how to add a background image to a Tailwinds CSS template section.

The tailwind docs say to extend the theme, with a defined image, which I have done in my tailwind.config.js as follows:

extend: {
backgroundImage: {
'ttuPattern': "url('/images/ttu.png')"
},

I want to use that image as the background of a section.

&lt;section
      id=&quot;ttu&quot;
      aria-label=&quot;Features for investing all your money&quot;
      className=&quot;bg-gray-900 py-20 sm:py-32 ALSO TRIED IT HERE bg-cover&quot; style=&quot;background-image: ttuPattern height: 100%&quot;
      
    &gt;
      &lt;Container&gt;
        &lt;div className=&quot;mx-auto max-w-2xl lg:mx-0 lg:max-w-3xl bg-cover&quot; style=&quot;background-image: ttuPattern height: 100%&quot;&gt;

The image doesn't display in my section at all.

How can I use a bg image in tailwind css?

答案1

得分: 1

class属性中最好使用bg-ttuPattern,因为它只需要更少的代码。在新版本3中,如果你只打算使用图片一次,可以使用任意值

<div class="bg-[url('../public/images/tailwind.png')]"></div>

并且我已经修复了tailwind.config.js中的一些语法问题。

代码:

结构:
<section class="bg-gray-900 bg-ttuPattern bg-cover py-20 sm:py-32">
  <!-- 这里放入你的内容! -->
</section>
配置:
module.exports = {
  theme: {
    extend: {
      backgroundImage: {
        ttuPattern: "url('https://via.placeholder.com/600x200.png')",
      },
    },
  },
  plugins: [],
}

请参见playground

英文:

It's better to use bg-ttuPattern in class attribute because it just takes less. In the new version 3, you can use arbitrary values if you're going to use the image once:

&lt;div class=&quot;bg-[url(&#39;../public/images/tailwind.png&#39;)]&quot;&gt;&lt;/div&gt;

And I've fixed some syntax issues in tailwind.config.js.

Code:

Structure:
&lt;section class=&quot;bg-gray-900 bg-ttuPattern bg-cover py-20 sm:py-32&quot;&gt;
  &lt;!-- Your content goes here! --&gt;
&lt;/section&gt;
Config:
module.exports = {
  theme: {
    extend: {
      backgroundImage: {
        ttuPattern: &quot;url(&#39;https://via.placeholder.com/600x200.png&#39;)&quot;,
      },
    },
  },
  plugins: [],
}

See playground.

huangapple
  • 本文由 发表于 2023年6月5日 18:13:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76405402.html
匿名

发表评论

匿名网友

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

确定