Using Images In React
The displaying pictures within a React web application. This involves tasks like importing image files, showing them on your app's pages or components.
There are several ways to do it. Here's a step-by-step guide on how to use images in a React application:
Prepare Your Images:
- First, make sure you have the images you want to use in your React project. Place these images in a folder within your project directory, typically named
publicorsrc.
- First, make sure you have the images you want to use in your React project. Place these images in a folder within your project directory, typically named
Import the Image:
- To use an image in your React component, you need to import it. You can use the
importstatement at the top of your component file to do this.
For example:
import ImgPizza from './pizza.png'; // Replace './pizza.png' with the actual path to your imageHere,
ImgPizzais a variable that now holds the image.- To use an image in your React component, you need to import it. You can use the
Render the Image:
- To display the image in your component, use the
<img>HTML element with thesrcattribute set to the imported image variable.
For example:
<img src={ImgPizza} alt="Pizza" />- The
altattribute is used to provide alternative text for the image.
- To display the image in your component, use the
CSS Styling (Optional):
- You can use CSS styles to control the appearance and layout of your image. You can apply styles inline using the
styleattribute or by adding CSS classes and defining styles in your CSS file.
<img src={ImgPizza} alt="Pizza" style={{ width: '200px', height: 'auto' }} />Or, in your CSS file:
.pizza-image {
width: 200px;
height: auto;
}In your component:
<img src={ImgPizza} alt="Pizza" className="pizza-image" />- You can use CSS styles to control the appearance and layout of your image. You can apply styles inline using the
Example:
import ImgPizza from './pizza.png'
export default function GoodComponent() {
return(
<img src={ImgPizza} alt="React Logo" />
)
}
Output :

Example Explanation :
In Line 1, import ImgPizza from './pizza.png': In this line, we are importing an image of a pizza. We're giving it the name ImgPizza so that we can use it in our component.
Line 2, This line export default function GoodComponent() {}: Here, we're defining a component named GoodComponent. This component is meant to display an image.
Line 4 and 12, Inside the return statement, we have <img src={ImgPizza} alt="React Logo" />. This part is responsible for rendering the image on the web page.
<img>: This is an HTML element used for displaying images on a webpage.src={ImgPizza}: Here, we are specifying thesource (src)of the image. We're using theImgPizzavariable that we imported earlier to tell the browser where to find the pizza image.alt="React Logo": This is used to provide an alternative text for the image.
So, this code imports an image of a pizza and creates a React component called GoodComponent that displays the pizza image on a webpage when used.