【问题标题】:How to change the background color of customize checkbox in tailwind如何在顺风中更改自定义复选框的背景颜色
【发布时间】:2021-10-01 00:02:51
【问题描述】:

我使用 tailwind 创建了一个自定义复选框(作为 react 项目中的一个组件)。现在我想在检查之前和检查之后更改复选框的背景颜色。 这就是我要的:

这是当前的外观:

我想知道如何在 tailwind.css 中更改此颜色 这是复选框代码:

<div className="check-box">
    <input type="checkbox"
         value={ value }
         id={ id }
         name={ name }
         onBlur={ onBlur }
         className="absolute h-6 w-6 cursor-pointer" onChange={ onChange } />
                    
     <div className="bg-xaplink_gray_1 border-2 border-   xaplink_gray_3 w-6 h-6 flex flex-shrink-0 justify-center items-center mr-2 focus-within:border-xaplink_black_2">
        <svg className="fill-current hidden w-4 h-4 text-xaplink_white pointer-events-none" version="1.1" viewBox="0 0 17 12" xmlns="http://www.w3.org/2000/svg">
          <g fill="none" fillRule="evenodd">
              <g transform="translate(-9 -11)" fill="#FFFF" fillRule="nonzero">
                  <path d="m25.576 11.414c0.56558 0.55188 0.56558 1.4439 0 1.9961l-9.404 9.176c-0.28213 0.27529-0.65247 0.41385-1.0228 0.41385-0.37034 0-0.74068-0.13855-1.0228-0.41385l-4.7019-4.588c-0.56584-0.55188-0.56584-1.4442 0-1.9961 0.56558-0.55214 1.4798-0.55214 2.0456 0l3.679 3.5899 8.3812-8.1779c0.56558-0.55214 1.4798-0.55214 2.0456 0z" />
              </g>
          </g>
         </svg>
  </div>
</div>

这是我在 react 中的复选框组件:

import React, { useEffect, useState }  from "react";
const XCheckBox = ({ value, id, name, onBlur,label, side, error, touched, onChange }) => {
    
    return (
        <>
           <div className=" flex flex-row gap-5 py-2">
               <div className="check-box">
                   <input type="checkbox"
                          value={ value }
                          id={ id }
                          name={ name }
                          onBlur={ onBlur }
                          className="absolute h-6 w-6 cursor-pointer" onChange={ onChange } />
                    <div className="bg-xaplink_gray_1 border-2 border-xaplink_gray_3 w-6 h-6 flex flex-shrink-0 justify-center items-center mr-2 focus-within:border-xaplink_black_2">
                        <svg className="fill-current hidden w-4 h-4 text-xaplink_white pointer-events-none" version="1.1" viewBox="0 0 17 12" xmlns="http://www.w3.org/2000/svg">
                            <g fill="none" fillRule="evenodd">
                                <g transform="translate(-9 -11)" fill="#FFFF" fillRule="nonzero">
                                    <path d="m25.576 11.414c0.56558 0.55188 0.56558 1.4439 0 1.9961l-9.404 9.176c-0.28213 0.27529-0.65247 0.41385-1.0228 0.41385-0.37034 0-0.74068-0.13855-1.0228-0.41385l-4.7019-4.588c-0.56584-0.55188-0.56584-1.4442 0-1.9961 0.56558-0.55214 1.4798-0.55214 2.0456 0l3.679 3.5899 8.3812-8.1779c0.56558-0.55214 1.4798-0.55214 2.0456 0z" />
                                </g>
                            </g>
                        </svg>
                    </div>
                </div>
            </div>
            <div>
                { touched && error ? (
                    <p className="text-xs pl-2 text-xaplink_red_2 mb-4">{ error }</p>
                ) : null }
            </div>
        </>
    );
};

export default XCheckBox;

【问题讨论】:

    标签: reactjs tailwind-css


    【解决方案1】:

    不清楚您是否只想更改复选框的背景颜色,或删除其复选标志。

    无论如何,复选框输入的默认样式包含复选符号,您可以根据需要创建自定义输入。

    如果您想删除复选标记,您可以使用以下沙箱创建自定义复选框。 Custom checkbox without check sign

    如果你想要一个复选标志,你可以根据你的主题使用 tailwindCss 实现跟随样式。 Custom checkbox with check sign

    【讨论】:

    • 不,我想用复选标志更改背景颜色。你已经使用了 CSS。但是在我的项目中,我使用了tailwind.css 我想知道如何使用tailwind 更改背景颜色。
    • 如果需要,您可以使用 @tailwindcss/forms 插件。这是它的related sample。但是如果你不想使用这个插件,你应该使用CSS更改样式(因为它需要更改:after伪元素。)
    猜你喜欢
    • 2020-05-19
    • 1970-01-01
    • 1970-01-01
    • 2021-10-14
    • 1970-01-01
    • 2012-10-05
    • 1970-01-01
    • 2011-08-17
    • 1970-01-01
    相关资源
    最近更新 更多