What is the best way to incorporate a splatter image within an MDX blog post?

I am working on an MDX blog where I render a blog post.

pages/index.tsx

<main className="flex min-h-screen dark:bg-black">
                <div className="wrapper mb-24 gap-8 px-8 lg:grid lg:grid-cols-[1fr,70px,min(70ch,calc(100%-64px)),70px,1fr]">
                    <div className="mdx-wrapper prose relative col-[2/5] max-w-full dark:prose-light lg:grid lg:grid-cols-[70px_1fr_70px]">
                        <MDXLayoutRenderer mdxSource={props.code as never} />
                    </div>
                </div>
            </main>

The component MDXLayoutRenderer is responsible for inserting the MDX content into the page.

index.mdx

This is the title

import { Splatter } from '../../components/Splatter.tsx'

This is a block of text with some random content...

<Splatter />

Another block of text with more gibberish...

components/Splatter.tsx

export const Splatter = () => (
    <div className="relative inset-0 h-64 w-64 bg-[url('/splatter.jpg')]">
    </div>
)

public/splatter.jpg


In my project, I have encountered an issue where the image overlapped the text in index.mdx or behaved like a regular div, pushing down the content that followed it.

My goal is to set the splatter image as a background rather than a foreground element.

I have tried using an img tag and setting background properties in Tailwind CSS, but neither approach worked for me. I also experimented with z-index and different positioning attributes without success.

Can someone suggest a solution to this problem?

Answer №1

If you want to experiment with layering images, consider using two images positioned absolutely with the left and right properties within a parent element set to relative positioning, and give them a negative z-index.

Here's an example:

export const Splatter = () => (
  <div className="relative -z-10">
    <div className="absolute bg-cover left-[100%] h-96 w-[500px] bg-[url('https://i.sstatic.net/J0ik2.jpg')]"></div>
    <div className="absolute bg-cover right-[100%] h-96 w-[500px] bg-[url('https://i.sstatic.net/J0ik2.jpg')]"></div>
  </div>
);

Adjust the height and width values as needed for your design. Consider hiding this on smaller screens.

Take a look at this codesandbox demo for reference.

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

What are some ways to troubleshoot the TypeScript React demonstration application in Chrome?

Debugging a TypeScript app in the Chrome debugger is a straightforward process. First, you need to configure the tsconfig.json file: "sourceMap": true, Next, install ts-node and set a breakpoint in your TypeScript code using "debugger;" ...

Adjust background color on click using JavaScript

Could someone provide me with the JavaScript code to change the background color of a page from blue to green when a button is clicked? I have seen this feature on many websites but haven't been able to write the code myself. I am specifically lo ...

Testing a particular module using React Testing Library (RTL)

When working in RTL mode, I encountered an issue where testing components under the specific module called Module.spec.js was testing all components with that same module name. However, I only want to test one module. I am using Jest as the test runner and ...

What is the method for compressing text in CSS?

I'm curious about how to condense my text so it doesn't extend beyond the page. Any ideas on how to achieve this? Issue: Solution: .Subheading{ text-align:center; font-family: 'Mukta', sans-serif; margin-top:70px; m ...

Rearrange the position of one div to be placed next to another div and assign the float property

I have numerous divs located on my webpage, including .rightColumnBar and .divAttributes. HTML <div class="mainContent"> <div class="pageContent"> <form id="createLead" class="frmDiv clear" action="/leads/create_lead.html?lead_id=3287" nam ...

NodeJS on Cloudlinux requires that the node modules for applications be stored in a distinct folder (virtual environment) designated by a symbolic link known as "node_modules"

I recently encountered an issue while trying to deploy my Nodejs/TypeScript web application on my cpanel shared hosting. The error I received stated: * Cloudlinux NodeJS Selector requires the node modules for the application to be stored in a separate f ...

Customizable form with dynamic content and interactive button within a set location

One distinct challenge not addressed in the similar inquiries below relates to a FORM component with both a variable segment and a fixed footer for submit buttons. The objective is to present: A header (a table set to 100% width including a logo and tex ...

Use React to conditionally assign values

let productCount = 1 let subItemLength = 1 let [isFetching, setIsFetching] = (productCount === subItemLength) ? ([isFetching, setIsFetching] = null) : ([isFetching, setIsFetching] = useInfiniteScroll(fetchMoreListItems)) I am attempting to conditionally ...

Combine SVGR with Material UI for a powerful design experience

Currently, I am experimenting with SVGR to convert my SVG into React components. My goal is to utilize <SvgIcon /> from Material UI and pass the converted component as a prop to it. Everything seems fine so far. However, SVGR stores these components ...

What is the best practice for incorporating CSS and JavaScript files into a PHP template?

I've created a basic template for my PHP website, but I'm struggling to find the best way to include my CSS and JavaScript files. Take a look at my index.php file below: <?php include'core/template.php'; $temp=new Template(); $sett ...

How can I arrange images vertically instead of placing them side by side?

I need help figuring out how to display multiple images in a vertical format instead of horizontally in the code below. The images are wide and take up too much space if shown side by side. My coding skills are basic, so specific guidance on where to mak ...

Resolving odd SVG anomalies

I recently exported three SVGs from Sketch to include in HTML, but one of them (specifically the Twitter icon) is presenting a mystery with its appearance. All three SVGs were created in the same manner, using #999999 as the stroke color and having a stro ...

Creating Bespoke Designs with Material UI

Upon visiting the Material UI landing page, I was impressed by its sleek design in terms of both layout and components. It got me wondering if I could implement this design instead of the default Material Design. Surprisingly, there seems to be a lack of ...

Basic let or const declarations in a React project

I am currently exploring React and I'm curious about best practices regarding the use of simple let or const variables within functional components in React. While I am aware of hooks such as useState, useRef, useMemo, and useCallback, my question re ...

Is it possible to customize the color of the placeholder and clear-icon in the ion-search bar without affecting

I am working with two ion-search bars and I need to customize the placeholder and clear icon color for just one of them. <ion-searchbar class="search-bar" placeholder="search"></ion-searchbar> My goal is to target a specific i ...

Creating a Music Bingo game to ring in the New Year

Looking to create a Music Bingo game for New Year's Eve with randomized songs that can be selected, but experiencing an issue where nothing happens when you have 4 in a row. Attempted adding a submit button, but it doesn't trigger any action. Ide ...

Utilizing numerical values in useParams - A beginner's guide

Trying to access specific data from my json file using an ID, like "http://localhost:3001/pokemons/3", leads to a 404 error. All the data is visible at http://localhost:3001/pokemons. It seems that useParams doesn't want me to use id as a number - q ...

Having trouble getting your jQuery/JavaScript function to detect the property "-webkit-clip-path"?

Exploring the clip-path feature of CSS has been a fun experiment for me. I have successfully implemented it in my project and am now trying to incorporate JavaScript to dynamically change the start/stop positions of the clip-path. Below is a snippet of my ...

Leverage the router through the getServerSideProps method

My goal is to automatically redirect the user to the login page if their token has expired. I need to handle this in the getServerSideProps method where I make a request to the server for data. If the request is unauthorized, I want to use the useRouter ho ...

Resolving "SyntaxError: Unexpected identifier" when using Enzyme with configurations in jest.setup.js

I'm currently facing an issue while trying to create tests in Typescript using Jest and Enzyme. The problem arises with a SyntaxError being thrown: FAIL src/_components/Button/__tests__/Button.spec.tsx ● Test suite failed to run /Users/mika ...