React JS - Large image failing to display entirely

I'm currently facing challenges with displaying my image in full size. I am unsure what is causing this issue and would appreciate any guidance on correcting my approach.

The specific image I am attempting to display at full-size can be found https://i.sstatic.net/lQaNy.png

However, the current output seems to differ from what I intendedhttps://i.sstatic.net/9i9Wl.png

Despite setting the height to 100%, the image still does not appear as expected :-("

.App {
  text-align: center;
  height:100%;
  width:100%; 
}

body, html {
    height:100%;
    width: 100%;
}

.bg { 
    /* The image used */
    background-image: url("./../../images/City.png");

    /* Full height */
     height: 100%;

    /* Center and scale the image nicely */
    background-position: center;
    background-repeat: no-repeat;
    background-size: cover;
}

This is how my App.js looks like:

import React, { Component } from 'react';
import './App.css';

class App extends Component {
   render() {
      return <div className="App">
         <div className="bg">
            hello world
         </div>
     </div>;
   }
}

Thank you for your assistance!

Update: Here is the result after applying one of the solutions provided in the responses

https://i.sstatic.net/qxugY.png

Answer №1

Make sure to include the following CSS in your bg div:

.bg{
min-height: 600px; /*Adjust the height as needed*/
}

This is important because the bg div will only take the height of its content if min-height is not defined.

Alternatively, you can place the image in a regular image element and position the bg div absolutely over it.

Here's an example using inline CSS for clarification:

<div class="App" style="position:relative;width:100%;">
    <img src="the path" style="width:100%; height:auto;" />
    <div class="bg" style="position:absolute;top:0;left:0;right:0;bottom:0;"
         hello world!
    </div>
</div>

Answer №2

The appearance of your .App will vary based on its CSS, as well as the wrapping HTML structure containing the div displaying your city background image.

To update the CSS for .App, you can try:

.App {
  display:flex; 
  height:100%;
  width:100%; 
}

Make sure the html and body elements in your HTML/DOM cover the entire browser view area:

html, body {
  display:flex;
  height:100%;
  width:100%;
}

For a demonstration, check out this jsFiddle link. I hope this helps!

Note that while referencing this jsFiddle, remember to use the className attribute instead of class for DOM elements with JSX/React. For example:

<div class="App">
  <div class="bg">
    hello world
  </div>
</div>

should be changed to:

<div className="App">
  <div className="bg">
    hello world
  </div>
</div>

in your component's render() method.

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

Is it possible to transition an existing T3 Stack (TypeScript, Tailwind, trpc) application from a 'page router' setup to an 'app router' setup?

Lately, I've been immersed in a project that was originally set up using the t3 stack with Next.js 'page router'. However, I have decided that I want to switch to using the 'app router' in this project. The current version of Next. ...

React: When an array state is controlling my components, why aren't they re-rendering?

I am facing an issue with my app where the className of buttons is not updating correctly when clicked. It seems that only active buttons trigger a re-render, while non-active ones do not. This behavior is confusing to me. Here's the code snippet for ...

Having trouble loading external CSS in HTML?

I have been attempting to load an external CSS file into my current HTML file without success. I have provided images showcasing the code: View the code here See the output here Currently, I am working on Windows and using Aptana Studio 3 as my IDE. ...

Creating a Bootstrap 4 DIV element with horizontal scrolling functionality

I've been struggling to implement a horizontal scroll for a DIV on my site, but none of the common solutions like overflow-x or white-space:nowrap are working for me. This is the section of my website where I need a horizontal scroll bar If you want ...

Display loading spinner within Material-UI CardMedia

I am facing an issue with displaying a loader in CardMedia when the image is not loaded. Strangely, when I place the loader in CardText instead, it works fine and displays the loader as expected. Can someone help me identify what's wrong with my code ...

Encountered an error message stating 'Unexpected Token <' while attempting to launch the node server

After adapting react-server-example (https://github.com/mhart/react-server-example), I encountered an issue with using JSX in my project. Despite making various changes like switching from Browserify to Webpack and installing babel-preset-react, I am still ...

Sending an array of files to an express backend: A step-by-step guide

I am having an issue sending an array of images to my backend for upload using multer-s3. The process involves sending the images from a React frontend to a Node Express backend. Once submitted from the frontend, the images are formatted as follows: image ...

The event.pageY position consistently reflects the laptop screen size rather than the exact position where the click occurred

My webpage is scrollable, but the event.pageY position always corresponds to my screen size. Even when scrolling down and clicking near the top of the screen, it registers as 50px. I am currently utilizing event.pageY While this functions correctly on a ...

Add transparency to a component using CSS

I am currently facing an issue with a div element called signup-box. I have applied an opacity value of 0.65 to it, but unfortunately, this is affecting everything within the div as well. This means that my white text is no longer visible as white and th ...

What are the steps to create a unique popup div effect with jQuery?

Upon clicking the icon on this page, a mysterious div appears with information. I'm completely baffled by how they achieved this cool effect using css/jQuery tools. Can anyone shed some light on the mechanism behind this? ...

Guide on displaying an array object in MongoDB using React

I'm having trouble figuring out how to display certain data from my MongoDB schema in a React component. Here is my MongoDB schema: const postSchema = new mongoose.Schema({ userID: { type: String }, dateTime: { type: Date, default: Date.now } ...

Modifying CSS to ensure compatibility with various browsers

I am curious if it is possible to modify some CSS when viewed in a different browser. For instance, I have this CSS for a flexbox div: .wdFlex { display: -webkit-box; display: -moz-box; display: -ms-flexbox; display: -webkit-flex; display: flex; -webkit- ...

Avoid displaying the index page in Next.js until after redirecting to the Keycloak login page

Recently, I have been working on a Next.js project that involves authenticating with Keycloak. To implement frontend authentication, I utilized the '@react-keycloak/ssr' library. If you want to take a look at my _app.js code, you can find it he ...

Pass Target Value Along with onClick Event in React

Is there a way to have one Display function that can control two Popper elements from Material UI independently? I attempted to assign different targets to each Popper element, but couldn't make it work using event.target.target. How can I achieve thi ...

The oversized image is refusing to scale down to fit within the confines of the CSS grid container

I am currently facing a challenge with creating a responsive image within a grid layout using CSS. The image is not resizing properly when the browser window changes size and is extending beyond its container borders. I have experimented with various techn ...

Nested CSS selector within a parent element

How can I change the color of both elements inside a custom button when the button is active? The first color is defined by the class of the button, and the second color is defined by the class of the span inside that button. CSS: .buttonClass{ color:b ...

Triangle-shaped image mapping and interactive hover effects

I am attempting to create a simple hover effect using two triangles. One triangle should start below the other, and upon hovering, it should appear above the first triangle. The problem I am encountering is that the surrounding boxes are obstructing the e ...

Material Design Elements: Choosing Between Paper and Card Titles

When creating a React application, I aim to have the title of a Card or Paper positioned on the top border with some indentation from the left side. Here is an example: I have extensively searched for a universal method to achieve this layout but have not ...

The deployed app on Heroku is showing a 304 status code with no JSON response

I've been working on a project using React, Express, and MongoDB. While everything works fine locally, I encountered an issue with GET requests after deploying the project on heroku.com. Instead of receiving the expected JSON response, I'm gett ...

What is the best way to align a TabPanel component at the center using React Material UI

As I attempt to compile a list of articles while switching to a list of other entities in React + material UI, I have encountered some difficulties. Specifically, I am struggling to center the Card displaying an article in alignment with the centered Tabs. ...