React Application - The division is positioned centrally when run on local host, however, it does not retain its center alignment

I'm encountering an issue with deploying my create-react-app on github pages.

The first image showcases my deployed app on github pages, but the image is not properly centered on the page as intended.

https://i.stack.imgur.com/rOciT.jpg

On the other hand, the second picture demonstrates the appearance of the app when running on my localhost using npm start.

https://i.stack.imgur.com/7w8Hh.jpg

If anyone could assist me in resolving the issue and help center the image on deployment, I would greatly appreciate it. Here's the relevant code:

In my About.js:

import React from 'react';
import me from '../../photos/portraits/portraits8.jpg';
import './About.css';


const About = () =>  {
  return(
    <div>
    <figure className="figure center">
      <img src={me} alt="" className="about-img"/>
      <figcaption className="figcaption bg-color">Hi, my name is Thomas Nguyen, and I’m a freelance photographer based in Morgan Hill, CA. My main focus is automotive photography as well as portrait photography. I'm also a Software Engineer, and this whole website was built with React, Javascript, CSS, and HTML!</figcaption>
    </figure>
    </div>
  );
}


export default About;


In my About.css:

.about-img{
  /*max-height: 500px;*/
  max-height: 400px;
  height: auto\9;
  width: auto;
}

.center {
  display: block;
  margin-left: auto;
  margin-right: auto;
}


.figure {
  display: table;
  margin: 0;
}

.figcaption {
  display: table-caption;
  caption-side: bottom;
  padding: 10px;
}

.bg-color{
  background-color: rgba(78, 100, 124, .1);

}

Answer №1

One puzzling aspect is how it functions in a local environment initially. After transferring the code to a new HTML file, it ceased to work as expected. The styles defined within the .center class were not being applied by the browser, resulting in uncentered content. This issue arises from the inability to use margins on an element with the property display: table. Upon reviewing the classes, it becomes evident that the .figure class takes precedence over the display: block property of the .center class.

To address this, follow these steps:

<style>
.center {
  display: flex;
  justify-content: center;
}
</style>

<div className="center">
    <figure className="figure">
      ...rest of the code
    </figure>
</div>

Remove the .center class from the figure tag and apply it to the surrounding div instead. Adjust the CSS properties of the .center class accordingly to properly center the figure.

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

Using jQuery to scroll within a table

I am currently working on a project that involves displaying specific data rows. To achieve this, I have decided to use a table. For a better understanding, you can check out a table demo here. The issue I am facing is that whenever I change the selected ...

Utilize a module within a script in the continuous integration setup of a React application

I've created a module to verify if all the necessary environment variables are properly set in a React application. Here's a simple example of how it works: const getEnvironmentVariable = (key: string): string => { if (process.env[key]) { ...

"Customizing the topbar of Twitter Bootstrap for right-to

Attempting to implement twitter bootstrap for a top navigation bar on my master page. http://jsfiddle.net/ZqCah/1/ Encountering some issues and seeking assistance: 1- Desiring to switch the direction of all content to right-to-left (rtl). This would me ...

What is the best way to combine Bootstrap and custom CSS, specifically the home.module.css file, in a React project?

I'm currently experimenting with utilizing multiple classes to achieve an elevated button effect and a fade animation on a bootstrap card. Here's the code snippet I've been working on: import Head from 'next/head' impo ...

Looking to Move the Image Up?

I've been experimenting with creating a grid layout where the bottom image will move up until it is 10px away from the image directly above it. However, no matter what I attempt, the spacing seems to be based on the tallest image rather than the one a ...

Error: Unable to process reduction on ships - function "reduce" is not functional

I created a boat visualization tool using a specific API. The API responds with a JSON that I then inject into a table. The issue: At times during the day, I observed the application would abruptly stop functioning and throw an error: Unhandled Rejection ...

Extract the entire div including all its elements and then transmit it using the PHP mail function

Currently, I am developing a feature that allows users to send an email. The email should include only one div from the page which contains elements added using the jQuery drag and clone function. I am unsure how to copy the entire div along with its child ...

Caution: It is not permitted for `<a>` to be nested within another `<a>` tag when working on a Gatsby JS project

I'm currently working with Gatsby JS and react-bootstrap but I'm facing an issue regarding routing. I'm not sure how to properly utilize Gatsby's native routing alongside react-bootstrap, which seems to be using react-router. I attempte ...

The function auth.createUserWithEmailAndPassword is not recognized in the context of React

base.jsx: import { initializeApp } from "firebase/app"; import { getAuth } from "firebase/auth"; const firebaseConfig = { config }; export const app = initializeApp(firebaseConfig); export const auth = getAuth(); Register.jsx ...

How to create a WordPress sidebar that remains fixed while scrolling

I have a WordPress website with a right sidebar that I want to make sticky while scrolling up and down. I applied some CSS styles to achieve this, and now the sidebar is sticky. However, the issue is that it keeps shifting its position from right to left. ...

Tips for stopping a flex:1 div from expanding to accommodate its abundant content

I'm facing a situation where I need to create two columns, one fixed at 150px and the other filling up the remaining space with scroll functionality for overflow content. Despite trying to use flexbox for this layout, the second column keeps expanding ...

Guide to Applying a Custom Style to Every Icon within the Array (React/Material-ui)

I'm currently working with material-ui icons and I have a scenario where I need to assign a specific icon for each object in an array. Instead of manually applying the "iconStyle" property to each individual icon in the array, I am looking for a more ...

I am unable to use the "@" symbol to search for files in the Next platform

Recently, I used @/ to find and import files in Next JS. Due to an issue, I deleted my node modules folder and reinstalled all the packages. After that deletion, @/ stopped functioning properly and I had to change it to ../ instead. This is how my jsconf ...

Although React forms are being detected, the submission is not being recorded

I have been a fan of Netlify forms for quite some time now. However, I am facing an issue while trying to integrate them into my react app for the first time. Although I followed the official guide and the form appears in my dashboard, it doesn't seem ...

What is the best way to toggle the visibility of my menu using JavaScript?

I recently implemented a script to modify a CSS property in my nav bar as I scroll down, triggering the change after reaching 100px. $(window).scroll(function() { var scroll = $(window).scrollTop(); //console.log(scroll); if ...

Execute action based on local state when component is unmounted in React

I have encountered an issue with a component's internal state not being properly propagated throughout the application, specifically under certain events. const FunComponent = (initialDescription, onSave) => { const [description, setDescription] ...

The system displayed an 'Error' stating that the variable 'index' is defined in the code but is never actually utilized, resulting in the (no-unused-vars) warning

I have configured eslint and eslint-plugin-react for my project. Upon running ESLint, I am encountering no-unused-vars errors for every React component in the codebase. It seems like ESLint is not properly identifying JSX or React syntax. Any suggestions ...

The md-autocomplete feature showcases search results in a list format rather than a dropdown menu

Currently, I am working on implementing the Angular Material md-autocomplete tag to provide suggestions for zip codes in a form. To achieve this, I have set up an asynchronous service that fetches the suggestions and populates the results. My reference poi ...

Error: Reactjs - Attempting to access the 'name' property of an undefined variable

As I continue to learn about React, I have been experimenting with props in my code. However, I encountered an error where the prop is appearing as undefined. This issue has left me puzzled since I am still at a basic level of understanding React. If anyo ...

Effortless 'rotational' script

I am currently developing a HTML5 Canvas game with the help of JavaScript. My aim is to create an object that smoothly transitions to a specific direction. The direction is being stored as a variable and calculated in radians. Here's how the code op ...