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 methods can I use to reduce the input field height while using floating labels?

Is there a way to reduce the input height when using a floating label? I attempted to adjust the size property but it had no effect. <Col xs={5} md={2}> <FloatingLabel size="sm" controlId="floatingInput" l ...

Implement a function that runs upon Page Load using Javascript

Here is some Javascript code that loads a map with different regions. When you hover or click on a country, additional information about that country is displayed on the right side of the map. I would like to have a random country already displaying infor ...

What is the best way to show a table with 100% width in the Chrome browser?

I am currently working on debugging and expanding an express application that showcases a dataset through a series of nested tables on a webpage. Initially, all the CSS resided within style tags in the head section of the HTML file, and the tables were dis ...

CSS experts caution that the addition of margin and padding can cause the screen to jump

When I apply margin and padding to this class, it starts glitching like in the second GIF. However, if I remove margin and padding everything works fine, but I do need them for styling purposes. What could be causing this issue and how can I resolve it? ...

Issues with DropDown and ListBox functionality in Next.js when using TailwindCSS and HeadlessUI

I encountered an issue with the dropdown menu. Whenever I click on it, the dropdown opens and immediately closes again. I set up TailwindCSS and HeadlessUI according to the documentation. I used DropDown component from HeadlessUI docs. Initially, I suspect ...

Incorporating an external SVG link into a React application

While I may not be an SVG expert, I haven't encountered any issues with loading SVGs in my React app so far. I prefer using the svg tag over the image tag because sizing tends to present problems with the latter option when working with external links ...

How to Efficiently Display Multiple Modals with React Components

I'm currently navigating the world of React and coding in general. I am facing an issue where multiple modals are being rendered simultaneously within the same component, creating a confusing display where it seems like all links trigger the content f ...

How to Turn Off Elevation for Buttons in React Using Material-UI

I was looking to remove the default button elevation in material-ui when using React. Unfortunately, the following code does not appear to be effective: const customTheme = createTheme({ components: { MuiButton: { defaultProps: { disa ...

Getting the hang of revealing a div through flashing

Is it possible to make a div flash using CSS alone? My goal is to have this div alternate between two colors. .chat-window .msg-container-base .notification-message-unread{ float: right; font-size: 10px; color: #666; background: white; padding ...

Caching Data at Regular Intervals (Using Redis and Node.js)

I am looking to optimize my app for speed by caching data retrieved from my MYSQL database every 10 minutes. As a beginner in fullstack development, I am wondering how I can achieve this using Redis? Below is the index.js file located in my server directo ...

Challenges arise with dependencies during the installation of MUI

[insert image description here][1] I attempted to add mui styles and other components to my local machine, but encountered a dependency error. How can I resolve this issue? [1]: https://i.stack.imgur.com/gqxtS.png npm install @mui/styles npm ERR! code ERE ...

What is the process for assigning chat memory to the openai model?

I've been working on a project with Django where I'm using the OpenAI API in one of my views. In the frontend, I'm utilizing React to create a chatbot similar to ChatGPT. My goal is to have a record of the conversations just like ChatGPT doe ...

Adjusting book cover size based on screen dimensions using Express and React

I am currently developing a website where users can browse and read books. The homepage features multiple book covers, but I want these covers to adjust their size based on the display dimensions without cropping them. Additionally, I am utilizing the MUI ...

Creating a draggable element in JavaScript using React

I've been attempting to create a draggable div element, but I'm encountering some perplexing behavior. The code I'm using is directly from w3schools and it functions correctly for them, however, in my implementation, the div always shifts to ...

Adding a JSX file type to the "File > New" list in WebStorm is a simple process that can enhance your development workflow

Is there a way to customize the items on this list? I am specifically interested in adding a jsx file type to it. ...

The Facebook Comments feature on my React/Node.js app is only visible after the page is refreshed

I am struggling with getting the Facebook Comment widget to display in real-time on my React application. Currently, it only shows up when the page is refreshed, which is not ideal for user engagement. Is there a way to make it work through server-side r ...

Is there a way to determine whether all fields in a schema have been populated or remain empty?

I am working with a schema that looks like this: How can I determine if all the fields in the schema have been filled or not? The front-end (React.js) includes an onboarding process where users who are logging in for the first time need to complete onboa ...

Dealing with unresponsive sidenav in MaterializeCSS when trying to click and pop-out with React

One particular challenge I am encountering relates to user experience when it comes to authentication. If a user is authenticated, the AuthLinks will be displayed, but if not, the GuestLinks will show instead. The issue arises when switching to responsive ...

I want to create a feature in my Reactjs app styled with Material UI that allows the modal to close when the user moves their

I am facing an issue with a modal in my project. The modal opens onMouseEnter, but I am struggling to close it onMouseLeave when the user stops hovering over the button. Below is my component code: I have attempted to attach an event listener, onMouseLea ...

Using a variable obtained from React redux useSelector in the dependency array of useCallback leads to an endless cycle of re-render

I have a list called topics that I retrieve using useSelector. I am using this list in a callback function named updateXXX, and including it in the dependencies array of useCallback is causing infinite rendering. Can someone provide some suggestions on how ...