Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tailwind CSS : Is there a way to target next sibling?

Tags:

tailwind-css

I have a radio input with a label like below. input is hidden and label is used to make a visually appealing circle on which user can click.

<input id="choice-yes" type="radio" class="opacity-0 w-0 fixed"/>
<label for="choice-yes" class="transition  duration-500 bg-blue-300 hover:bg-blue-500 w-20 h-20 rounded-full mr-5 flex items-center align-middle justify-center">Yes</label>

When user clicked on the label input get checked. I'm trying to figure out how to target that, so I could give the label a different style.

This can be achieved in pure css using next sibling selector.

input[type="radio"]:checked + label {
  background-color: #bfb !important;
  border-color: #4c4 !important;
}

Is there something similar in tailwind.css that I could use instead?

like image 664
EastSw Avatar asked Aug 10 '20 05:08

EastSw


People also ask

How do I target a sibling in CSS?

Adjacent Sibling Selector (+) The adjacent sibling selector is used to select an element that is directly after another specific element. Sibling elements must have the same parent element, and "adjacent" means "immediately following".

What is next sibling selector?

Description: Selects all sibling elements that follow after the "prev" element, have the same parent, and match the filtering "siblings" selector.

How do I get previous siblings in CSS?

No, there is no "previous sibling" selector. On a related note, ~ is for general successor sibling (meaning the element comes after this one, but not necessarily immediately after) and is a CSS3 selector. + is for next sibling and is CSS2.

What is SR only in Tailwind?

​ Use sr-only to hide an element visually without hiding it from screen readers: <a href="#"> <svg><!-- ...


2 Answers

Here's a complex variant I wrote for this purpose (tested with tailwindcss 2.0.1)

// tailwind.config.js

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

const focusedSiblingPlugin = plugin(function ({ addVariant, e }) {
  addVariant("focused-sibling", ({ container }) => {
    container.walkRules((rule) => {
      rule.selector = `:focus + .focused-sibling\\:${rule.selector.slice(1)}`;
    });
  });
});

module.exports = {
  // ...
  plugins: [focusedSiblingPlugin],
  variants: {
    extend: {
      backgroundColor: ["focused-sibling"],
    },
  },
};
like image 56
Jordan Mackie Avatar answered Sep 29 '22 09:09

Jordan Mackie


For those looking to implement this, since introduction of just-in-time mode (Tailwind CSS v2.1+) sibling selector variant is available.

like image 26
Furkat Kholmatov Avatar answered Sep 29 '22 09:09

Furkat Kholmatov