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

Having trouble implementing Material Ui on React? Encountering an error message that reads "Invalid Hook call

`× Error: Oops, looks like there's an issue with the hook call. Remember, hooks can only be used inside a function component. This error might occur for a few reasons: You could be using conflicting versions of React and the renderer (like React DOM ...

React Material Autocomplete: Customize deletion options for selected items

I am working with an Autocomplete feature where I have preselected certain values. Everything is functioning properly at the moment. However, my goal is to prevent users from deleting the preselected options from the Autocomplete. Some suggestions have ...

The bootstrap navbar dropdown feature isn't functioning properly on iPhones

Currently utilizing "bootstrap": "~3.3.4" within the mean.js framework, I am facing an issue with the navbar dropdown menu. On desktop, everything functions as expected - the dropdown opens and remains open when the icon is clicked. However, once deployed ...

Designing with Bootstrap 4 involves incorporating a button that uses the data-toggle attribute set to "collapse" along with an href anchor linked to

I have attempted to link to an ID while also triggering a data-toggle collapse: <a href="#id" data-toggle="collapse" data-target="#collapseTwo" class="btn btn-primary" title="Something cool">Something very cool</a> for linking to this specif ...

Guide to creating an HTML table with bokeh

As a newcomer to Bokeh, I've been searching through the documentation for examples but haven't come across an in-built method for creating tables. I have some data that I need to display in tabular format and was wondering what the most straightf ...

Tips for converting binary image data stored in a database and viewing the images on a web browser

I'm currently working on a web application where users can upload images that are then saved as binary data in my MongoDB using Multer. The issue I'm facing now is how to retrieve and display these images on the webpage. When I try to use the img ...

Create an animated hamburger menu using Tailwind CSS and Angular framework

Within my Angular project, I have successfully integrated Tailwind UI to handle the opening and closing of the hamburger menu by utilizing the code below: <header> <div class="-mr-2 -my-2 md:hidden"> <button type="button ...

Warning: ReactJS has detected a transition in the input type of TextField from uncontrolled to controlled

I encountered the following error message: Warning: TextField is changing an uncontrolled input of type text to be controlled. Input elements should not switch from uncontrolled to controlled (or vice versa). Decide between using a controlled or uncontr ...

Challenges with adjusting the value of HTML range inputs

Within my HTML, I have incorporated the following three elements: <button id="left" >Previous</button> <input id="animSlider" type="range" min="0" max="0" step="1" value="0" /> //THE MAX VALUE IS SET DYNAMICALLY UPON PAGE LOAD, NOT AS ZE ...

Building a custom server rack table using PHP and MySQL is a great way to optimize your

I am in the process of developing a rack server using PHP and MySQL. My first task is to set up a dynamic table with rowspan functionality, followed by implementing drag-and-drop features using Ajax. Although I have researched various resources and attemp ...

Vue 2 does not properly transition the initial element when using the `mode=out-in` attribute

In my Vue project, I am using version 2.6.14 and trying to toggle between two buttons by utilizing v-if and v-else. Despite wrapping everything in a transition element, the first button doesn't transition smoothly, neither when appearing nor disappear ...

putting a division element above a image carousel

Is there a way to overlay a div tag on top of a slideshow, similar to the one shown in this link? In the example provided, a div with the title "DISCOVER THE JOY OF COOKING" is positioned over a slideshow. Any tips on how I can achieve this effect? ...

Changing textarea html content to an iframe in real time with the use of jQuery

Is it possible to use jQuery to transfer HTML content entered into a textarea to an iframe in real time, without refreshing the page? I have searched for a solution but have not found a clear direction. Any code snippet and explanation would be greatly ap ...

Modify the color of Material UI's Select Component's IconComponent

Currently in my project, I am utilizing MUI's Select Component with the LanguageIcon as the designated IconComponent. My goal is to change the color of this icon from black (default) to white, but I have been unsuccessful in my attempts. I attempte ...

Problem encountered with a selection menu

I'm experiencing some trouble with the dropdown menu. Whenever I try to move the mouse to access the dropdown menu, it seems unresponsive. I attempted adjusting the padding on a line of code but ended up moving the entire line instead. Here is the cod ...

When executing code in React JS, I encountered no errors, but the output did not match my expectations

I am facing a challenge with running the Hello World program in React JS using webpack. Attached below is the project structure for reference: https://i.stack.imgur.com/tQXeK.png Upon executing the npm run dev command in the CLI, the browser launches bu ...

The hierarchy of CSS in Vue components

I've developed a customized CSS framework to streamline the design process across all my internal projects. The central file is structured as shown below: @import "~normalize.css/normalize.css"; @import "_variables.scss"; @import "_mixins.scss" ...

Attempting to relocate various containers

My task involves handling a group of randomly placed boxes on a webpage, each painted in random colors. I am currently attempting to enable their movement from one location to another. Despite being a seemingly simple task, my lack of familiarity with mous ...

Resolving the Issue of Jerky Graphics During HTML5 Canvas Animation

When animating a layer in canvas, the visual quality becomes uneven if there are additional layers present. You can see an example of this by clicking "RUN" on this fiddle: http://jsfiddle.net/Q97Wn/ If I modify this line: opts.percentageIndicator.clearR ...

The HTML code may fade away, but the JavaScript is still up and running behind the

Switching between different div elements in my HTML document is a challenge. Here's the code I currently have: <div id="screen1" class="current"> <div id="press_any_key_to_continue"> <font style="font-family: verdana" color="yellow ...