A step-by-step guide on showcasing three post images in separate divs at the bottom of a webpage with Reactjs and Css

In the array below, there are three post photos. When I click on each post button, I should see a corresponding post photo div at the bottom for each post.

Issue:

I am facing a problem where only one post photo div is being displayed, which keeps replacing the others after I added the following CSS code.

const mainArea={
 position: 'fixed',
  width: '80%',
  bottom: '0%',
   display: 'inline-block'
}

const photodiv={
position: 'relative',
width: '250px',
  // height:auto,
  background: 'orange',
  color: 'black',
  borderRadius: '5px 5px 0px 0px',
  bottom: '0px',

}

Screenshot demonstrating the jammed div due to CSS implementation:

Desired Outcome: I want to see three div post photos when the three toggle buttons are clicked.

Main Code:

import React, { Component, Fragment } from "react";
import { render } from "react-dom";

const mainArea={
 position: 'fixed',
  width: '80%',
  bottom: '0%',
   display: 'inline-block'
}

const photodiv={
position: 'relative',
width: '250px',
  // height:auto,
  background: 'orange',
  color: 'black',
  borderRadius: '5px 5px 0px 0px',
  bottom: '0px',

}

class Focus extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      data: [],
      shown: true,
    };
  }

  componentDidMount() {
    this.setState({
      data: [
        { id: "1", title: "my first title", image: "http://localhost/apidb_react/1.png", visible: true , photoVisible: true},
        { id: "2", title: "my second title", image: "http://localhost/apidb_react/2.png", visible: true, photoVisible: true},
        { id: "3", title: "my third title", image: "http://localhost/apidb_react/3.png", visible: true, photoVisible: true}
      ]
    });
  }


  toggle(id) {
    const newData = this.state.data.map(item => {
      if(item.id === id) {
        return { ...item, visible: !item.visible};
      }

      return item;
    })

    this.setState({
      data: newData
    });
  }




/*

  hideUnhidePhoto(id) {

    const newData = this.state.data.map(item => {

alert(id);

      if(item.id === id) {
alert('ttto  ' +item.id);
        return { ...item, photoVisible: !item.photoVisible};
      }

      return item;
    })

    this.setState({
      data: newData
    });
  }
*/



hideUnhidePhoto(id) {

    this.setState(({ data }) => {
        return { 
            data : data.map(item => ({ 
            ...item, 
            photoVisible : (id == item.id) ? !item.photoVisible : item.photoVisible }))
        }
    });
}



  render() {
    return (
      <div>
        <label>
          <ul>
            {this.state.data.map((post, i) => (
              <li key={i}>

<div style={mainArea}>
<div style={photodiv}>
                <div style={{ display: post.visible ? "none" : "block"}}> 

<b>Post Data:</b> {post.title} --{post.id}   <br />

<span style={{color: 'red'}} onClick={ () => this.hideUnhidePhoto(post.id) }> Hide/Unhide Photo</span>

<div style={{ display: post.photoVisible ? "block" : "none"}}> 

<img src={post.image} />

</div>

</div></div>
</div>
 <button onMouseDown={ () => this.toggle(post.id) }>Toggle </button><br />


                <br />
              </li>
            ))}
          </ul>
        </label>
      </div>
    );
  }







}

Answer №1

If I have a clear understanding of the issue at hand, it seems that making adjustments to your style object for mainArea can resolve the issue:

const mainArea={
  /* position: 'fixed', Remove position fixed */
  width: '80%',
  bottom: '0%',
  display: 'inline-block'
}

The fixed position places elements on the screen relative to the client area of the window. When multiple elements share the same (default) coordinates and are set with the fixed rule, they may overlap each other, giving the impression of only one element being visible.

To see a functional example, check out this jsFiddle: enter link description here

I hope this information proves helpful!

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 displaying a wrong timeThe time in React not showing correctly

I am trying to ensure that the time displayed is based on the values I have set in the useState. Specifically, it should show as 9:00 AM. However, instead of displaying this, I am seeing Invalid Time Format. If you could take a look at my code on codesand ...

Basic title in material-ui navigation bar

I was hoping to display a simple label on the right side of the AppBar. Currently, I am using the iconElementRight prop and inserting a disabled flat button: <AppBar iconElementRight={<FlatButton label="My Label" disabled={true}/>} /> Thi ...

Anchor Element in a Popper Component in a Functional Setup

Is there a way to utilize AnchorEl for a popover within a functional component where the instance variable this is not accessible? In components that extend React.Component, one could employ buttonRef={node => { this.anchorEl = node;}} on the button a ...

Combining classes within LessCSS for nested functions

I'm struggling to get LessCSS to process a file with a series of nested rules using the "&" concatenation selectors. For instance, the code below works fine: .grey-table { background: #EDEDED; tr { th { background: #D ...

Is there a method to globally pass retrieved server-side data to client components in a Next.js version greater than 13 app directory?

My goal is to retrieve translation JSON files from an API for my Next.js application, which is structured using the /app directory. The server-side implementation works smoothly as I can utilize the await keyword to make the API call and share the data glo ...

Place a gap at a specific spot within the boundary line

Here is a CSS code snippet that displays a horizontal line: .horizontalLineBottom { border-bottom:solid #6E6A6B; border-width:1px; } Is it possible to add space at a specific position on this line? For example, _________________________________ ...

Can MobX storage be initialized with data during the initial loading phase?

I am in the process of creating a straightforward Next.js website that contains numerous components nested deep within each other, all utilizing the same set of data such as article names, project names, and FAQ card details. As per the documentation, it ...

the mobile website is not properly aligned on a horizontal axis

As I work on creating a mobile version of my website, I've come across a challenge: The entire site fits perfectly on the computer at a browser width of 480px, but when viewed on my mobile phone (regardless of the browser used), it leaves space on the ...

Upon reacting with Typescript, the window will transition to the homePage, however, it will also reset

Trying to redirect this component to the HomePage</code causes the data to restart once it reaches the home page.</p> <p>Any recommendations for an alternative to <code>window.location.href = "/HomePage"? import React, { useE ...

Adjusting and arranging multiple thumbnail images within a container of specific width and height

I need a user-friendly method to achieve the following. The thumbnails will not be cropped, but their container will maintain a consistent height and width. The goal is for larger images to scale down responsively within the container, while smaller image ...

Attach a child element to the bottom of its parent div when the parent div reaches the bottom

I'm looking to make a child div stick to the bottom when the parent div reaches the bottom of the browser window. Note: This behavior should only occur when the parent div is pushed down, not when the page is scrolled down. For instance, in my demon ...

Determining the precise spacing needed for a personalized cursor

Currently, I'm facing an obstacle in my attempt to design a custom cursor/crosshair inside a canvas. The problem lies in the Length, Width, and Gap dimensions assigned to the four rectangles that make up the cursor, as it is resulting in an incorrect ...

Securing Your Next.js Web App with Session Authentication

I have encountered a challenge while integrating NextAuth authentication into my React.js web application. To ensure seamless user authentication across the entire app, I am attempting to wrap a SessionProvider around the root component. However, VSCode ...

Ensure that the flex item expands to occupy the vacant space left when other child elements are deleted

I am facing an issue with multiple children inside a parent container that has the CSS properties display: flex and flex-direction: column. There is a toggle button on the webpage which removes one of the children when clicked. The problem I need to solv ...

A user-friendly JavaScript framework focused on manipulating the DOM using a module approach and aiming to mirror the ease of use found in jQuery

This is simply a training exercise. I have created a script for solving this using the resource (function(window) { function smp(selector) { return new smpObj(selector); } function smpObj(selector) { this.length = 0; i ...

Switch between different content sections using a button in a Bootstrap tab-pane

Here is the code snippet I am currently working with: <!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap Example</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-s ...

Customize React Material UI styling with withStyles

I'm currently involved in a project where I am using React Material UI and need to change the border bottom color of the Select component when it is focused. Here's the code snippet that I have implemented so far. import { Select as MuiSelect, w ...

Is it possible to change the maximum column count in material-ui by transposing the grid?

Currently, I am working on setting up a grid layout of cards : https://codesandbox.io/s/u93zn In our scenario, we are facing an issue where the number of columns will always exceed the number of rows (and it surpasses the maximum limit that the grid can ...

Concealing other items in an array on selecting one item in React Native- A step-by-step guide

Currently, I have set up my view to display an array of items (Circle components) as shown in the image below: However, I am facing a challenge in hiding all other Circle components when I click on one of them. The onPress event is configured to zoom in a ...

Issue encountered during the upgrade to React 16: Unable to resolve 'react/lib/ReactMount' error

After upgrading from React 15 to React 16, I encountered the following error: Error: Module not found - 'react/lib/ReactMount' This issue seems to affect most, if not all, of my components. How can I resolve this? It may be relevant that I a ...