使用自定义颜色在Tailwind中

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

Using custom colors in Tailwind

问题

我正在尝试在我的React/TypeScript应用中使用Tailwind创建自定义十六进制颜色。为什么背景颜色不随颜色变量值而改变?以下是我的代码:

import React, { useState } from 'react';

const ColorChanger = () => {
  const [color, setColor] = useState('#ffffff');

  const handleColorChange = () => {
    const randomColor = `#${Math.floor(Math.random() * 16777215).toString(16)}`;
    console.log("setColor =", randomColor);
    setColor(randomColor);
  };

  return (
    <div>
      <div className={`w-20 h-20 bg-[${color}] mx-auto mb-4`}>{color}</div>

      <button
        type="button"
        className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
        onClick={handleColorChange}
      >
        Changer la couleur
      </button>
    </div>
  );
};
function App() {
  return (
    <div className="App">
      <ColorChanger />
    </div>
  );
}

export default App;

Index.css

body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

code {
  font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
  monospace;
}

@tailwind base;
@tailwind components;
@tailwind utilities;

App.css

@tailwind base;
@tailwind components;
@tailwind utilities;

如何在Tailwind CSS中使用自定义颜色?

英文:

I'm trying to create custom hex colors for my React/TypeScript app using Tailwind.

Why doesn't the background color change with the color variable value?

Here is my code:

    import React, { useState } from &#39;react&#39;;

    const ColorChanger = () =&gt; {
    const [color, setColor] = useState(&#39;#ffffff&#39;);

      const handleColorChange = () =&gt; {
        const randomColor = `#${Math.floor(Math.random() * 16777215).toString(16)}`;
        console.log(&quot;setColor = &quot;, randomColor)
        setColor(randomColor);
      };

      return (
        &lt;div&gt;
          &lt;div className={`w-20 h-20 bg-[${color}] mx-auto mb-4`}&gt;{color}&lt;/div&gt;

          &lt;button
            type=&quot;button&quot;
            className=&quot;bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded&quot;
            onClick={handleColorChange}
          &gt;
            Changer la couleur
          &lt;/button&gt;
        &lt;/div&gt;
      );
    };
    function App() {
      return (
        &lt;div className=&quot;App&quot;&gt;
          &lt;ColorChanger /&gt;
        &lt;/div&gt;
      );
    }

    export default App;

Index.css

    body {
        margin: 0;
        font-family: -apple-system, BlinkMacSystemFont, &#39;Segoe UI&#39;, &#39;Roboto&#39;, &#39;Oxygen&#39;, &#39;Ubuntu&#39;, &#39;Cantarell&#39;, &#39;Fira Sans&#39;, &#39;Droid Sans&#39;, &#39;Helvetica Neue&#39;, sans-serif;
        -webkit-font-smoothing: antialiased;
        -moz-osx-font-smoothing: grayscale;
    }

    code {
      font-family: source-code-pro, Menlo, Monaco, Consolas, &#39;Courier New&#39;,
      monospace;
    }

    @tailwind base;
    @tailwind components;
    @tailwind utilities;

App.css

...
    @tailwind base;
    @tailwind components;
    @tailwind utilities;

How can I use custom colors with Tailwind CSS?

答案1

得分: 2

看起来你在使用Tailwind CSS类中遇到了一些问题。Tailwind CSS通常不支持在类名中使用动态变量,这可能导致背景颜色不会改变的问题。

修复这个问题的一种简单方法是在React中使用内联样式。你可以直接在div上设置backgroundColor,就像这样:

<div style={{backgroundColor: color}} className="w-20 h-20 mx-auto mb-4">{color}</div>

这样,你的颜色状态将直接应用于divbackgroundColor,同时保持其他Tailwind样式不变。

英文:

It looks like you're having trouble with using dynamic colors in your Tailwind CSS classes.
Tailwind CSS doesn't usually support dynamic variables in class names, which might be causing your issue with the background color not changing.

One simple way to fix this is to use inline styles with React. You can set the backgroundColor directly on your div like this.

&lt;div style={{backgroundColor: color}} className=&quot;w-20 h-20 mx-auto mb-4&quot;&gt;{color}&lt;/div&gt;

This way, your color state will be applied directly to the div's backgroundColor, while still keeping your other Tailwind styles intact.

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

发表评论

匿名网友

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

确定