Skip to content

Children selectors #1984

@jenstornell

Description

@jenstornell

To prevent repetition both in CSS and HTML we can use a component. However, when working with static lists it may not be a good idea to have every list item as a component.

A simplified example below:

<ul>
  <li class="bg-red-500 hover:bg-red-700 rounded text-sm text-white">Home</li>
  <li class="bg-red-500 hover:bg-red-700 rounded text-sm text-white">About</li>
  <li class="bg-red-500 hover:bg-red-700 rounded text-sm text-white">Projects</li>
  <li class="bg-red-500 hover:bg-red-700 rounded text-sm text-white">Hello</li>
  <li class="bg-red-500 hover:bg-red-700 rounded text-sm text-white">World</li>
  <li class="bg-red-500 hover:bg-red-700 rounded text-sm text-white">Foo</li>
  <li class="bg-red-500 hover:bg-red-700 rounded text-sm text-white">Bar</li>
  <li class="bg-red-500 hover:bg-red-700 rounded text-sm text-white">Tail</li>
  <li class="bg-red-500 hover:bg-red-700 rounded text-sm text-white">Wind</li>
<ul>

For static lists like this you could split the data into an array and keep the classes as a variable. It's a bit more hassle and not always as readable.

Solution - Child selectors

Let's take the first example again:

<ul class="bg-red-500> hover:bg-red-700> rounded> text-sm> text-white>">
  <li>Home</li>
  <li>About</li>
  <li>Projects</li>
  <li>Hello</li>
  <li>World</li>
  <li>Foo</li>
  <li>Bar</li>
  <li>Tail</li>
  <li>Wind</li>
<ul>
  1. When need to change a class, you only do that in one place.
  2. It should only affect direct children, not grand children.
  3. Children classes on the li should override the child class set in the ul.
  4. I put > last as class names needs to start with a letter.
  5. Very similar to the current Tailwind syntax.

Cons

I can only come up with one con at the moment.

  1. It would probably double the filesize of the CSS file before purge.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions