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

However, the current output seems to differ from what I intended

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

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

React page stuck in an infinite loop due to a timeout alert notification

Currently, I am developing an app that is designed to display a message indicating whether user creation was successful or not. The framework I am using is deno's fresh, which utilizes "preact". Preact can be considered as a simplified version of reac ...

Troubleshooting the onExited callback issue with Popover component in Material UI

<Popover key={element.name} classes={{ paper: classes.paper }} open={open} anchorEl={this.myRef.current} anchorOrigin={{ vertical: ' ...

Customizing MUI DataGrid: Implementing unique event listeners like `rowDragStart` or `rowDragOver`

Looking to enhance MUI DataGrid's functionality by adding custom event listeners like rowDragStart or rowDragOver? Unfortunately, DataGrid doesn't have predefined props for these specific events. To learn more, check out the official documentati ...

What is the best way to implement a modal that can toggle dark mode using the Konami code, with the added functionality of a close button?

Recently, I attempted to create a Modal window that would activate when the Konami code (↑↑↓↓←→←→BA) is typed. As someone new to JavaScript, I'm still learning and open to feedback. While I have the coding part figured out, I need assi ...

The anchor tag seems to be malfunctioning within the div container

<style type="text/css> .xyz { position: absolute; top: 120px; left: 160px; z-index: -1; } #container { position: relative; overflow: visible; opacity: .3; } </style> <body> <div> <video i ...

Encountering a Component Exception error in React Native while attempting to implement a Victory chart component

Currently, I am exploring the Victory library for react native to create a line chart for my budgeting application. Below is the code snippet from my Graph component: import React from 'react'; import { View, StyleSheet } from 'react-native& ...

Error in React JS: Trying to iterate over an undefined value (unable to access property Symbol(Symbol.iterator))

When attempting to run the code on localhost, I encounter an error that prevents me from proceeding with coding tasks. import React, { useEffect, useState } from 'react' import styled from 'styled-components' import {BsThreeDotsVert ...

Error: The variable prevDeps is not defined in withStyles

Currently delving into the world of ReactJS and making use of material-ui. I'm uncertain about my grasp on withStyles. The error "TypeError: prevDeps is undefined" arises when I include setState in the app component. If I omit setState, everything ...

Using the mpdf addPage function generates new empty pages

Hey there, I'm facing an issue where using $mpdf->addPage() within a for loop results in creating blank pages instead of adding content. My goal is to generate a new page when a certain condition is met and then populate it with the required conten ...

From integrating Vue 2 TSX to React TSX: ensuring seamless cooperation

Currently, my app is built with Vue 2 using vue-tsx-support, vue-class-component, and vue-property-decorator. All of my Vue 2 components are already TSX classes. I am interested in gradually transitioning to React. I experimented with https://github.com/ ...

identify external components based on their internal identifiers

Can we target an external element based on its descendant elements? This question arises from the need to apply one stylesheet to two different dynamic pages within the same wrapper. Page 1 <div id="api"> <div class="a"></div> </div ...

ReactJS: Alert for controlled form element

Utilizing inputs from Material-Ui, I am retrieving values from an API to populate these inputs. If the API does not return any values, the user must enter them manually. In my implementation, I am utilizing Hooks with the initial state defined as: const [ ...

Changing the chart background color in amCharts: A step-by-step guide

import React from 'react'; import * as am4core from "@amcharts/amcharts4/core"; import * as am4charts from "@amcharts/amcharts4/charts"; import am4themes_animated from "@amcharts/amcharts4/themes/animated"; am4core.useTheme(am4th ...

Troubleshooting jQuery Dropdown Menu Animation Bugs

Take a look at this interesting fiddle: https://jsfiddle.net/willbeeler/tfm8ohmw/ HTML: <a href="#" class="roll-btn">Press me! Roll me down and up again!</a> <ul class="roll-btns"> <li><a href="#" class="control animated noshow ...

Tips for efficiently dealing with "null" elements while mapping an array in React

It looks like the posts array is coming through as expected, as shown below: posts: [ ] 0: { } 1: { } 2: null 3: null 4: { } I'm a bit puzzled by the presence of null values. I am integrating NextJS with headless WordPress. Does anyone have any insig ...

What is preventing my CSS Animation from activating on my website?

Is there something in the code preventing the animation from working properly? .projectLinks a{ background-color: var(--secondary-color); color: var(--main-color); padding: 2px 4px 4px 4px; border-radius: 15px; margin-right: 25px; text-decorati ...

Is there a counterpart to message.error() [antd] in Material UI?

Just diving into the world of Material UI for web development. As a user of antd, I'm looking for a way to display error messages similar to message.error(antd popup) without having to embed it in the render section and call it as a simple function. H ...

How can I pass a specific value, rather than the entire array, in a ReactJS dropdown menu?

I am facing a problem where the entire array is being passed as an argument when I call onchange after getting correct values in the dropdown. Instead of only receiving the selected value, e contains the whole array. Here is the code snippet that demonst ...

Removing the Tooltip for Sorting in React-Table: Here's How to Remove the 'Toggle sortBy' Tooltip from Column Headers

Is there a way to remove the 'Toggle sortBy' tooltip from the column header when using the Material-UI Table components in the most recent version (7.5.x) of React-Table? Two tooltips Upon hover, I have a tooltip displaying the column header na ...

Creating a multi-tiered cascading menu in a web form: Harnessing the power of

In my form, there is a field called 'Protein Change' that includes a multi-level dropdown. Currently, when a user selects an option from the dropdown (for example, CNV->Deletion), the selection should be shown in the field. However, this function ...