The Wayback Machine - https://web.archive.org/web/20240717024349/https://www.geeksforgeeks.org/how-to-access-all-direct-children-of-a-div-in-tailwind-css/
Open In App

How to Access All Direct Children of a Div in Tailwind CSS ?

Last Updated : 02 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to learn how to access all the direct children of a div in Tailwind CSS. In Tailwind CSS, there is no specific utility class or built-in mechanism to directly access all the direct children of a div element. Tailwind CSS primarily focuses on providing utility classes for styling elements, rather than complex selector support.

Tailwind CSS is a popular utility-first CSS framework known for its simplicity and flexibility. When working with a div container, you may encounter scenarios where you need to access and style its direct children selectively.

There are several methods that can be used to access all the direct children of a div in tailwind CSS.

  • Using arbitrary values
  • Using @layer directive

Approach 1: Using arbitrary values

The arbitrary values are the custom values that can extend or override the default utility classes provided by Tailwind. Here, we are going to use arbitrary values to target the children of a div directly and then apply the styles using Tailwind CSS.

Syntax:

[& > *]: tailwind-classes

Example: The below example demonstrates accessing the direct children of a div in tailwind CSS using arbitrary values.

In the below example, we are using the Tailwind CDN to solve the problem, so there is no need to install Tailwind CSS.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <script src="https://cdn.tailwindcss.com"></script>
</head>
  
<body>
    <h1 class="text-4xl 
               text-green-600 
               text-center font-bold">
        GeeksforGeeks
    </h1>
    
    <h2 class="text-3xl">
        How to access all the direct children 
        of a div in Tailwind CSS
    </h2>
    
    <div class="my-container [&>*]:bg-green-600 
                text-center">
        <p>Java</p>
        <p>C++</p>
        <p>Python</p>
        <p>JavaScript</p>
    </div>
</body>
  
</html>


Output:

h11

Approach 2: Using @layer directive

In this approach, we will use the @layer directive as it allows us to organize the CSS styles into different layers, making it easier to manage and maintain our stylesheets.

Syntax:

@layer base {
div.section > div {
@apply text-lg;
}
}

As the @layer directive requires the tailwind CSS to be installed locally, so we now have to create a project for HTML and CSS so that we can access all the direct children of a div. Below are the complete steps:

Step 1: Create a project

mkdir myproject
cd myproject

Step 2: Make a new src folder and install Tailwind

npm install -D tailwindcss
npx tailwindcss init

Step 3: Configure Tailwind paths

module.exports = {
content: ["./src/**/*.{html,js}"],
theme: {
extend: {},
},
plugins: [],
}

Step 4: Create an HTML and CSS file and add the below code to the CSS file

@tailwind base;
@tailwind components;
@tailwind utilities;

Step 5: Add a @layer directive

@layer base {
.class-name > html_element {
@apply styles;
}
}

To run the above code, start the Tailwind CLI process by opening the package.json and modify the file as below:

"scripts":{
"dev":"npx tailwindcss -i ./styles/tailwind.css -o ./styles/styles.css --watch"
},

Example: The below example demonstrates accessing the direct children of a div in tailwind CSS using layer directive.

index.html

HTML




<!DOCTYPE html>
<html>
  
<head>
    <title>GFG</title>
    <link href="/styles.css" rel="stylesheet" />
</head>
  
<body>
    <h1 class="text-center text-4xl 
               font-bold text-green-600">
        GeeksforGeeks
    </h1>
    <h2 class="text-3xl">
        How to access all the direct children
        of a div in Tailwind CSS
    </h2>
    <div class="my-container text-center">
        <p>Java</p>
        <p>C++</p>
        <p>Python</p>
        <p>JavaScript</p>
    </div>
</body>
  
</html>


styles.css file

CSS




@tailwind base;
@tailwind components;
@tailwind utilities;
  
@layer base {
    .my-container>p {
        @apply text-2xl text-green-400;
    }
}


Run the server:

npm run dev

Output:

h22



Similar Reads

How to select only direct children from element with Sass?
Introduction:Sass is a scripting language that is compiled into Cascading style sheets (CSS). It is a kind of preprocessor language. It was initially designed by Hampton Catlin and then it was developed by Natalie Weizenbaum. After its initial versions, Weizenbaum and Chris Eppstein have continued to extend SASS with SassScript. It supports four da
2 min read
How to overlay one div over another div using CSS
Creating an overlay effect simply means putting two div together at the same place but both the div appear when needed i.e while hovering or while clicking on one of the div to make the second one appear. Overlays are very clean and give the webpage a tidy look. It looks sophisticated and is simple to design. Overlays can create using two simple CS
2 min read
How to Make a Child Div Element Wider than Parent Div using CSS ?
When the child div element has a fixed width that is wider than the parent div element, it will display overflow outside of the parent element. Because the overflow is hidden by default, one approach is to utilize the overflow attribute to enable it on an element. Another approach includes setting the position property of the parent div element to
2 min read
How to clear all div's content inside a parent div ?
In this article, we are given an HTML document containing div elements and the task is to remove the existing HTML elements using jQuery. To accomplish this task, we can use the following methods: Table of Content jQuery remove() methodjQuery empty() methodjQuery html() methodjQuery remove() methodThe remove() method in jQuery is used to remove the
2 min read
How to display button on hover over div in Tailwind CSS ?
Tailwind CSS provides a huge number of CSS utility classes that allow you to quickly apply to style and easily construct custom designs. In this article, we’ll see how to display the button when hovering over the div in TailwindCSS. Example 1: Create a &lt;div&gt; element and apply group and relative CSS classes to make it a parent element and to p
2 min read
How to align div vertical across full screen in Tailwind CSS ?
You can easily align div vertical across the full screen using flex property in Tailwind CSS. Tailwind uses justify-center and items-center property which is an alternative to the flex-property in CSS. Syntax: &lt;div class="flex h-screen justify-center items-center"&gt; . . . &lt;/div&gt; flex property: h-screen: It makes an element span the entir
2 min read
How to make div fill full height of parent in Tailwind CSS ?
Tailwind CSS is a utility-first CSS framework that provides various pre-designed classes for easy and efficient styling of HTML elements. Sometimes, we may encounter a scenario where we need to make a div fill the full height of its parent element. In such a case, we can use Tailwind CSS classes to achieve this goal. To make a div fill the full hei
4 min read
How to make Transparent Input Field on a Transparent Div in Tailwind CSS ?
To make an HTML input field transparent on a transparent div using Tailwind CSS, apply the bg-transparent class to both the input field and the div. This ensures that the background of both elements is transparent, allowing the underlying content or background to show through. Syntax:&lt;div class="bg-transparent"&gt; &lt;input type="text" class="b
1 min read
How to make Div on Top of Another in Tailwind CSS ?
Positioning elements in Tailwind CSS allows for creating complex layouts with ease. One common requirement is overlaying one div on top of another. Below are the approaches to make div on top of another in Tailwind CSS: Table of Content Absolute Positioning and Z-indexTailwind CSS Utility Classes for Absolute PositioningAbsolute Positioning and Z-i
2 min read
How to force child div to be 100% of parent div's height without specifying parent's height?
When designing a website, you may want to create a layout where a child div element fills up the height of its parent div element. However, you may not know the exact height of the parent div element and you don't want to specify it. In this article, we will explore different approaches to force a child div element to be 100% of its parent div elem
3 min read
three90RightbarBannerImg