2

I tried with the following code after importing the image to the src folder. But I can't see the image getting displayed. How to solve this issue?

enter image description here

My React code is this.

import React from 'react'
import burgerLogo from '../../assets/images/burger-logo.png'
import classes from './Logo.module.css'

const logo=(props)=>(
    <div className={classes.Logo} style={{height:props.height}}>
        <image src={burgerLogo} alt="MyBurger"/>
    </div>

);

export default logo;

My CSS code is this.

.Logo{
    background-color: white;
    padding: 8px;
    height: 100%;
    box-sizing: border-box;
    border-radius: 5px;
}

.Logo img{
    height: 100%;
}
2
  • Try instead of using a variable using the path in the image source. I don't think that you need to import the image like that. Commented Oct 18, 2020 at 2:37
  • There's no need to import the image like you did Commented Oct 18, 2020 at 2:38

3 Answers 3

3

Can you change the tag image to img

    <image src={burgerLogo} alt="MyBurger"/>

to

    <img src={burgerLogo} alt="MyBurger"/>

if the import import burgerLogo from '../../assets/images/burger-logo.png' is not correct, you will get an error, so that should not be a problem in your case.

Just want to point that user defined components should start with a capital letter.

When an element type starts with a lowercase letter, it refers to a built-in component like or and results in a string 'div' or 'span' passed to React.createElement. Types that start with a capital letter like compile to React.createElement(Foo) and correspond to a component defined or imported in your JavaScript file.

We recommend naming components with a capital letter. If you do have a component that starts with a lowercase letter, assign it to a capitalized variable before using it in JSX.

from the ReactJS docs

Coming back to your question

How do I display an image with CSS in ReactJS?

You can do that using CSS background-image property.

Put your image burger-logo.png in public folder (you can change location if needed eg: inside img or assets folder).

In you CSS use like this

.Logo {
    background-color: white;
    background-image: url('./burger-logo.png');
    padding: 8px;
    height: 100%;
    box-sizing: border-box;
    border-radius: 5px;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, @kiranvj have you verified with this css approach you are also able to CTRL+P. In my case. I have a logo on React Dialog, when user wants to make print. The logo doesn't show in print preview niether print. Any idea ?
@khmub try adding -webkit-print-color-adjust: exact; in the css class. Check MDN for more details
1

Depending on your React setup, the src for image can vary.

Can you try changing the location of your image file like so:

import burgerLogo from './burger-logo.png'

If Webpack is setup to look into a certain folder for .png files, it will automatically pick it up from that folder and attach the correct file location behind the scenes.

PS. Do you see any errors in the console?

1 Comment

No console erros. It worked by replacing <image> tag with <img> tag. Thank You
1

Assuming your assets folder is inside your public folder do it like this : <img src='./assets/images/burger-logo.png' alt="MyBurger"/> If it doesn't work, try this <img src='./images/burger-logo.png' alt="MyBurger"/> It really depends on the path of the image.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.