tailwind-css - 顺风 CSS : Is there a way to target next sibling?

标签 tailwind-css

我有一个带有如下标签的 radio 输入。输入被隐藏,标签用于制作一个视觉上吸引人的圆圈,用户可以点击它。

<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>
当用户点击标签输入时被检查。我想弄清楚如何定位,所以我可以给标签一个不同的风格。
这可以使用下一个兄弟选择器在纯 css 中实现。
input[type="radio"]:checked + label {
  background-color: #bfb !important;
  border-color: #4c4 !important;
}
我可以改用 tailwind.css 中的类似内容吗?

最佳答案

这是一个 complex variant我为此目的而编写(使用 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"],
    },
  },
};

关于tailwind-css - 顺风 CSS : Is there a way to target next sibling?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63334626/

相关文章:

html - CSS/顺风 : Why does justify-between send element to bottom instead of content-between?

javascript - DOM 中 div 的响应式重组

css - 如何使用 Tailwind CSS 将宽度设置为超过 100%

tailwind-css - TailwindCSS 使用带有占位符颜色的 @apply

vuejs2 - Tailwindcss @apply 指令在 vue 组件中不起作用

reactjs - Tailwind 无法与 NextJS + Bun.sh 一起使用

tailwind-css - 保持一列固定而另一列滚动

ruby-on-rails - Tailwind CSS 响应类不适用于 Rails ViewComponent gem

html - 在 React 中添加组件会导致水平滚动条

css - 顺风CSS : Using a ref to calculate width using an arbitrary value