I am trying to set a background image specifically for one page in my application, but I am struggling to make it work. As a newcomer, I am still learning how to

I've been working on the code for a login page, but I'm having trouble setting a background image for the page. I know how to set it for the whole application by modifying the app component, but when I try to do the same for this specific component, only the component itself gets the background image and not the entire page. I have imported the image for this purpose.

import React, { useState } from "react";
import "./Loginp.css";
import axios from "axios";

import bgimg from "../../assets/Loginimage.jpg";

function Loginp() {
  const [usernamed, setusername] = useState("");
  const [passwordd, setpassword] = useState("");

  let check = false;

  function clicki() {
    console.log("clicked");
    const dat = {
      username: usernamed,
      password: passwordd,
    };
    axios.post("http://localhost:7000/login", dat).then((resp) => {
      check = resp.data;
    });
    if (check) {
      alert("User name already exists");
    }
  }
  return (
    <div
      id="loginform"
      style={{
        backgroundImage: bgimg,
        height: 100,
        marginTop: 10,
        textAlign: "center",
      }}
    >
      <FormHeader title="Login" />
      {/* <Form /> */}
      <div>
        <div class="row">
          <label>Username</label>
          <input
            type="text"
            placeholder="Enter your username"
            onChange={(e) => {
              setusername(e.target.value);
            }}
          />
        </div>

        <div class="row">
          <label>Password</label>
          <input
            type="text"
            placeholder="Enter your password"
            onChange={(e) => {
              setpassword(e.target.value);
            }}
          />
        </div>
        <div id="button" class="row">
          <button onClick={clicki}>Log in</button>
        </div>
      </div>
    </div>
  );
}

const FormHeader = (props) => <h2 id="headerTitle">{props.title}</h2>;

export default Loginp;

Answer №1

import React from 'react';
import backgroundImage from './path-to-your-image.jpg';

function CustomComponent() {
  const customStyle = {
    backgroundImage: `url(${backgroundImage})`,
    backgroundRepeat: 'no-repeat',
    backgroundSize: 'cover',
  };

  return (
    <div style={customStyle}>
      {/* Add your content here */}
    </div>
  );
}

export default CustomComponent;

Answer №2

Give this a shot.

backgroundImage: url(${newBgImg})

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

Troubleshooting the Hover Effect of Buttons in Next.js when Using Tailwind CSS for Dynamic Color Changes

Encountering a problem with button hover functionality in a Next.js component using Tailwind CSS. The objective is to alter the button's background color dynamically on hover based on a color value stored in the component's state. This code func ...

Framer Motion causing a mix-up with my belongings

During the initial "reordering" process, framer-motion seems to incorrectly calculate the position of my items. This issue appears to be related to the fact that I am implementing this feature within a side panel, causing the items to be positioned at a di ...

NodeJS is having trouble being accessed through IIS with a reverse proxy, resulting in a "Cannot GET" error. However

My node app is currently running on Port 3000 with an IIS Reverse Proxy setup to serve the app through an IIS site, following the instructions outlined here: The IIS site is configured to use Port 80 and has the following Web.config: <?xml version=&quo ...

Redux Toolkit's Async Thunk library

Hello everyone, I am completely new to React and currently in the process of learning reduxjs/toolkit for managing application state. I recently developed a simple application that deals with posts. However, I am facing an issue where my async thunk fetchP ...

Is it possible for Express in Node.js to handle multiple static folders at once?

Currently, I am working on a project that involves a user uploaded collection of styles, scripts, and images as well as my app's own collection of these resources. They are stored in separate directories on my server. I am wondering if there is a way ...

NodeJS and Snowflake - Specifying connection options is essential

I am currently using NodeJs to input data into Snowflake, but I am encountering the following error: Error [MissingParameterError]: Connection options must be specified. Below is a simplified version of my code: import * as snowflake from 'snowf ...

Encountering error ORA-12514 when utilizing the node oracle-db npm package

At the moment, I am immersed in a project that relies on utilizing oracle for its backend. Following the instructions provided in this link, I successfully installed node-oracledb on my Mac using npm. The contents of my file are as follows: var oracledb = ...

Remove the 'name' attribute from the input tag within the URL

Is there a way to make the server update the URL format to localhost:3000/userinput instead of localhost:3000/?name=userinput when submitting a name? Any suggestions would be greatly appreciated. Thank you in advance. ExpressJs FILE <!DOCTYPE html> ...

Tips for Saving process.argv in a .js File for Future Reference

I am struggling with passing process.argv value to a config file for later use. I am trying to call it from npm scripts. I have the following config file: module.exports = { foo: proccess.argv[2] } So far, I have tried calling it via node config.js &apo ...

Navigating between child nodes in an HTML drop down list with multiple options and hierarchical structure

Hey there! I am looking to create a unique HTML hierarchy drop-down menu. It will have multiple levels or options, with each option having the potential for child nodes. When an option has child nodes, an arrow will appear next to it. Clicking on this ar ...

Validating HTML forms using Javascript without displaying innerHTML feedback

Hey, I'm just diving into web development and I'm struggling to figure out why my innerHTML content isn't displaying on the page. Can anyone offer some assistance? I'm trying to display error messages if certain fields are left empty, b ...

Finding the path to the npm repository in scripts once it has been installed globally

In the scope of my project, I have set up a repository on github housing various tools related to the project. This repository includes a package.json file and some binaries linked with bin directives. Despite being a private repo and not yet released on a ...

Achieving emulation of Cordova/Phonegap on a Genymotion virtual device

After setting up an app using the Cordova CLI, I went through the following steps: cordova create hi com.example.hi Hi cd hi cordova platform add android cordova build android Next, I launched Genymotion and began a device called "api9". When running th ...

Dividing a JSON array by a specific key using lodash underscore

I'm on a quest to extract the distinct items in every column of a JSON array. Here is what I aim to convert : var items = [ {"name": "Type 1","id": 13}, {"name": "Type 2","id": 14}, {"name": "Type 3","id": 14}, {"name": "Type 3","id": 13}, {"na ...

Organizing grid elements within the popper component

I am struggling to align the labels of options in the AutoComplete component with their respective column headers in the popper component: https://i.stack.imgur.com/0VMLe.png const CustomPopper = function (props: PopperProps) { co ...

What is the best way to use createElement in JavaScript to insert <p> and <span> elements within a <div>?

I am currently experimenting with generating sentences accompanied by draggable text boxes. To achieve this, I intend to construct the following HTML structure using JavaScript exclusively: <div> <p>Data1<span class = "smallBox droppabl ...

Retrieving information from an object using JavaScript

Hello, I am facing a challenge with extracting data from an object via a web API in ReactJS. It seems like the data returned by the API is not structured properly as a JavaScript object. To view the issue directly in your browser visit: I need assistance ...

React is unable to recognize the 'delete key' in my KeyDown event detection

I've been struggling for days to capture a 'delete' key press event in React. No matter what I try, my function always seems to return false. What could be the reason for this? <textarea className="DetailsPage-t ...

Customizing pressed row styles and properties within a map iteration in React Native

How can I modify the style of a single item in a list when a button is pressed in React-Native? I want only the pressed item to change, not the entire row. Here is my attempt at implementing this: //hooks const [activeStyle, setActiveStyle] = useState( ...

Is there a method to establish varied usage permissions for a single page without any tracking?

I am puzzled by how a solution could create something like this. My goal is to have a webpage displaying 2 squares on a large screen. There will be 2 users, each needing access to write in their own square only on this page. <div class="square1"> ...