Unable to display imported image in React application

I have a component where I want to display an image:

import React from 'react'
import background from '../media/content-background.jpg'

function ContentPage(){
    return(
        <div id="content-page">
            <div id="content-header">
                <img href={background} alt='back'/>
            </div>
            <div id="content-body">

            </div>
        </div>
    )
}

export default ContentPage

The image is displaying correctly as I can see it when I check the URL in the inspector.

https://i.stack.imgur.com/00lXe.png

However, on the webpage only the alt text is appearing:

https://i.stack.imgur.com/eV2Nz.png

Answer №1

It's recommended to utilize src="" rather than href="".

The href attribute is typically used to indicate the destination of a hyperlink within an anchor tag.

Answer №2

You might encounter an issue because you have utilized the incorrect attribute within your img element; opt for src instead:

<img src={background} alt='back'/>

Answer №3

Replace the href attribute with src. The href attribute is typically used for anchor tags, while src is used for img tags.

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 is the best way to incorporate one Div within another Div using JavaScript?

I have a single main div with an id, and I am looking to insert 4 additional child divs inside it. Each of these child divs will then contain 5 more divs each. The code snippet that I currently have is as follows: $( document ).ready(function() { for( ...

Exploring NextJS: Where can I find the exportPathMap page data? It seems to be missing from getStaticProps

After configuring exportPathMap, I am encountering an issue where I receive an empty object when exporting getStaticProps from my component. I am confused about how to access the data properly. Can anyone provide guidance? // next.config.js exportPathMap: ...

When running "composer install," the Laravel/UI dependency is not automatically installed

I'm seeking insights on nodejs and npm dependencies because I am not very familiar with them. Our CI system builds docker images using a multi-stage build process: first, a node image is used for running "npm install", followed by a composer image in ...

Is it possible to use a lowercase variable name in ReactJS for a class name?

Just starting out with ReactJS and I noticed that when I declare a class like var quiz=React.createClass{}.. it doesn't work unless I change the variable to Quiz. Is this a requirement or am I overlooking something? I'm currently using JSX and ...

Exploring how to retrieve session data in Angular.js

I'm facing an issue where I can't access the session values set by node.js in my Angular.js controller. My setup involves using the Express framework. Any thoughts on how to resolve this? Below is a snippet of the code: app.use(express.cookiePar ...

How can Cypress tests effectively interact with the React application?

In my current setup, there are four unique Cypress tests identified as test1, test2, and test3. Additionally, one of the tests does not have any specific ID assigned to it. My goal is for the application to trigger three distinct APIs based on the test I ...

Trouble with React Material Select Options Failing to Populate

After iterating and producing MenuItems, I am able to see the items when I console.log, but in the UI, the dropdown appears empty. I'm puzzled as to why the Select's are not being populated. Any thoughts on this issue? Below is the supplied code ...

Command in Makefile to execute react test suite in subdirectory

I need help setting up a Makefile for my React app's test suite. The app is located in the frontend/ folder at the root level. Could someone provide me with the correct make command to run the tests? I've attempted the following commands but non ...

`Is it possible to identify errors in return values while using MVC?`

One of the challenges I encountered while trying to organize the logic in my back-end server was separating it into route/controller/service layers. The issue arises when attempting to handle errors in the service layer and then determining how to return a ...

Visual Studio Project Alert: Node.js Absent of NPM

I'm currently exploring a beginner's guide on setting up a Node.Js project in Visual Studio. After creating the project, I noticed an "npm" folder containing packages like body-parser, cookie-parser, but all marked as "(missing)". Despite repeati ...

What is the best method for invoking hook calls within React class-based components?

Is it possible to use functions such as useMediaQuery in class-based components? If so, how can this be achieved? import React from 'react'; import clsx from 'clsx'; import { withStyles ,withTheme ,withMediaQuery } from '@material ...

Using the HTML video tag to apply different background colors based on the individual machines being

My web application includes a video that is set to play against the same background color as the webpage. However, I am experiencing an issue where the video background appears differently on various machines using the same browser. What I would like to ac ...

Ways to prevent Express.js route from advancing to next()?

I encountered a strange scenario... In my web app, I am utilizing Express.js, Node.js, and Mongoose. Within one of the routes, there is a mongoose callback that triggers respond.send(...). However, as there are no further actions after the callback, it s ...

What is the best way to integrate a ttf custom font into my Rails application?

Despite following the instructions in this thread, I am unable to achieve the same results. main.css h1 { font-family: 'Shadows Into Light', serif; src: url('/assets/fonts/shadows.ttf') format("Shadows Into Light"); font-size: 4 ...

implement a dynamic filtering system for products on a React webpage

My header component contains a filter list with categories like men, women, kids, etc. <span className="category" onClick={()=>{filter('men')}}>Men </span> <span className="category" onClick={ ...

After resolving a promise, what is the process for loading a Next.js App?

Seeking guidance on implementing the code snippet below using Next.js. I suspect there is an issue with Next.js not being able to access the window object without being within a useEffect(() => {}) hook. When switching back to regular React, the code ...

Step-by-step guide on using variables to apply CSS

In our table consisting of 6 rows and 2 columns, the second column displays a Font Awesome circle followed by various values in the database field (such as Outstanding, Very Good, Good, Needs Improvement, and Adequate). I am looking to dynamically change ...

Simple steps to correct the npm installation of the React list filter

I encountered an issue while trying to install react-list-filter using npm (npm install react-list-filter). The error messages displayed in my console are as follows: npm ERR! code ETARGET npm ERR! notarget No matching version found for <a href="/cdn-c ...

Sharing a variable across all modules in a Next.js application on a global scale

Is there a way to create a variable accessible to all modules in a next.js application without the need to import it every time? This feature would be beneficial for commonly used utilities within my application. ...

Understanding the dependency array in useEffectA closer look at the dependency

I'm seeking a deeper understanding of the React (functional) component lifecycle and finding myself puzzled by useEffect() when given a dependency array as the second argument. While I have reviewed the documentation and grasped the fundamentals of us ...