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>
Apply the text-{color}-{shade} utility class from Tailwind CSS to change the color of the radio component.
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.
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.
Use the text-{color}-{shade} classes from Tailwind CSS to change the color of the checkbox component.
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 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.
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.
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:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With