What could be causing the issue of images not loading in Chrome while they are loading perfectly in Mozilla when using CSS3?

Recently, I dove into a project utilizing react with webpack, and everything was smooth sailing until I encountered the CSS hurdle. Despite successfully configuring webpack, trouble brewed when it came to styling. Specifically, setting the background image of a div proved to be problematic in Chromium and Chrome browsers - the image simply refused to load. Strangely enough, Mozilla Firefox had no issues loading the image. I went as far as uninstalling and reinstalling Chromium, clearing browsing data, but alas, the problem persisted.

My code snippets:

Here is the CSS snippet causing the issue:


    .full-div {
      background: url("../img/slides/slide-1.jpg") center center fixed no-repeat;
      height: 100vh;
      width: 100vw;
    }

And here are my React components:


    import React, { Component } from 'react';
    import customMsg from '../utils/customMessages';

    class HomePage extends Component {
      constructor() {
        super();
        this.state = {};
      }

      render() {
        const { welcomeMessage } = customMsg;
        return (
          <div className="full-div">
            <h1>{welcomeMessage}</h1>
            <img alt="foto1" src="../assets/img/slides/slide-2.jpeg" />
          </div>
        );
      }
    }

    export default HomePage;

The challenge persists as setting the background image using CSS3 in Chrome seems futile, along with loading images using

<img alt="foto1" src="../assets/img/slides/slide-2.jpeg" />
.

Below is an excerpt of my webpack configuration:


    // webpack config goes here

Despite resetting the project with create-react-app, the issue remains unresolved. This suggests that there might not be any setup errors on my end. Could the browser itself be the culprit? Does anyone have insights on this matter, or should I consider raising an issue on the official react repository for assistance?

I stumbled upon a similar question on StackOverflow titled CSS working in Firefox but not Chrome, but unfortunately, it did not provide a solution applicable to my scenario.

Answer №1

If you want to include images in your project, consider importing them this way:

import slide-1 from '../img/slides/slide-1.jpg'

Once the import is done, you can easily use it in your JavaScript file like this:

<img src={slide-1} alt="Slide1" />

The create-react-app documentation explains this process in detail:

You have the ability to directly import a file into a JavaScript module. By doing so, webpack will add that file to the bundle for you. Unlike CSS imports, file imports provide you with a string value. This string represents the final path that you can utilize within your code, such as using it as the source attribute for an image or the href for a link to a PDF document.

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

Troubleshooting Next.js and NextAuth.js Authentication Redirect Issue

I am experiencing a problem with authentication in my Next.js application using NextAuth.js. The issue specifically pertains to the redirection after successful login. Here is an overview of my setup: NextAuth.js Configuration (app/api/auth/[...nextauth.js ...

How to retrieve all items instead of just the initial key item in React JSX by using the .map method

const dynamic_table_data = [ { id: "1", first_name: "Test", last_name: "Again", email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d3a7b6a0a793b4beb2babffdb0bcbe">[email protected]</a>", }, ...

Updating GridView row only if there are changes, the value remains unchanged in JavaScript. Disappointing

Incorporating a gridview (ASP.net) inside an update panel, I included a "Save" button that triggers a loop through all the rows of the grid view to pass data to a stored procedure for updating each row. However, this process proved to be slow and resulted ...

What is the method to access nested JSON data returned from the Angular JS $http service?

Here is the main JSON file I am using { "chartType" : ["column", "column", "pie"], "chartTitle": ["Number of teams", "Number of consumable items","Number of employees in charge"], "yAxisTitle": ["Teams", "Consumables", "Employees"], "seriesName": ...

Removing duplicate elements from an array using lodash

What is the best way to remove duplicate values from an array? var numbers = [1, 1, 5, 5, 4, 9]; I want my result to be: var numbers = [4, 9]; Is there a method in lodash that can help me achieve this? ...

Inquiring about Vue component prop defaults and detecting when a user has not assigned a value

1. I am looking for a way to set a default value for a component prop in Vue 2. Take, for example, a basic movies component that can be utilized like this: <movies year="2016"><movies> Vue.component('movies', { props: ['yea ...

Can we switch the country name to the database name in Jvector?

I've successfully implemented the Jvectormap application and it's functioning as expected. When I click on a country, it displays the name of that country. I have established a simple database for specific countries. Through AJAX, I've conn ...

Managing "unprocessed information" in a Node.js environment and transferring the information through a Node Express endpoint

Currently, I am in the process of making an API call to retrieve a file using axios: async function fetchData() { const configuration = {}; // { responseType: 'stream'}; const { response } = await axios.get(URL, configuration); c ...

What could be causing the CSS to not display properly on specific routes?

I'm facing an issue where CSS seems to be working fine for certain routes with node.js & express, but not for others. It's puzzling because the HTML file links to the same CSS file in both cases. For instance, CSS works perfectly for the route " ...

Aligning text vertically within an HTML <a> tag block

Calling all CSS experts! I'm struggling with aligning text to the bottom of a block element with a fixed width and height that contains a background image and a title. I've tried using display: table-cell; and vertical-align: bottom; without succ ...

CSS. Absolute positioning in relation to a div element

I discovered a way to adjust the positioning of a div using jQuery. Check out this example. Is it possible to achieve the same result using CSS? UPDATE: I came across this solution. I also want the horizontal window scrollbar to appear if the fixed elem ...

Having trouble integrating date-fns with material-ui/react. Constantly encountering errors that prevent it from functioning properly

Just about a month ago, I delved into the world of React. In my eagerness, I downloaded several libraries, and while most proved to be useful, one in particular left me scratching my head. I encountered some strange errors when attempting to make date-io/d ...

Comparing global variables in ng-switch: Best practices

I'm currently utilizing the AngularJS $rootScope object to expose some global constants that should be accessible to both controllers and views: var app = angular.module('myApp', []); app.run(function ($rootScope) { $rootScope.myConsta ...

Disrupting Alignment Due to Input Tag

Hey there, I'm having an issue with my header design. I have a horizontal list of links in my header, and next to them, I wanted to add a search bar. Unfortunately, when I added the search bar next to the links, they all ended up appearing behind my b ...

What is the best way to modify an image in a column when I hover over a table row that was dynamically inserted using a button function?

Is there a way to dynamically change an image in a column when hovering over a table row that was added through a button function? Here is the current code that I am using, but it does not seem to work as intended when I hover over the row. This function ...

Is it considered poor design to pass a function two levels deep? Are there other possible alternatives to achieve the same outcome?

I am currently working on a scenario involving componentA, which also contains another componentB with buttons that need to update the scene. My initial thought was to pass a function from the scene to componentB through componentA, but since I am new to ...

Error in Angular timer causing NaN result

Struggling to create a timer in JavaScript, a basic one at that. I wrote what I thought was correct code, but it's not functioning properly - resulting in the textbox value changing to NaN after just one second. Below is the code snippet: <timer ...

Scroll through the div to quickly find the relevant content

I have implemented the following HTML structure: <div style="height:200px;overflow-y:scroll;"> <table>.....</table> </div> By using this setup, I am aiming to replicate the functionality of an expanded <select> control wit ...

Inquire: How can I transfer a value from one form to another HTML document, and then retrieve and utilize the value in JavaScript?

My goal in Express is quite complex, as I need to retrieve data from the form located in 'inputdata.html' (with a ROUTE of '/inputdata'). This data will then be passed to 'info.html' (with a ROUTE of '/info') Once ...

Iterating through an array and setting variables according to asynchronous code

I have created a function to loop through an array, call a promise, and update a variable based on the result. The code seems to be functioning correctly, but I am wondering if there is a more optimal way to write it. Any suggestions are appreciated. Tha ...