Last updated: Apr 6, 2024
Reading timeยท3 min
To display an image from a URL, use the img
tag and set its src
prop to
the complete URL of the image.
Optionally set the alt
prop to a short description of the image.
import React from 'react'; export default function App() { return ( <div> <img src="https://bobbyhadz.com/images/blog/react-prevent-multiple-button-clicks/thumbnail.webp" alt="car" /> </div> ); }
The example shows how to display an image from an external URL.
img
tag and set its src
prop to the complete URL that points to the image.Notice that the image that is self-closing - <img />
.
The alt
prop helps screen readers understand what the image is about.
If you need to display an image from a local path in React:
src
directory.import MyImage from './thumbnail.webp'
.src
prop of the image element to the imported image.import React from 'react'; import MyImage from './thumbnail.webp'; export default function App() { return ( <div> {/* ๐๏ธ local image */} <img src={MyImage} alt="horse" /> {/* ๐๏ธ external image */} <img src="https://bobbyhadz.com/images/blog/react-prevent-multiple-button-clicks/thumbnail.webp" alt="car" /> </div> ); }
The example assumes that you have an image named thumbnail.webp
in the same
folder as the App
component.
For example, if you are importing an image from one directory up, you would
import as import MyImage from '../thumbnail.webp'
.
The image has to be located in the src
directory of your project.
src
prop on the img
tag in order to render the image.With images from an external URL, you set the src
prop of the image to the
complete URL.
With local images, you set the src
proper of the img
tag to the imported
image.
Depending on your setup, you might also be able to use the require()
syntax to
display local images.
import React from 'react'; export default function App() { return ( <div> {/* ๐๏ธ local image */} <img src={require('./thumbnail.webp')} alt="horse" /> </div> ); }
The code sample assumes that there is a thumbnail.webp
file located in the
same directory as your App.js
module.
You can also check out my detailed guide on how to import an image in React.
You can also render images that are located in the public
directory of your
React project.
When your images are located in the public
directory, use an absolute path.
The following example assumes that there is a thumbnail.webp
image located
under public/images/thumbnail.webp
.
import React from 'react'; export default function App() { return ( <div> <img src="/images/thumbnail.webp" alt="horse" /> </div> ); }
Notice that we specified an absolute path to the image - one that starts with a forward slash.
public/ images/ thumbnail.webp
The public
folder is omitted and the path starts at the images
directory.
Make sure to specify the extension of the image in the value of the src
attribute.
I've also written a detailed guide on where you should store images in a React app.
You can learn more about the related topics by checking out the following tutorials: