Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

tailwindcss-children

benface32.3kISCdeprecated2.1.0

Use tailwind-children

Tailwind CSS plugin to add variants that target child elements

readme

Children Variant Plugin for Tailwind CSS

Requirements

This plugin requires Tailwind CSS 1.2 or later. If your project uses an older version of Tailwind, you should install the latest 1.x version of this plugin (npm install tailwindcss-children@1.x).

Installation

npm install tailwindcss-children

Usage

// tailwind.config.js
module.exports = {
  variants: {
    display: ['children', 'default', 'children-first', 'children-last', 'children-odd', 'children-even', 'children-not-first', 'children-not-last', 'children-hover', 'hover', 'children-focus', 'focus', 'children-focus-within', 'focus-within', 'children-active', 'active', 'children-visited', 'visited', 'children-disabled', 'disabled', 'responsive'],
  },
  plugins: [
    require('tailwindcss-children'),
  ],
};

The above configuration would generate the following CSS:

.children\:block > * {
  display: block;
}

.block {
  display: block;
}

.children\:first\:block > :first-child {
  display: block;
}

.children\:last\:block > :last-child {
  display: block;
}

.children\:odd\:block > :nth-child(odd) {
  display: block;
}

.children\:even\:block > :nth-child(even) {
  display: block;
}

.children\:not-first\:block > :not(:first-child) {
  display: block;
}

.children\:not-last\:block > :not(:last-child) {
  display: block;
}

.children\:hover\:block > :hover {
  display: block;
}

.hover\:block:hover {
  display: block;
}

.children\:focus\:block > :focus {
  display: block;
}

.focus\:block:focus {
  display: block;
}

.children\:focus-within\:block > :focus-within {
  display: block;
}

.focus-within\:block:focus-within {
  display: block;
}

.children\:active\:block > :active {
  display: block;
}

.active\:block:active {
  display: block;
}

.children\:visited\:block > :visited {
  display: block;
}

.visited\:block:visited {
  display: block;
}

.children\:disabled\:block > :disabled {
  display: block;
}

.disabled\:block:disabled {
  display: block;
}

/* etc. */

Which you can then use in your HTML like this:

<ul class="children:block children:not-first:border-t children:border-gray children:hover:bg-gray">
  <li>
    First item
  </li>
  <li>
    Second item
  </li>
  <li>
    Last item, this one doesn't have a bottom border
  </li>
</ul>

You can also override children: classes on specific children if needed:

<ul class="children:block children:bg-gray">
  <li>
    First item
  </li>
  <li class="bg-red">
    Second item, this one has a red background
  </li>
  <li class="bg-blue">
    Third item, this one has a blue background
  </li>
  <li>
    Last item
  </li>
</ul>

The above depends on the order of the generated CSS, so make sure to add the default variant after the children one in the array of variants (as well as the hover variant after the children-hover variant if you want to override a children:hover:* utility, etc.).

changelog

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project mostly adheres to Semantic Versioning.

2.1.0 - 2020-05-09

Added

  • Added a children-not-last variant

Changed

  • Changed the implementation of the children-not-first variant to > :not(:first-child) (instead of > * + *) for consistency with children-not-last

2.0.0 - 2020-02-05

Added

  • Added a children-not-first variant (e.g. children:not-first:border-t)

Changed

  • Changed to use Tailwind 1.2’s new plugin definition syntax
  • Changed the name and class prefix of the following variants:
    • first-child is now children-first and its class prefix is now children:first:
    • last-child is now children-last and its class prefix is now children:last:
    • odd-children is now children-odd and its class prefix is now children:odd:
    • even-children is now children-even and its class prefix is now children:even:

Fixed

  • Fixed an issue where the variants didn’t work as expected on utilities with pseudo-elements

1.3.0 - 2019-12-20

Added

  • Added children-visited and children-disabled variants

1.2.0 - 2019-09-02

Added

  • Added odd-children and even-children variants

1.1.0 - 2019-07-08

Added

  • Added 4 variants: children-hover, children-focus, children-focus-within, and children-active

1.0.0 - 2019-02-14

Initial release