如何在 tailwind css 中的条件上添加样式

Tus*_*ha 21 javascript css typescript reactjs tailwind-css

我正在使用 tailwind css 在 React 中制作侧导航,当选择特定链接时,我必须添加边框红色。所以我正在使用这种方法但这不起作用任何人都可以在这里帮助我

<li className={"flex items-center space-x-2 px-4 py-5 transition duration-500 ease-in-out transform hover:-translate-y-1 hover:scale-110 border-l-4 border-white" + (window.location.pathname === '/' ? 'border-red-200' : '')}>
              <NavLink to={"/"}>
                <div className="sidebar">
                  <div className="float-left">
                    <svg
                      xmlns="http://www.w3.org/2000/svg"
                      className="h-8 w-6 text-red-400"
                      fill="none"
                      viewBox="0 0 24 24"
                      stroke="currentColor"
                    >
                      <path
                        strokeLinecap="round"
                        strokeLinejoin="round"
                        strokeWidth={2}
                        d="M3 12l2-2m0 0l7-7 7 7M5 10v10a1 1 0 001 1h3m10-11l2 2m-2-2v10a1 1 0 01-1 1h-3m-6 0a1 1 0 001-1v-4a1 1 0 011-1h2a1 1 0 011 1v4a1 1 0 001 1m-6 0h6"
                      />
                    </svg>
                  </div>
                  <span className="text-2xl float-right pl-5">
                    Home
                  </span>
                </div>
              </NavLink>
            </li>
Run Code Online (Sandbox Code Playgroud)

小智 43

使用模板文字,或使用 npm 库类名

  • 使用模板文字
<... className={`flex items-center space-x-2 px-4 py-5 transition duration-500 ease-in-out transform hover:-translate-y-1 hover:scale-110 border-l-4 border-white ${window.location.pathname === '/' ? 'border-red-200' : ''}`}>

Run Code Online (Sandbox Code Playgroud)
  • 使用类名
import classNames from 'classnames';

<... className={classNames('flex items-center space-x-2 px-4 py-5 transition duration-500 ease-in-out transform hover:-translate-y-1 hover:scale-110 border-l-4 border-white',
    { 
        'border-red-200': window.location.pathname === '/' 
    }
)}>
Run Code Online (Sandbox Code Playgroud)

  • 可能有用的是,条件也可以发生在模板文字之外,例如 `const borderColor = window.location.pathname === '/' ?'border-red-200' : ''` 然后 `&lt;... className={`flex ${borderColor }`}`。Tailwind 仍然可以检测类名:https://tailwindcss.com/docs/content-configuration#class-detection-in-deep (3认同)

小智 10

className={yourItem === value ? 'your-class' : 'or-default-to'}
Run Code Online (Sandbox Code Playgroud)

` /* 如果您设置的条件为真或不为真,则这将应用类。下面的例子*/

className={active === value ? 'text-green' : 'text-white'}

class={active === value ? 'text-green' : 'text-white'}
Run Code Online (Sandbox Code Playgroud)


小智 7

对我来说,使用类名库是具有多种条件的最干净的方法:

从“类名”导入 cx;

const classes = cx(
        'flex items-center justify-center', // classes that should be used in any case
        size === 'small' && 'h-10 text-lg px-2.5',
        size === 'medium' && 'h-12 text-lg px-4',
        variant === 'primary text-primary`,
        variant === 'secondary text-secondary`,
        fullWidth && 'w-full',
        className // classname passed from props
    );
Run Code Online (Sandbox Code Playgroud)

但在你的情况下字符串插值应该足够了

`flex items-center space-x-2 px-4 py-5 transition duration-500 ease-in-out transform hover:-translate-y-1 hover:scale-110 border-l-4 border-white ${window.location.pathname === '/' ? 'border-red-200' : ''}`
Run Code Online (Sandbox Code Playgroud)