2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Tailwind CSSで動的に色を指定できないときの解決方法

Last updated at Posted at 2024-02-23

問題

Tailwind CSSでは,16進数のカラーコードを動的に指定できない.すなわち,次のようなことはできない.

const colorInHex = "ffffff";
return <Hoge className=`bg-[#${colorInHex}]`>

これは,Tailwind CSSではビルド時にCSSのコンパイルが行われるためである.

解決法

styleプロパティで指定する.

const colorInHex = "#ffffff";
return <Hoge style={{backgroundColor: colorInHex}}>

あとは,styled-components等のCSS in JSライブラリを使っても多分できる.

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?