tailwind classes not working when i transfer through props while working perfecly when i use cdn link of tailwind css

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

tailwind classes not working when i transfer through props while working perfecly when i use cdn link of tailwind css

问题

//Button.jsx

import React from 'react';

const Button = (props) => {
  let color = props.color || 'blue';

  return (
    <button className={`px-4 py-2 font-bold text-black bg-${color}-500 rounded-full hover:bg-${color}-700 focus:outline-none focus:shadow-outline`}>
      Click
    </button>
  );
};

export default Button;


// App.jsx
import Button from "./Button";
import './index.css';
function App() {
  return (
    <div>
       <Button >Click me!</Button>
      <Button color="red">Click me!</Button>
      <Button color="green">Click me!</Button>
    </div>
  )
}

export default App

这是您提供的代码的中文翻译部分。

英文:

<!-- begin snippet: js hide: false console: false babel: false -->

<!-- language: lang-js -->

//Button.jsx

import React from &#39;react&#39;;

const Button = (props) =&gt; {
  let color = props.color || &#39;blue&#39;;

  return (
    &lt;button className={`px-4 py-2 font-bold text-black bg-${color}-500 rounded-full hover:bg-${color}-700 focus:outline-none focus:shadow-outline`}&gt;
      Click
    &lt;/button&gt;
  );
};

export default Button;


// App.jsx
import Button from &quot;./Button&quot;
import &#39;./index.css&#39;
function App() {


  return (
    &lt;div&gt;
       &lt;Button &gt;Click me!&lt;/Button&gt;
      &lt;Button color=&quot;red&quot;&gt;Click me!&lt;/Button&gt;
      &lt;Button color=&quot;green&quot;&gt;Click me!&lt;/Button&gt;
    &lt;/div&gt;
    )
}

export default App  

<!-- end snippet -->

this is a component where i'm transferring background color using props

i don't know what's wrong... same code just when i add cdn link of tailwindcss in html page it work but don't work without cdn.
if i will transfer bg-red-500 whole class as props then it works but if i send only red as props then it doesn't work.
even in inspect page i can see classes but they're not taking effect.
i'm using vite-react.

答案1

得分: 1

你可能忘记了一步,你是否在你的主CSS文件中执行了以下代码:@tailwind base; @tailwind components; @tailwind utilities;?入门指南还显示了一个额外的步骤:https://tailwindcss.com/docs/installation

英文:

I think your forgetting a step, did you do @tailwind base;
@tailwind components;
@tailwind utilities;
in your main css?
The getting started also shows one more extra step: https://tailwindcss.com/docs/installation

huangapple
  • 本文由 发表于 2023年1月6日 14:36:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75027728.html
匿名

发表评论

匿名网友

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

确定