Struggling to apply a background image to a specific section with the help of Tailwind CSS in a Next.js project using TypeScript

I've been struggling to add a background image to a section despite trying various solutions I found on Stackoverflow. Here is the current code that I have:

import Image from '../../public/images/header2.jpg';
  
  const Home: React.FC = () => {
  
    return (
        <main className="super-donnie-bg-slate">
            <section id="first" className="bg-no-repeat bg-bottom bg-cover px-4 md:px-8" style={{ backgroundImage:`url(${Image})`,backgroundRepeat:"no-repeat" }}>
                <div className="container max-w-xs w-full md:max-w-sm lg:max-w-md xl:max-w-lg mx-auto pt-24 pb-96 "><img className="mb-8 max-w-full w-64 sm:w-full mx-auto" src="/images/fungifimage.gif" alt="Super donnie"></img>
                    <div className="mb-8">
                        <div className="super-donnie-text-yellow flex justify-between w-full mb-4"></div>
                        
                    </div>
                    <p className="text-center text-2xl leading-tight super-donnie-font-press-start-2p ">text here.</p>
                
                </div>
            </section>

Currently, I can display images within a div successfully, but setting a background image for the section remains a challenge.

Answer №1

discovered the solution by incorporating images into the tailwind.config.js file in the following manner:

backgroundImage: {
        'main-hero' : "url('../public/images/header.jpg')",
        'custom-bg' : "url('../public/images/bg-custom.jpg')",
        'product-bg' : "url('../public/images/bg-product.jpg')"
      }

Answer №2

To apply styles using tailwind css, utilize the className property and ensure to specify width and height while closing the img tag within the opening tag.

bg-[url('/img/hero-pattern.svg')]

<div class="bg-[url('/img/hero-pattern.svg')]">
  <!-- ... -->
</div>

If you have a unique aspect-ratio requirement that is not part of your theme settings

aspect-[4/3]

Your code snippet:

       <section id="first" className="bg-cover px-4 md:px-8   bg-[url(`${Image}`)]  bg-no-repeat     aspect-[4/3] w-full">
            <div className="container max-w-xs w-full md:max-w-sm lg:max-w-md xl:max-w-lg mx-auto pt-24 pb-96  aspect-[4/3]"><img className="mb-8 max-w-full w-64 sm:w-full mx-auto" src="/images/fungifimage.gif" alt="Super donnie"/>
                <div className="mb-8">
                    <div className="super-donnie-text-yellow flex justify-between w-full mb-4"></div>
                    
                </div>
                <p className="text-center text-2xl leading-tight super-donnie-font-press-start-2p">text here.</p>
            
            </div> </section>

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

PHP file unable to access Bootstrap file

I have my bootstrap css file stored in the following directory: C:\Users\SCEA\Downloads\bootstrap-3.3.7\css When linking this css file to my php file, I used the absolute path: <link href="C:\Users\SCEA\Downl ...

HTML - maintain centered text positioning while implementing padding on the left side

Here are the HTML and CSS code snippets I am working with: #page { background-color: #000; color: #fff; height: 360px; width: 360px; min-height: 100%; } #viewport { font-size: 20pt; text-align: center; } .taskTitle { height: 13%; fon ...

Expand the range of input movement using a unique and oversized custom thumb graphic

In my project, I am creating a personalized input range feature by utilizing a 40px x 80px image as the background for the thumb. Typically, the thumb is limited to moving within the length of the track from edge to edge. However, I am aiming for the thu ...

Chrome and Firefox: Images cling together

I've encountered an issue with my recently launched website. Some images in a grid appear to be stuck together, but this problem only occurs in Firefox and Chrome browsers. Oddly enough, zooming in to around 110% then back to 100% seems to temporarily ...

Learn how to fetch data from PHP using XMLHttpRequest in ReactJS and then display that data within a sibling component

Currently in the process of developing a ReactJS web application. Absolutely no JQuery is being utilized. Whenever ReactJS detects an onTouchEnd event, it initiates an XMLHttpRequest to communicate with my php server. PHP then conducts a loop through a M ...

Looking to align a div in the middle of a parallax background image?

I'm trying to center a div with a parallax background image both horizontally and vertically on the page. I attempted using classes from Bootstrap 4 such as d-block, mx-auto, and text-center. While it centered the div horizontally, it did not center i ...

Unexpected behavior: Bootstrap columns collapsing into a single row

I recently started learning how to utilize Bootstrap columns and encountered a minor issue. Whenever I shrink my browser window to the smallest size possible (or even halfway), causing the CSS rules for the columns to take effect, instead of adjusting to ...

What is the method for superimposing a div with a see-through input field?

I am currently designing a compact upload feature. It consists of a responsive element (actually a vue/quasar card) that adjusts in size according to the window dimensions and viewport. Within this element, there is a form containing an input field and a ...

Ways to achieve a gradient effect on top of an image

Hey, I'm trying to implement a gradient on images in my project. Here's what I have so far: import Image from 'next/image' import Box from 'mui/material/Box' import PlayIcon from './PlayIcon.tsx' <Box ...

Collapse all "Read More" buttons simultaneously using Bootstrap

I am attempting to design a segment that contains multiple paragraphs that can be expanded or collapsed individually. How can I achieve this using only pure bootstrap css so that they do not all expand and collapse simultaneously? #summary { font-size: ...

Activate a large sub menu upon hover starting from the first `li` using CSS

I have an issue with the menu on my website. The main menu is working fine, but the sub-menu does not start from the first list item, which is needed for it to function as a Mega menu. I attempted to fix this by adding the following code: #ayurmenu ul li ...

The functionality of the "Slots" prop has no impact when used on the material-ui Slider component

Trying to understand the purpose of the "slots" prop in relation to customizing how inner components like track and thumb are rendered within the Slider component. A basic example of rendering a Slider component is shown below const marks = [ { value: 0 ...

Click on the link that surrounds an inline-block span in Internet Explorer

In Chrome and Firefox, the following code works fine and makes the container clickable. However, in Internet Explorer, only the inner div changes the cursor to indicate that it's clickable, not the span. I could use cursor:pointer to fix this issue, ...

Placing two tables in an HTML document

I am working on integrating two tables into my web application and I would like them to be positioned on the same row horizontally. <span><table><tr><td>A</td><td>B</td></tr></table></span> <s ...

Steps for adding multiple images to a page in a React application:1. Create a

Having trouble displaying images on a React page. I have a directory with 30 images that I want to display on the page (.jsx file). Instead of exporting each image individually, is there a faster way to accomplish this task? Any ideas or suggestions would ...

Ways to eliminate bottom margin of text area in vuetify

Greetings, I am facing an issue with the following code snippet: <v-text-field :rules="rules" v-model="exam.title" class="exam-title ma-0 pa-0" ></v-text-field> <v-text-field class="exam-title ma-0 pa-0"></v-text-field& ...

Adjusting the background element of a fullpage.js layout during resizing and customization

Is there a way to make the background image responsive on a fullpage.js page, specifically for mobile and tablet devices? How can I ensure that a specific part of the image stays at the center of the page when resizing? For instance, in the provided imag ...

unable to properly display fullcalendar using react server rendering

I'm experiencing an issue with fullcalendar and React.js server rendering (NextJS). When I directly access the calendar page at , I receive the error message attached below. However, if I access other components first and then navigate to the calendar ...

Having trouble retrieving payload from emitted event in Socket.io

I am dealing with a socket setup that includes an authentication server structured like this: const io = require('socket.io').listen(3002); const jwt = require('jsonwebtoken'); io.use((socket, next) => { if (socket.handshake.query ...

Split Screen Text Comparisons

For some reason, I am finding it challenging to display two paragraphs side by side without disrupting the overall layout. If you would like a visual explanation of the issue, please check out this video: Video If you're interested in reviewing the c ...