What is the best approach to styling a sub-child element within the first child using TailwindCSS?

Currently working with React (Next.js) and TailwindCSS.

<ul>
  <li>
    <h1 className="bg-red-400">first</h1>
  </li>
  <li>
    <h1 className="bg-green-400">second</h1>
  </li>
  <li>
    <h1 className="bg-green-400">third</h1>
  </li>
  <li>
    <h1 className="bg-green-400">four</h1>
  </li>
</ul>

I'm interested in creating a TailwindCSS class that will specifically change the background color of the first child's subchild within the <ul> element, like so:

ul > li::first-child > h1

Answer №1

Targeting the h1 element with a default Tailwind CSS class is not possible, but you can target the first li element using pseudo-class references.

<li className="first:bg-red-400 first:text-black">

If feasible, it is better to avoid this approach as Tailwind recommends, but you can also create a custom class that utilizes Tailwind's styles.

@layer components {
 ul > li::first-child > h1 {
  @apply bg-red-400 text-black;
 }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble getting the full width of a hidden div to work properly?

I am facing an issue with a two-part menu design: the left side consists of a traditional UL, while the right side contains a link within a div element. The fixed-width of the right div is causing complications as the left div needs to occupy all remaining ...

Ensuring a Fixed Delta Tooltip in lightweight-charts is essential for maintaining a seamless

I have implemented lightweight-charts to present a chart using mock data. I am looking to establish a fixed Delta Tooltip on the chart when the page loads, with predetermined start and end dates that are specified as input. Furthermore, I aim to restrict t ...

Starting a new React project

prtScn I'm encountering an issue while trying to initiate a new react app in Visual Studio Code. I have been following the given instructions as follows: in the visual code terminal : create-react-app sali (sali is the name) npm install node node_m ...

npm encountered an error: EPERM - Unable to perform the operation 'RENAME' due to insufficient permissions

Every time I run npm install, I encounter the following error: npm ERR! code EPERM npm ERR! syscall rename npm ERR! path C:\Users\Vaneeza10698\Downloads\ServiceQualityFinal\react-reduction\node_modules\npm\node_modul ...

Encountering a Hydration error when attempting to utilize react-player

How are you doing today? I've been attempting to integrate react-player into my Next.js application without success. The snippet of code looks like this: import ReactPlayer from "react-player"; const Home = () => { return ( <div> ...

Discover how to apply unique styles to specific HTML elements using their index values in jQuery

In the process of creating a party game similar to Tambola or Housie, I am faced with the challenge of automatically detecting and highlighting the numbers called out on each ticket in the game. For instance, if number 86 is announced, I aim to visually di ...

Is there a way to horizontally align my navigation bar links with CSS while maintaining grey borders on the sides?

What is the best way to center my navigation bar links using CSS without losing the grey sides? Both Blogs and History have dropdown menus. Check out this screenshot: (source: gyazo.com) CSS: .navbar ul { list-style: none; margin: 0; pad ...

Encountered an npm ERR while executing the React project

I'm encountering an issue with running my React project. When I use the command "npx start", I receive the following error message. Can someone please assist me with this? npm ERR! could not determine executable to run npm ERR! A detailed log of thi ...

Is there a way to verify if an object exists in a Mobx observable array?

I'm implementing the indexOf method within a React component to style a button based on whether an object is present in a MobX observable array. The button serves the purpose of favoriting. It adds the object related to that specific list item into a ...

Animation of active chat list items on Whatsapp

Has anyone figured out a simple method to create an animation like the one in Whatsapp? For example, when you are on a chat screen and go back to the chat list, have you noticed how an active element is briefly highlighted in gray (to indicate which chat ...

Exploring deep nested components and elements in Angular for a targeted specific functionality

Is it possible to apply the ng deep css class to only one specific checkbox in my component, rather than all checkboxes? I want to customize just one checkbox and leave the others unchanged. How can this be achieved? Thank you. I need the CSS modificatio ...

Checking for grammar and spelling errors using Node.js

How can I incorporate spell checking, grammar and punctuation error detection, greeting usage, short keyword analysis, and time tracking for each chat session in my socket.io express mongoose chat application? Although I have successfully implemented a sp ...

Syntax found within the function_name()()

Explore more about the withStyles API on material-ui The final line of code from the example is: export default withStyles(styles)(MyComponent); According to the withStyles API documentation, the syntax for withStyles() function is: withStyles(styles, ...

MERN stack tutorial: How to modify a particular string in an array's object

When it comes to connecting my backend (Express) server to the database using mongoose, I am facing an issue with performing CRUD operations. While I can easily access and update direct data in objects, I need a solution to manipulate array data as well. ...

Can anyone suggest a more efficient approach to handling variations using CSS?

Within the message component, there are currently only two variants available: error and success. This project is built using vue3 script setup and utilizes SCSS for styling. <script setup lang="ts"> defineOptions({ name: 'Notificat ...

Synchronizing two navigation menus on a single-page application website

Let me start by saying that I specialize in back end development and am facing a specific challenge with building a website that includes two navigation menus. The main navigation menu features links like Home, while the sub-navigation menu includes option ...

Tips for resetting Material UI Autocomplete back to its default value when the reset button is clicked

Currently, I am implementing Material UI Autocomplete in React to create a dynamic dropdown list. My goal is to reset the autocomplete value to its default when the reset button is clicked. Despite my efforts to search for a solution, I have not had any su ...

Reduce the dimensions of a CSS attribute automatically

Displayed below is the image with a width of 192 and height of 109: <a href="https://www.blogger.com/u/1/blogger.g?blogID=4402911157743442948#"><img alt="" class="thumbnail" height="109" src="https://2.bp.blogspot.com/-E5ftfbCCgkw/XjnLpO347cI/AAA ...

Is there a way to align this in a horizontal inline block?

I have this element and I would like it to display inline-block but horizontally. Currently, it is displaying as inline-block but vertically. Here is my HTML: <div class="ondisplay"> <div class="left-text"> <p&g ...

Is it possible to modify the default behavior of a sensitive region within a button?

I created a calculator application in React and overall, it's working fine, however... I've noticed that when I hold a click longer, it only registers as a click if the mouse was pressed down and released on the button itself. Although I unders ...