What is the best method to dual-layer backgrounds in Tailwind without causing any conflicts between them?

I want to create a unique design where one container acts as an accent for another. Essentially, I have a styled text box and underneath it, I want a similarly sized container with a different background color that can be positioned in the bottom-right corner to serve as an accent. Although I have made some progress, here is my closest attempt so far: (Please ignore the colors)

          <div className="space-y-6">
            <p className="relative font-dmSans text-md leading-tight text-white-classic bg-yellow-600 mt-2 z-20 p-4 ">
              <span className="absolute bottom-3 right-3 h-full w-full bg-black -z-10 "></span>
              This is the content of my text.
            </p>

Although this setup achieves some of what I want, the main issue is that it positions the text inside the box, which is expected. My ultimate goal is to position the yellow box behind the black one. I've tried other methods, but they resulted in the containers clashing or interfering with each other due to my limited experience with before: and after: pseudo-elements. Another reason I opted for this approach is because it didn't disrupt my overall styling structure.

Is there a better way to achieve this shifting effect with the yellow box positioned behind the black one while maintaining the desired aesthetics?

I attempted to use z-index within a layered container/accent effect and experimented with Tailwind's before: and after: classes, but encountered issues such as the background shining through the container and z-index not behaving as intended.

Answer №1

It seems like what you're looking for is the desired outcome described below:

<script src="https://cdn.tailwindcss.com/3.4.5"></script>

<div class="relative m-4 space-y-6">
  <div class="absolute left-2 top-2 h-full w-full bg-yellow-400"></div>
  <p class="font-dmSans text-md relative bg-black p-4 leading-tight text-white">
    Here goes the content of my text.
  </p>
</div>

To achieve the offset effect, you can use absolute to position the yellow element relative to its first relative ancestor. Just keep in mind that your <p> element must also be set to relative; otherwise, it will be positioned behind any sibling elements with the absolute property.

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

A variety of negative () DOM Selectors

I've been trying to select a specific node using two not clauses, but so far I haven't had any luck. What I'm attempting to achieve is selecting an element whose div contains the string 0008, but it's not 10008 and also does not contain ...

Leveraging the power of JavaScript and possibly regular expressions to extract numerical values from a

I am attempting to extract a variable number from the text provided in the following example: Buy 5 for € 16.00 each and save 11% Buy 50 for € 15.00 each and save 17% Buy 120 for € 13.00 each and save 28% Buy 1000 for € 10.00 each and save 45% Th ...

Arranging nested columns with Bootstrap 4 ordering techniques

Having an issue with the order-[num] not functioning properly due to different columns on the left and right sides. Here is a screenshot of the view: https://i.sstatic.net/8feaj.png https://i.sstatic.net/DJZpN.png <section class="row justify-content ...

How can you style the modal image to center it? Which <div> should you target for styling?

I'm facing an issue with my modal image within a Bootstrap 4 carousel that uses JQuery. When I open the modal, it doesn't appear centered on the screen. Could you advise me on which parent div to style and how? Additionally, how can I ensure the ...

The issue of React UseEffect not functioning properly in conjunction with firepad and firebase has been identified

When attempting to utilize the username fetched from Firebase to create a user in the FirepadUserList, the code resembles the following: import { useRef, useEffect, useState } from 'react'; import 'codemirror/lib/codemirror.css' impo ...

Only fire the click event of the parent div if none of the child elements are clicked

Here is a fiddle I've prepared for reference: https://jsfiddle.net/9m4bgyq8/16/ The issue I am facing is that I am attempting to wrap a checkbox and label within a div, and apply a click event to the div. The intention is for the click event to only ...

Retrieve an array of objects by matching a specific object id

Seeking assistance for a React project centered around a quiz application. Within my database (currently utilizing Firebase Realtime Database), I have a collection of questions and another collection of answers. Questions are identified by an Id, title, an ...

Trouble with targeting a specific css class

Can someone help me with a CSS issue I'm experiencing? Here is my code: <div class='test'> <div>test 1</div> <div>test 2</div> </div> <div class='test'> <div>test 3&l ...

Using React and Typescript: Populating an array within a For Loop using setTimeout is not happening sequentially or at all

I'm currently working on a function that animates images of random cars moving across the screen. My goal is to stagger the population of the "carsLeft" array using setTimeout, where I will eventually randomize the delay time for each car. Everything ...

Converting Antdesign's Datepicker to Typescript

I'm having trouble figuring out how to properly annotate the dateObj parameter in the handleDateChange function that I've created. App.tsx import { useState } from 'react'; import logo from './logo.svg'; ...

Show Zeroes in Front of Input Numbers

I am working with two input fields that represent hours and minutes separately. <input type="number" min="0" max="24" step="1" value="00" class="hours"> <input type="number" min="0" max="0.60" step="0.01" value="00" class="minutes"> This se ...

Ways to retain a variable's value beyond the scope of a loop in PHP

<?php //establishing connection with the database define('DB_HOST', 'localhost'); define('DB_NAME', 'visitor_list'); define('DB_USER','root'); define('DB_PASSWORD',''); ...

Adjust the height of each child element to be half of the fixed height of the parent

There is a wrapper containing multiple boxes generated using ng-repeat from a list. The wrapper has a fixed height of 300px. If the list contains only one box, a class is added to fill the entire space of the wrapper. However, when there are more than on ...

Maintaining uniformity in Python by assigning and populating empty values for any missing HTML tags to ensure consistency across structures

Here are the HTML lines I currently have: <nonDerivativeTable> <nonDerivativeHolding> # First Holding <securityTitle> <value>Common Stock</value> </securityTitle> <ownershipNat ...

Fetch function call results in an undefined value - Using Express and React

Struggling with an issue while following the stripe tutorial directly from the official stripe documentation. I keep receiving an undefined value from my server on a post request. Can someone help me identify where I am going wrong? When the user clicks t ...

Create functionality for editing and reviewing content within an HTML webpage

Looking to add edit and review modes to my webpage. In edit mode, users can drag and drop divs to different locations or resize them. Once changes are made, users may want to review their work. I have included a link for the user to switch to review mode ...

Struggling to modify the background color of carousel captions using BootStrap4

I'm having trouble changing the background color of a basic carousel-caption element. Upon inspecting in the debugger, I keep getting the warning "Invalid property value," but I can't pinpoint the reason why. I find it odd that the 'backgr ...

Make SVG Path (Vector Graphic) Simplification easier

Provided Input: <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="800pt" height="600pt" viewBox="0 0 800 600"> <g enable-backgrou ...

"Effortlessly incorporate a new contact with Material UI's latest simple dialog

I've been exploring the material-ui next branch and using a specific example as a guide to create a dialog. For more information, you can check out the example here: You can also view the code sandbox here: https://codesandbox.io/s/7rq8nl11x I&apos ...

React encountered a parsing error due to an unexpected keyword 'this'

Having trouble with the line that includes "this.props.isCompleted?" and it's giving me an error about 'this' being an unexpected keyword. I'm using a class component and this is an old project I revisited today after a year, where it w ...