How to Access Direct Children of a Div in Tailwind CSS v3

In this tutorial, we'll explore how to target and style the direct children of a div using Tailwind CSS v3's powerful arbitrary value syntax. This feature allows for more flexible and precise styling, especially when dealing with nested layouts.

The Problem

Consider the following HTML structure:

<div class="section">
   <div class="footer">footer</div>
   <div class="content">
      <div>sub contents 1</div>
      <div>sub contents 2</div>
   </div>
</div>

We want to style only the direct children of the div with the class "section", which are the divs with classes "footer" and "content". In regular CSS, we'd use the child combinator selector like this: div.section > div. But how do we achieve this in Tailwind CSS v3?

The Solution: Arbitrary Value Syntax

Tailwind CSS v3 introduced the arbitrary value syntax, which allows us to use the & character to reference the current selector. This feature provides a powerful way to target direct children. Here's how you can do it:

Syntax: [&>*]:{class}

Let's see this in action with some examples.

Example 1: Adding padding to all direct children

<div class="section [&>*]:p-4">
   <div class="footer">footer</div>
   <div class="content">
      <div>sub contents 1</div>
      <div>sub contents 2</div>
   </div>
</div>

In this example, [&>*]:p-4 adds padding of 1rem (p-4) to all direct children of the "section" div.

Example 2: Targeting specific child elements

<div class="section [&>div]:bg-gray-100 [&>.footer]:text-red-500">
   <div class="footer">footer</div>
   <div class="content">
      <div>sub contents 1</div>
      <div>sub contents 2</div>
   </div>
</div>

Here, we're applying different styles to different direct children:

Example 3: Combining multiple styles

<div class="section [&>*]:p-4 [&>*]:mb-2 [&>.content]:bg-blue-100">
   <div class="footer">footer</div>
   <div class="content">
      <div>sub contents 1</div>
      <div>sub contents 2</div>
   </div>
</div>

This example combines multiple arbitrary value selectors:

Practical Example

Let's create a footer with multiple columns, social media links, and a newsletter signup form. We'll use the arbitrary value syntax to style the direct children of our footer container.

<footer class="py-12 text-white bg-gray-800">
  <div class="container px-4 mx-auto">
    <div class="[&>div]:mb-8 [&>div:last-child]:mb-0 md:[&>div]:mb-0 md:flex md:justify-between">
      <!-- Company Info -->
      <div class="md:w-1/4">
        <h2 class="mb-4 text-2xl font-bold">TechCorp</h2>
        <p class="[&>a]:text-blue-400 [&>a]:hover:underline">
          123 Tech Street, San Francisco, CA 94122<br>
          <a href="mailto:[email protected]">[email protected]</a><br>
          <a href="tel:+14155551234">(415) 555-1234</a>
        </p>
      </div>

      <!-- Quick Links -->
      <div class="md:w-1/4">
        <h3 class="mb-4 text-lg font-semibold">Quick Links</h3>
        <ul class="[&>li]:mb-2 [&>li>a]:text-gray-300 [&>li>a]:hover:text-white [&>li>a]:transition-colors">
          <li><a href="#">About Us</a></li>
          <li><a href="#">Services</a></li>
          <li><a href="#">Blog</a></li>
          <li><a href="#">Contact</a></li>
        </ul>
      </div>

      <!-- Social Media -->
      <div class="md:w-1/4">
        <h3 class="mb-4 text-lg font-semibold">Connect With Us</h3>
        <div class="flex [&>a]:mr-4 [&>a]:text-2xl [&>a]:text-gray-300 [&>a]:hover:text-white [&>a]:transition-colors">
          <a href="#" aria-label="Facebook"><i class="fab fa-facebook"></i></a>
          <a href="#" aria-label="Twitter"><i class="fab fa-twitter"></i></a>
          <a href="#" aria-label="LinkedIn"><i class="fab fa-linkedin"></i></a>
          <a href="#" aria-label="Instagram"><i class="fab fa-instagram"></i></a>
        </div>
      </div>

      <!-- Newsletter Signup -->
      <div class="md:w-1/4">
        <h3 class="mb-4 text-lg font-semibold">Subscribe to Our Newsletter</h3>
        <form class="[&>*]:mb-2 [&>*:last-child]:mb-0">
          <input 
            type="email" 
            placeholder="Enter your email" 
            class="w-full px-4 py-2 text-white bg-gray-700 rounded focus:outline-none focus:ring-2 focus:ring-blue-500"
          >
          <button 
            type="submit" 
            class="w-full px-4 py-2 font-bold text-white transition-colors bg-blue-500 rounded hover:bg-blue-600"
          >
            Subscribe
          </button>
        </form>
      </div>
    </div>
  </div>
</footer>

![https://imgur.com/B6XHhRx.png]

In this example, we've used several instances of the arbitrary value syntax to style our footer:

  1. For the main container:
  1. For the company info section:
  1. For the quick links section:
  1. For the social media section:
  1. For the newsletter form:

Conclusion

Tailwind CSS v3's arbitrary value syntax provides a powerful and flexible way to target and style direct children of an element. This approach allows you to:

  1. Apply styles to all direct children using [&>*]:{class}.
  2. Target specific direct children using class or element selectors like [&>.classname]:{class} or [&>element]:{class}.
  3. Combine multiple selectors for more complex styling needs.

If you're looking to improve your Tailwind CSS development process even further, check out Tails by DevDojo. This powerful page builder allows you to visually create stunning, responsive Tailwind CSS websites with drag-and-drop ease, making it an excellent tool for both beginners and experienced developers alike.

Happy coding!

Bobby

© 2023 Bobby Iliev - Personal Blog

Facebook 𝕏 GitHub YouTube Instagram