Approach 1 Using Inline Styles

  • We have applied the styles directly as inline styles to the <div> element containing the Image component.
  • The border, borderRadius, boxShadow, and margin properties have been properly applied using inline CSS.
  • You can change the styles in the code, the changes will be reflected in the image too.

Syntax:

import Image from 'next/image';
const HomePage = () => (
<div>
{/* Header */}
{/* Section Title */}
{/* Container with inline styles */}
{/* Image component */}
</div>
);
export default HomePage;

Example: Now in your index.js file add the below content:

Javascript




//index.js
import Image from 'next/image';
 
const HomePage = () => {
    return (
        <div style=
            {
                {
                    display: 'flex',
                    flexDirection: 'column',
                    alignItems: 'center',
                    justifyContent: 'center',
                    minHeight: '100vh'
                }
            }>
            <h1 style={{ color: 'green' }}>
                w3wiki
            </h1>
            <h3>
                Next Image not taking class
                properties [Approach 1: Using Inline Styles]
            </h3>
 
            <div style=
                {
                    {
                        border: '2px solid red',
                        borderRadius: '4px',
                        boxShadow: '0 2px 4px rgba(0, 0, 0, 0.1)',
                        margin: '20px'
                    }
                }>
                {/* next/image with specified width, height, and styles */}
                <Image
                    src=
"https://media.w3wiki.org/wp-content/uploads/20230816191453/gfglogo.png"
                    alt="GFG Image"
                    width={300}
                    height={300}
                />
            </div>
        </div>
    );
};
 
export default HomePage;


Now, paste the below config code in the file “next.config.js“. We are adding the configuration for external images.

// next.config.js
module.exports = {
images: {
domains: ['media.w3wiki.org'], // Add the domain of your image source
},
};

Step to run the application: Now run the application with the below command:

npm run dev

Output:

How to resolve ‘Next Image not taking class properties’ issue?

In this article, we will solve the problem of “Next Image not taking class properties” occurring due to the way the ‘next/image‘ component handles its underlying HTML layout and overall structure. The ‘next/image‘ component is the component that generates the additional wrapper elements and inline styles to perform the image optimization.

Table of Content

  • Using Inline Styles
  • Using Custom Image Wrapper

Similar Reads

Steps to Create the Next App:

Step 1: Set up React Project using the Command:...

Approach 1: Using Inline Styles:

We have applied the styles directly as inline styles to the

Approach 2: Using Custom Image Wrapper:

...