Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TailwindCSS - Change Label When Radio Button Checked

I see that the TailwindCSS checked: variant can be enabled to change the input element when checked, but how can I change the input's label when checked?

Here is the relevant Tailwind CSS docs.

Sample code below.

After enabling the variant in tailwind.config.js, putting checked:bg-green-300 in the div or the label doesn't work. It only works in the input.

<div>
  <label>
    <input checked type="radio" name="option1" id="option1" className="hidden" />
    <div>option1</div>
  </label>
  <label>
    <input checked type="radio" name="option2" id="option1" className="hidden" />
    <div>option2</div>
  </label>
</div>
like image 533
wongz Avatar asked Jan 19 '21 01:01

wongz


People also ask

How do I change the color of a radio button in Tailwind CSS?

Apply the text-{color}-{shade} utility class from Tailwind CSS to change the color of the radio component.

How do I style a checkbox with Tailwind CSS?

To style a checkbox, first install the @tailwindcss/forms plugin, which makes it much easier to style form elements with Tailwind CSS. Then, add the plugin to your Tailwind config file: // tailwind. config.

How do you use peer in Tailwind CSS?

Tailwind Peer class You can add peer behavior by adding two classes to the HTML. Add the peer class to the HTML tag you want to observe the state for. Add the peer-checked class, followed by the desired behavior change, to a sibling element.

How do I change the color of a checkbox in Tailwind CSS?

Use the text-{color}-{shade} classes from Tailwind CSS to change the color of the checkbox component.

How do I create a radio component with tailwind CSS?

Make sure that you have included Flowbite as a plugin inside your Tailwind CSS project to apply all the necessary styles for the radio component. Use the default example of a radio component with the checked and unchecked state. Apply the disabled attribute to the radio component to disallow the selection for the user.

How does tailwind compare to traditional CSS?

How does this compare to traditional CSS? When writing CSS the traditional way, a single class name would do different things based on the current state. In Tailwind, rather than adding the styles for a hover state to an existing class, you add another class to the element that only does something on hover.

How do I hide a radio button on a label?

Use the CSS :checked pseudo-class and the adjacent sibling selector (+). This will apply to any label that directly follows a checked radio button. In our next example, we add a margin to the "radio-button" class, then hide the circular buttons but differently from the previous example.

Can I use tailwind’s modifiers with my own custom classes?

All of Tailwind’s modifiers are available to use with your own custom classes as long as you’ve defined them in one of Tailwind’s layers or added them using a plugin: When stacking modifiers, they are applied from the inside-out, like nested function calls:


1 Answers

EDIT: as version 2.2+ was released it has built-in support for sibling selector variants called peer (watch updates release)

This feature is only available in Just-in-Time mode.

<label>
    <input checked type="radio" name="option" id="option1" class="hidden peer" />
    <div class="peer-checked:bg-red-600">option1</div>
</label>

For versions bellow 2.2: You need to write your own plugin for adding new variant. Mor info here

For example, let name it label-checked

tailwind.config.js

const plugin = require('tailwindcss/plugin');

module.exports = {
    purge: [],
    darkMode: false, // or 'media' or 'class'
    theme: {},
    variants: {
        extend: {
            backgroundColor: ['label-checked'], // you need add new variant to a property you want to extend
        },
    },
    plugins: [
        plugin(({ addVariant, e }) => {
            addVariant('label-checked', ({ modifySelectors, separator }) => {
                modifySelectors(
                    ({ className }) => {
                        const eClassName = e(`label-checked${separator}${className}`); // escape class
                        const yourSelector = 'input[type="radio"]'; // your input selector. Could be any
                        return `${yourSelector}:checked ~ .${eClassName}`; // ~ - CSS selector for siblings
                    }
                )
            })
        }),
    ],
};

This configuration should work for next cases (We extended backgroundColor, so it should work with bg-color classes):

1 - label is the wrapper, it's text should wrapped in any selector (in this case div)

<label>
    <input checked type="radio" name="option1" id="option1" class="hidden" />
    <div class="label-checked:bg-red-600">option1</div>
</label>

2 - label after input

<input checked type="radio" name="option1" id="option1" class="hidden" />
<label for="option-1" class="label-checked:bg-red-600"></label>

DEMO - https://play.tailwindcss.com/SEQ4NRpPV3

like image 145
Ihar Aliakseyenka Avatar answered Feb 16 '23 11:02

Ihar Aliakseyenka