Is it possible to incorporate a next.js image onto the background design?

I've encountered an issue while attempting to create a fixed background image with a parallax effect using Tailwind CSS and Next.js Image.

If you need to see an example of this in action, check out this Template Monster theme.

Here's the code snippet I experimented with:

<section className="stats w-full h-48 relative">
  <div className="absolute top-0 right-0 bottom-0 left-0 object-cover bg-cover">
    <Image
      layout='fill'
      src={data.backgroundImageUrl}
    />
  </div>
  <div className="relative z-10 flex flex-col items-center">
    <div>Stat 1</div>
    <div>Stat 2</div>
    <div>Stat 3</div>
    <div>Stat 4</div>
  </div>
</section>

Unfortunately, it didn't produce the desired parallax effect as the image is attached to the section.

As a workaround, I resorted to the following solution:

  <div className="aboslute top-0 right-0 bottom-0 left-0 object-cover bg-cover"
    style={{
      backgroundImage: `url(${data.backgroundImageUrl})`
    }}
  />

This method worked but it led to decreased image optimization and negatively impacted my SEO score according to Lighthouse metrics.

My question now is how can I effectively combine next/image with TailwindCSS to achieve a parallax effect on section backgrounds?

Answer №1

Check out this example on how to create a clipPath with a fixed position wrapper: https://stackblitz.com/edit/nextjs-ubh9ml?file=pages%2Findex.js

<div
  style={{
    position: 'relative',
    height: '50vh',
    width: '100%',
    clipPath: 'inset(0 0 0 0)',
  }}
>
  <div
    style={{
      position: 'fixed',
      height: '100%',
      width: '100%',
      left: '0',
      top: '0',
    }}
  >
    <Image
      src="https://images.unsplash.com/photo-1630496128261-27ce4ab6ad76?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2070&q=80"
      layout="fill"
      objectFit="cover"
      sizes="100vw"
    />
  </div>
</div>

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

Ensure that any relevant information is placed adjacent to a floated image

I am currently designing a responsive webpage featuring images of people's faces placed next to descriptive text. At smaller screen widths, everything looks perfect. However, as the page widens, the text for one person starts next to the image of the ...

Having trouble with WebRTC video streaming on Firefox?

I have implemented one-way broadcasting in my Dot Net MVC website for video streaming using the example found at https://github.com/muaz-khan/WebRTC-Experiment/blob/master/webrtc-broadcasting/index.html. While it works perfectly in Google Chrome, unfortuna ...

The Angular application is receiving a 404 error when trying to access the .NET Core

Having trouble calling a method in the controller via URL, as I keep encountering a 404 error. What could be the issue? API Endpoint: http://localhost:5000/Home/HomeTest /*.net core web-api*/ namespace MyApp.Controllers { Route("api/[controller]") ...

Unable to implement multiple draggable inner objects using Angular 5 and dragula library

After struggling for the past few days, I can't seem to get it to work... Here is a brief explanation of my issue: In this example, I have an array of objects structured like this: public containers: Array<object> = [ { "name": "contain ...

Enable search functionality for jQuery Select2 values that have been formatted by a formatter function

Trying to use a formatter with select2 for better alignment of code and description elements, but the plugin seems to be searching based only on the description rather than the entire text. This may be because it's only looking at the original <opt ...

An issue occurred in React 16.14.0 where the error "ReferenceError: exports is not defined" was not properly

As the creator of the next-translate library, I have encountered a perplexing issue with an experimental version involving an error specifically related to React 16.14.0. Interestingly, upgrading React to version 17 resolves the issue, but I am hesitant to ...

Having trouble with your jQuery code not loading properly?

Currently working on debugging jQuery code and facing an issue where a particular section of the code is not being triggered upon clicking. The HTML/PHP in question: $cartlink .= "<a class='add_to_cart button' data-id='{$product->id} ...

AngularJS Vimeo API Request Error: "401 Authorization Required"

I've been experimenting with making external API calls to Vimeo from my AngularJS code using $http.jsonp. However, I keep receiving a 401 Authorization required error even though I have included my authorization key in the header. I encountered a simi ...

The art of perfect alignment: Mastering the positioning of Material-UI Grid

This issue has been resolved Hey there, I'm currently working on a React App with Material-UI and I'm trying to center a Grid item that includes a Card along with two additional Grid items. Below is the relevant code snippet. Although the Cards ...

Improving the appearance of my header on mobile devices

My header needs some improvement, how can I make it look better? Here is the code snippet: 1 Index.php <div class="header"> <div class="headerFont"><a href="index.php">Yo! Yo! Honey Singh..!!</a> </div> <div class="Login ...

Unable to connect Twitter authentication in Vercel server, although it works locally (Next.JS)

I'm currently working on a next.js website and utilizing the Twitter-lite library to interact with the Twitter API. Interestingly, the code functions smoothly on my local server, but once I attempt to deploy it on Vercel for publishing, it encounters ...

Steps to establish a connection with a remote MYSQL database using the actual IP address in Node.js

In my Nodejs file, I have set up the connection as follows. The issue arises when attempting to connect to the remote database on a server. module.exports = { host: '234.32432.32432',//this is an example IP address and not a real one. use ...

Transferring data only once specific agreements have been fulfilled

Imagine having a file with specific promises that, when executed sequentially, create an input file called input.txt. // prepareInput.js var step1 = function() { var promise = new Promise(function(resolve, reject) { ... }); return p ...

Generating JSON data from a loop with refined outcomes

My goal is to iterate through JSON data and extract "time", "blocks" information, while filtering the "amounts" based on a specific variable named _miner. So far, I've successfully retrieved the name, time, and blocks data, but I'm struggling wi ...

Before inserting a string into JSON in PHP, the length of the string should be included

When sending a POST variable containing a JavaScript dictionary to a PHP file via Ajax, the value of this POST variable is being parsed as a PHP dictionary. However, I noticed that the length of each String value is added before the actual string in the PH ...

Need help logging out using the Auth0 SDK? Make sure to properly wrap your component in the <Auth0Provider> tag first!

Currently, I am utilizing the logout function from the useAuth0() hook to log out. Nevertheless, upon attempting to use it, an error is thrown: Error: "You forgot to wrap your component in <Auth0Provider>" Despite already being wrapped in ...

AngularJS: incorporating modules across distinct controllers

Imagine we have two distinct AngularJS controllers housed in separate files that are both referenced in an HTML document. Here's how it looks: //controller1.js "use strict"; var someApp = angular.module('MYAPP'); //var someApp = angular.mod ...

Unusual Characteristics of Synchronous Ajax Requests in JavaScript

First and foremost, I'd like to apologize if my approach seems unconventional. My background is primarily in C development, so I tend to tackle AJAX issues in a way that reflects my experience in C programming. The scenario at hand involves a script ...

jquery activating the toggle function to switch between child elements

I'm facing a challenge where I can't use .children() in this scenario, as it won't work since the elements aren't technically children. Here's the snippet of HTML that I'm working with: <p class="l1">A</p> ...

If you don't get the correct response from the $.ajax success function

I am utilizing the $.ajax function to retrieve content, however I am encountering an issue when attempting to display special tags from it. The data appears to be missing! This is how I am currently handling it: $(document).ready(function(){ $("button") ...